using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Discord.WebSocket; using Discord.Commands; using System.Reflection; using System.IO; using System.Data; using Discord; namespace Kehyeedra3 { //Set CommandHandler as partial class of Bot class CommandHandler : Bot { public static async Task InstallCommands() { //adds HandleCommand to handle the commands from message received _bot.MessageReceived += HandleCommand; await InstallModules(); } private static async Task HandleCommand(SocketMessage arg) { if (!arg.Author.IsBot) { if(arg is SocketUserMessage message) { if (message == null) return; var Context = new SocketCommandContext(_bot, message); using (var Database = new ApplicationDbContextFactory().CreateDbContext()) { if (!Database.Users.Any(x => x.Id == arg.Author.Id)) { Database.Users.Add(new Services.Models.User { Id = arg.Author.Id, Avatar = arg.Author.GetAvatarUrl() ?? arg.Author.GetDefaultAvatarUrl(), Username = arg.Author.Username }); await Database.SaveChangesAsync(); } else { var user = Database.Users.FirstOrDefault(x => x.Id == arg.Author.Id); user.Username = arg.Author.Username; user.Avatar = arg.Author.GetAvatarUrl() ?? arg.Author.GetDefaultAvatarUrl(); await Database.SaveChangesAsync(); } } int argPos = 0; if (message.Content.Contains("\uD83C\uDD71")) //B emoji detector { await Context.Channel.SendMessageAsync($"B emoji detected. Proceed to kill yourself, {Context.User.Mention}"); } if (message.Content.ToLowerInvariant().Contains("jojo")) { var jojoke = WeebClient.DownloadString("https://api.skuldbot.uk/fun/jojoke/?raw"); await Context.Channel.SendMessageAsync($"{Context.User.Mention} is that a fucksnifflerling {jojoke} reference?"); } if (message.Channel is IGuildChannel chan) { var perms = chan.GetPermissionOverwrite(_bot.CurrentUser); if (perms.HasValue) { if (perms.Value.SendMessages == PermValue.Deny) return; } var botGuild = _bot.GetGuild(chan.GuildId).GetUser(_bot.CurrentUser.Id); bool exit = false; botGuild.Roles.OrderByDescending(x => x.Position).ToList().ForEach(x => { perms = chan.GetPermissionOverwrite(x); if (perms.HasValue) { if (perms.Value.SendMessages == PermValue.Deny) { exit = true; return; } } }); if (exit) return; } if (!(message.HasStringPrefix(Configuration.Load().Prefix, ref argPos))) return; { var result = await _cmds.ExecuteAsync(Context, argPos, _dmap); if (result.IsSuccess) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Sent successfully"); Console.ForegroundColor = ConsoleColor.White; } else { await Context.Channel.SendMessageAsync($"Command failed with the following error:\n{result.ErrorReason}\nPlease make sure your brain is plugged in and charging."); Console.ForegroundColor = ConsoleColor.Red; //set text red Console.WriteLine($"Something went wrong\n{result.ErrorReason}"); Console.ForegroundColor = ConsoleColor.White; //back to white } } } } } public static async Task InstallModules() { await _cmds.AddModulesAsync(Assembly.GetEntryAssembly(), _dmap); } } }