Small update to the main Program.cs file

This commit is contained in:
Purplebored
2023-09-24 20:42:16 +02:00
parent 63ff45bfa3
commit e7abfa7b35

View File

@@ -10,7 +10,13 @@ using Microsoft.Extensions.Configuration;
class Program
{
public static RevoltClient Client;
public static async Task Start()
public static async Task Main(string[] args)
{
await StartAsync();
}
public static async Task StartAsync()
{
IConfiguration Configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
@@ -26,70 +32,82 @@ class Program
});
await Client.StartAsync();
await Client.CurrentUser.ModifySelfAsync(statusText: new Option<string>("God please someone kill me."));
await Client.CurrentUser.ModifySelfAsync(statusType: new Option<UserStatusType>(UserStatusType.Focus));
CommandHandler CommandHandler = new CommandHandler(Client);
CommandHandler.LoadCommands();
await Task.Delay(-1);
}
static void Main(string[] args)
{
Start().GetAwaiter().GetResult();
// Modify user status
await Client.CurrentUser.ModifySelfAsync(
statusText: new Option<string>("God please someone kill me."),
statusType: new Option<UserStatusType>(UserStatusType.Focus)
);
CommandHandler commandHandler = new CommandHandler(Client);
commandHandler.LoadCommands();
await Task.Delay(-1);
}
}
// CommandHandler
public class CommandHandler
{
{
private readonly RevoltClient _client;
private readonly CommandService _service = new CommandService();
// Here you can change the prefix. Default is "?"
public const string Prefix = "?";
public CommandHandler(RevoltClient client)
{
Client = client;
Client.OnMessageRecieved += Client_OnMessageRecieved;
Service.OnCommandExecuted += Service_OnCommandExecuted;
_client = client;
_client.OnMessageRecieved += Client_OnMessageReceived;
_service.OnCommandExecuted += Service_OnCommandExecuted;
}
private RevoltClient Client;
private CommandService Service = new CommandService();
// Here you can change the prefix Default one is "?"
public const string Prefix = "?";
public async Task LoadCommands()
{
await Service.AddModulesAsync(Assembly.GetEntryAssembly(), null);
// Load commands from the entry assembly
await _service.AddModulesAsync(Assembly.GetEntryAssembly(), null);
}
private void Client_OnMessageRecieved(Message msg)
private void Client_OnMessageReceived(Message msg)
{
UserMessage Message = msg as UserMessage;
if (Message == null || Message.Author.IsBot)
if (msg is not UserMessage userMessage || userMessage.Author.IsBot)
return;
int argPos = 0;
if (!(Message.HasCharPrefix('?', ref argPos) || Message.HasMentionPrefix(Client.CurrentUser, ref argPos)))
if (!(userMessage.HasCharPrefix('?', ref argPos) ||
userMessage.HasMentionPrefix(_client.CurrentUser, ref argPos)))
return;
CommandContext context = new CommandContext(Client, Message);
Service.ExecuteAsync(context, argPos, null);
CommandContext context = new CommandContext(_client, userMessage);
_service.ExecuteAsync(context, argPos, null);
}
private void Service_OnCommandExecuted(Optional<CommandInfo> commandinfo, CommandContext context, IResult result)
private void Service_OnCommandExecuted(Optional<CommandInfo> commandInfo, CommandContext context, IResult result)
{
if (result.IsSuccess)
Console.WriteLine("Success command: " + commandinfo.Value.Name);
{
Console.WriteLine("Success command: " + commandInfo.Value.Name);
}
else
{
if (!commandinfo.HasValue)
return;
if (commandInfo.HasValue)
{
context.Channel.SendMessageAsync("Error: " + result.ErrorReason);
}
}
// Only god and the dude behind RevoltSharp know what's going on here.
}
// Only god and the developer of RevoltSharp know what's going on here.
}
public class Commands : ModuleBase
{
// Help commands:
// Basic Help Command.
// Help command.
[Command("help")]
public async Task Help()
{
var helpMessage = "#### List of available commands:\n\n" +
var helpMessage =
"#### List of available commands:\n\n" +
"#### Practical Commands: \n" +
"`?help` - Displays this command.\n" +
"`?source` - Links to the bots source code. \n" +
@@ -99,18 +117,18 @@ public class Commands : ModuleBase
"`?calculate` {num} {+, - , / , * } {num} - A very simple calculator. \n" +
"`?mod-help` - Displays a list of available mod commands. \n " +
"`?ping` - tests the bot ping. \n" +
"`?nsfw-help` - Sends a list of all aviable nsfw commands. \n " +
"`?nsfw-help` - Sends a list of all available nsfw commands. \n " +
"#### Fun Commands: \n" +
"`?dice` - Rolls a random number between 1 and 6.\n" +
"`?say` - says what the user told it to say!. \n" +
"`?dm` - Just DMs the user Hi :3. \n" +
"`?rps {paper,rock,scissors}` - Simple Rock paper scissors game.\n" +
"`?dogfact` - Gives a random dogfact using the Dogfact API!. \n" +
"`?catfact` - Gives a random Catfact using cat fact API (Currently somewhat bugged with the []. \n" +
"`?catfact` - Gives a random Catfact using cat fact API (Currently somewhat bugged with the []). \n" +
"`?joke` - Very simple command just gives a joke using the Joke API. \n " +
"`?flipcoin` - a Command so easy a child could do it. \n " +
"`?fact` - Gives a random useless fact. \n " +
"`?urban + {word}` - uses the urban dictionary the search for the word. \n" +
"`?urban + {word}` - uses the urban dictionary to search for the word. \n" +
"`?shitpost` - Sends a random shitpost. (The shitpost API is broken.) \n " +
"`?cat` - Cat :3 \n " +
"`?neko` - Neko command \n " +
@@ -120,14 +138,21 @@ public class Commands : ModuleBase
"`?avatar` - Sends the user Avatar (Beta) \n ";
await ReplyAsync(helpMessage);
}
// End of the Help command.
// Mod Help command
[Command("mod-help")]
public async Task modhelp()
public async Task ModHelp()
{
await ReplyAsync(
"## List Of Available mod commands. \n `?kick {Mention}` - Kicks the user \n `?ban {mentions}` - Bans the User \n `?unban {mention}` - Unbans the user. \n");
string helpMessage =
"## List Of Available mod commands. \n" +
"`?kick {Mention}` - Kicks the user \n" +
"`?ban {mentions}` - Bans the User \n" +
"`?unban {mention}` - Unbans the user. \n";
await ReplyAsync(helpMessage);
}
// End of the mod-Help command
// NSFW Help command:
[Command("nsfw-help")]
@@ -135,42 +160,54 @@ public class Commands : ModuleBase
{
await ReplyAsync("### Here are all the available NSFW commands: \n `?r34 {search}` - Searches rule34.xxx \n `?hentai` - Grabs a random hentai image \n ");
}
// End of the help command Section
// End of the NSFW-HELP command
// End of the help commands
// Dm Commands (commands that dm the user in some way.):
// Invite Command - Sends a invite in the DM
[Command("invite")]
public async Task Invite()
{
DMChannel DM = await Context.User.GetDMChannelAsync();
if (DM == null)
{
await ReplyAsync("Could not open DM :( wich means no invite :(((");
await ReplyAsync("Could not open DM :(");
return;
}
await DM.SendMessageAsync("## Invite me! \n" +
"If you on app.haydar.dev use this link: \n" +
await DM.SendMessageAsync(
"## Invite me! \n" +
"If you're on app.haydar.dev, use this link: \n" +
"https://app.haydar.dev/bot/01HA55V3K8B26T87TBKMZMWRKJ \n" +
"but if you on nightly.haydar.dev use this link \n" +
"But if you're on nightly.haydar.dev, use this link: \n" +
"https://nightly.haydar.dev/bot/01HA55V3K8B26T87TBKMZMWRKJ \n" +
"If you find any bugs report them to the bots creator. Thank you bye!");
"If you find any bugs, report them to the bot's creator. Thank you and bye!"
);
}
// End of the Invite command
// The Debug command.
[Command("debug")]
public async Task Debug()
{
DMChannel DM = await Context.User.GetDMChannelAsync();
if (DM == null)
{
await ReplyAsync("Your code is so messed up that even DMs are broken :skull: wow");
return;
}
try
{
// Send initial message
await DM.SendMessageAsync("DMs work if you received this message.");
await DM.SendMessageAsync("-----");
await DM.SendMessageAsync("You should recive the ping of the bot and a shitpost");
await DM.SendMessageAsync("You should receive the ping of the bot and a shitpost");
// Ping part
var stopwatch = new Stopwatch();
stopwatch.Start();
@@ -179,17 +216,18 @@ public class Commands : ModuleBase
var latency = stopwatch.ElapsedMilliseconds;
await DM.SendMessageAsync($"The ping is: {latency}ms");
await DM.SendMessageAsync("-----");
// Shitpost part
await SendRandomMeme(DM);
await DM.SendMessageAsync("If you saw a Shitpost and the ping of the bot everything should work correctly.");
}
catch (Exception ex)
{
await DM.SendMessageAsync($"An error occurred: {ex.Message}");
}
}
// Shitpost Logic
// The Shitpost Part.
private async Task SendRandomMeme(DMChannel DM)
{
const string apiUrl = "https://api.thedailyshitpost.net/random";
@@ -221,7 +259,9 @@ public class Commands : ModuleBase
}
}
}
// End of the Debug command.
// End of DM commands.
// Debug commands.
@@ -232,6 +272,7 @@ public class Commands : ModuleBase
{
await ReplyAsync("Ig it works :tm:");
}
// Ping command.
[Command("ping")]
public async Task PingCommand()
@@ -246,9 +287,11 @@ public class Commands : ModuleBase
await ReplyAsync($"Pong! (Latency: {latency}ms)");
}
// End of the Debug commands.
// Fun commands:
// Gif command
[Command("gif")]
public async Task GifCommand([Remainder] string keyword)
@@ -287,8 +330,9 @@ public class Commands : ModuleBase
}
}
}
// End of the GIF commands
// A Rock paper Scicors command.
// A Rock paper Scissors command.
[Command("rps")]
public async Task Rps([Remainder] string userChoice = null)
{
@@ -304,7 +348,7 @@ public class Commands : ModuleBase
userChoice = userChoice.ToLower();
if (Array.Exists(options, option => option == userChoice))
if (options.Contains(userChoice))
{
if (userChoice == botChoice)
{
@@ -326,21 +370,24 @@ public class Commands : ModuleBase
await ReplyAsync($"Invalid choice. Please choose from: {string.Join(", ", options)}");
}
}
// End of the Rock paper scicors command
// Roll command roles a random number between 1 - 6 (Perfect for bets :troll")
// Dice command rolls a random number between 1 - 6 (Perfect for bets :troll")
[Command("dice")]
public async Task dice()
public async Task Dice()
{
var random = new Random();
var result = random.Next(1, 7);
await ReplyAsync($"You rolled a {result}!");
}
// End of the Dice command
// Very simple very say command.
// Very simple say command.
private List<string> blacklist = new List<string>
{
"nigga", "nigger", "n i g g a", "fuck", "shit", "piss", "cunt", "dick", "fag", "faggot", "kys", "ky$", // Blacklist for the say command
};
{
"nigga", "nigger", "n i g g a", "fuck", "shit", "piss",
"cunt", "dick", "fag", "faggot", "kys", "ky$",
};
[Command("say")]
public async Task Say([Remainder] string text)
@@ -356,19 +403,12 @@ public class Commands : ModuleBase
}
await ReplyAsync($"{user} said: {text}");
}
// End of the Say command
// Haydar.
[Command("miau")]
public async Task miau()
{
await ReplyAsync("<@01H4H8M7E20HJAD6DXKDMXVKXF> Miau???");
}
// The flip a coin command.
[Command("flipcoin")]
public async Task FlipCoin()
// The coinflip command.
[Command("coinflip")]
public async Task Coinflip()
{
Random random = new Random();
int result = random.Next(2);
@@ -377,7 +417,10 @@ public class Commands : ModuleBase
await ReplyAsync($"It's {side}!");
}
// End of the coinflip command.
// End of the Fun commands.
// Api commands
// Advice command.
@@ -385,6 +428,8 @@ public class Commands : ModuleBase
public async Task Advice()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "https://api.adviceslip.com/advice";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -394,7 +439,7 @@ public class Commands : ModuleBase
string adviceJson = await response.Content.ReadAsStringAsync();
JObject adviceObject = JObject.Parse(adviceJson);
string advice = adviceObject["slip"]["advice"]?.ToString();
string advice = adviceObject["slip"]?["advice"]?.ToString();
if (!string.IsNullOrEmpty(advice))
{
@@ -410,12 +455,21 @@ public class Commands : ModuleBase
await ReplyAsync("Sorry, I couldn't fetch advice at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
}
}
// End of the Advice command
// Dog fact command very simple very fun
[Command("dogfact")]
public async Task DogFact()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "http://dog-api.kinduff.com/api/facts";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -443,12 +497,21 @@ public class Commands : ModuleBase
await ReplyAsync("Sorry, I couldn't fetch a dog fact at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
}
}
// End of the Dogfact command
// Cat fact command very simple very fun
[Command("catfact")]
public async Task CatFact()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "http://meowfacts.herokuapp.com/";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -473,12 +536,21 @@ public class Commands : ModuleBase
await ReplyAsync("Sorry, I couldn't fetch a cat fact at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
}
}
// End of the Cat fact command
// Joke command
[Command("joke")]
public async Task Joke()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "https://v2.jokeapi.dev/joke/Any?type=twopart";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -505,12 +577,21 @@ public class Commands : ModuleBase
await ReplyAsync("Sorry, I couldn't fetch a joke at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
}
}
// End of the Joke command
// Simple quote command
[Command("quote")]
public async Task Quote()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "https://api.quotable.io/random";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -530,12 +611,21 @@ public class Commands : ModuleBase
await ReplyAsync("Sorry, I couldn't fetch a quote at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
}
}
// End of the qoute command
// Fact command
[Command("fact")]
public async Task Fact()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "https://uselessfacts.jsph.pl/random.json?language=en";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -553,8 +643,15 @@ public class Commands : ModuleBase
await ReplyAsync("Sorry, I couldn't fetch a fact at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
// Urban Dictionary
}
}
// End of the fact command
// Urban command
[Command("urban")]
public async Task Urban([Remainder] string term)
{
@@ -570,9 +667,14 @@ public class Commands : ModuleBase
if (urbanObject.list.Count > 0)
{
string definition = urbanObject.list[0].definition;
int maxDefinitions = 3;
await ReplyAsync($"**Term:** {term}\n**Definition:** {definition}");
for (int i = 0; i < Math.Min(maxDefinitions, urbanObject.list.Count); i++)
{
string definition = urbanObject.list[i].definition;
string formattedMessage = $"```\nTerm: {term}\nDefinition {i + 1}:\n{definition}\n```";
await ReplyAsync(formattedMessage);
}
}
else
{
@@ -585,11 +687,15 @@ public class Commands : ModuleBase
}
}
}
// End of the Urban command
// Shitpost command
[Command("shitpost")]
public async Task shitpost()
public async Task Shitpost()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "https://api.thedailyshitpost.net/random";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -602,19 +708,28 @@ public class Commands : ModuleBase
string title = memeObject.title;
string imageUrl = memeObject.url;
await ReplyAsync($"{imageUrl}");
await ReplyAsync(imageUrl);
}
else
{
await ReplyAsync("Sorry, I couldn't fetch a meme at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
// Cat.
}
}
// End of the shitpost command
// Cat command.
[Command("cat")]
public async Task Cat()
{
using (HttpClient client = new HttpClient())
{
try
{
string apiUrl = "https://api.thecatapi.com/v1/images/search";
HttpResponseMessage response = await client.GetAsync(apiUrl);
@@ -624,8 +739,10 @@ public class Commands : ModuleBase
string catJson = await response.Content.ReadAsStringAsync();
dynamic catObject = Newtonsoft.Json.JsonConvert.DeserializeObject(catJson);
string imageUrl = catObject[0].url;
string imageUrl = catObject[0]?.url;
if (!string.IsNullOrEmpty(imageUrl))
{
await ReplyAsync(imageUrl);
}
else
@@ -633,7 +750,19 @@ public class Commands : ModuleBase
await ReplyAsync("Sorry, I couldn't fetch a cat image at the moment. Please try again later.");
}
}
else
{
await ReplyAsync("Sorry, I couldn't fetch a cat image at the moment. Please try again later.");
}
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
}
}
// End of the cat command
// Neko command
[Command("neko")]
public async Task Neko()
@@ -676,13 +805,16 @@ public class Commands : ModuleBase
// End of neko command
// The End of the API commands.
// Misc Commands:
// Give the credits of the bot
[Command("credits")]
public async Task Credits()
{
await ReplyAsync("This bot is made using revoltsharp by Purplebored known as Kniaż Jarema on nightly");
}
// End of the Credits command
// Avatar command
[Command("avatar")]
@@ -693,7 +825,7 @@ public class Commands : ModuleBase
await Context.Channel.SendMessageAsync($"Your avatar: {avatarUrl}");
}
// End of the misc commands
// End of the Avatar command
// Calculate command
[Command("calculate")]
@@ -738,28 +870,38 @@ public class Commands : ModuleBase
await ReplyAsync($"Result: {result}");
}
// End of the Calculat command.
// Moderation commands. from Darkly Bot
// Stuff neded to make these commands work.
// End of the misc commands
// Mod Commands
// Stuff needed to make these commands work.
private string RemoveMention(string mention)
{
return mention.Replace("<@", "").Replace(">", "");
}
private RevoltClient Client;
// Stats command
[Command("stats")]
public async Task GetStats()
{
try
{
IReadOnlyCollection<ServerMember> result1 = await Context.Server.GetMembersAsync();
int num2 = 0;
foreach (ServerMember serverMember in result1)
++num2;
int num2 = result1.Count;
IReadOnlyCollection<ServerMember> result2 = await Context.Server.GetMembersAsync(true);
int num3 = 0;
foreach (ServerMember serverMember in result2)
++num3;
await ReplyAsync($"### Stats:\n \n ### Member count: {num2}\n ### Online member count: {num3}\n");
int num3 = result2.Count;
await ReplyAsync($"### Stats:\n\n### Member count: {num2}\n### Online member count: {num3}\n");
}
catch (Exception ex)
{
await ReplyAsync($"An error occurred: {ex.Message}");
}
}
// Ban Command
[Command("ban")]
public async Task BanUser([Remainder] string args)
@@ -768,13 +910,14 @@ public class Commands : ModuleBase
{
string userId = RemoveMention(args);
await Context.Server.BanMemberAsync(userId, "Banned via command.");
await ReplyAsync("### Ban\n whomp whomp.");
await ReplyAsync("### Ban\nwhomp whomp.");
}
catch
{
await ReplyAsync("### ERROR \n Invalid Mention or Bot Perms.");
await ReplyAsync("### ERROR\nInvalid Mention or Bot Permissions.");
}
}
// Unban Command
[Command("unban")]
public async Task UnbanUser([Remainder] string args)
@@ -783,13 +926,14 @@ public class Commands : ModuleBase
{
string userId = RemoveMention(args);
await Context.Server.UnBanMemberAsync(userId);
await ReplyAsync("### Unbanned \n Very sadly a mod had a change of his heart.");
await ReplyAsync("### Unbanned\nVery sadly a mod had a change of heart.");
}
catch
{
await ReplyAsync("### ERROR \n Invalid Mention or Bot Perms.");
await ReplyAsync("### ERROR\nInvalid Mention or Bot Permissions.");
}
}
// Kick Command
[Command("kick")]
public async Task KickUser([Remainder] string args)
@@ -798,17 +942,23 @@ public class Commands : ModuleBase
{
string userId = RemoveMention(args);
await Context.Server.KickMemberAsync(userId);
await ReplyAsync("### Kicked \n And he gone.");
await ReplyAsync("### Kicked\nAnd he's gone.");
}
catch
{
await ReplyAsync("### ERROR \\n Invalid Mention or Bot Perms.");
await ReplyAsync("### ERROR\nInvalid Mention or Bot Permissions.");
}
}
// End of mod commands
// NSFW COMMANDS
// R34 command.
[Command("r34")]
public async Task ExampleCommand([Remainder] string query = null)
{
try
{
if (string.IsNullOrWhiteSpace(query))
{
@@ -818,8 +968,7 @@ public class Commands : ModuleBase
if (!(Context.Channel is TextChannel textChannel) || !textChannel.IsNsfw)
{
await Context.Channel.SendMessageAsync(
"This command is only allowed in NSFW channels. So go to a NSFW channel to get your NSFW smh");
await Context.Channel.SendMessageAsync("This command is only allowed in NSFW channels. So go to a NSFW channel to get your NSFW smh");
return;
}
@@ -846,19 +995,25 @@ public class Commands : ModuleBase
}
else
{
await Context.Channel.SendMessageAsync(
"Sorry, I couldn't fetch an image at the moment. Please try again later.");
await Context.Channel.SendMessageAsync("Sorry, I couldn't fetch an image at the moment. Please try again later.");
}
}
}
catch (Exception ex)
{
await Context.Channel.SendMessageAsync($"An error occurred: {ex.Message}");
}
}
// Hentai command.
[Command("hentai")]
public async Task hentai()
public async Task Hentai()
{
try
{
if (!(Context.Channel is TextChannel textChannel) || !textChannel.IsNsfw)
{
await Context.Channel.SendMessageAsync(
"This command is only allowed in NSFW channels. So go to a NSFW channel to get your NSFW smh");
await Context.Channel.SendMessageAsync("This command is only allowed in NSFW channels. So go to a NSFW channel to get your NSFW smh");
return;
}
using (HttpClient client = new HttpClient())
@@ -881,4 +1036,11 @@ public class Commands : ModuleBase
}
}
}
catch (Exception ex)
{
await Context.Channel.SendMessageAsync($"An error occurred: {ex.Message}");
}
}
// End of NSFW commands
// End of the Bot
}