public async Task Register(CommandContext ctx) { using (var context = new BotContext()) { foreach (Server s in context.Servers) { if (s.ServerSnow == ctx.Guild.Id) { await ctx.RespondAsync("You're already registered! Thank you!"); break; } } var server = new Server { ServerName = ctx.Guild.Name, ServerSnow = ctx.Guild.Id, SettingsJson = "{ \"warnWords\": [], \"deleteWords\": []}" }; await context.Servers.AddAsync(server); foreach (var user in ctx.Guild.Members) { if (!user.IsBot) { var newUser = new User { UserSnow = user.Id, Name = user.Username, Discriminator = user.Discriminator, Guild = server, Strikes = 0 }; await context.Users.AddAsync(newUser); } } try { await context.SaveChangesAsync(); } catch (Exception e) { Console.WriteLine(e.Message); throw; } } await ctx.RespondAsync("Registering server..."); }
public async Task RemoveDelete(CommandContext ctx, string Word) { using (var context = new BotContext()) { Server server = new Server() { ServerSnow = 0, SettingsJson = "{ \"warnWords\": [], \"deleteWords\": []}" }; foreach (Server s in context.Servers) { if (s.ServerSnow == ctx.Guild.Id) { server = s; } break; } if (server.ServerSnow == 0) { await ctx.RespondAsync("Sorry, you aren't registered in the database, please run the command '<>Admin Register' and then try this again!"); return; } var settings = Setting.FromJson(server.SettingsJson); settings.DeleteWords.Remove(Word); server.SettingsJson = Serialize.ToJson(settings); await context.SaveChangesAsync(); await ctx.RespondAsync($"Removed word '{Word}' from your list of delete words"); } }
static async Task MainAsync(string[] args) { string jsonData = File.ReadAllText("appSettings.Debug.json"); JObject jsonObject = JObject.Parse(jsonData); JToken jsonConnString = jsonObject["connString"]; string connString = jsonConnString.ToString(); DataHandler.DiscordToken = jsonObject["disKey"].ToString(); DataHandler.MWKey = jsonObject["mwKey"].ToString(); DataHandler.OpenWeather = jsonObject["oWeather"].ToString(); DataHandler.ConnString = jsonObject["connString"].ToString(); using (var context = new BotContext()) { await context.Database.EnsureCreatedAsync(); } discord = new DiscordClient(new DiscordConfiguration { Token = DataHandler.DiscordToken, TokenType = TokenType.Bot, UseInternalLogHandler = true, LogLevel = LogLevel.Debug }); discord.GuildCreated += async e => { using (var context = new BotContext()) { var server = new Server { ServerName = e.Guild.Name, ServerSnow = e.Guild.Id, SettingsJson = "{ \"warnWords\": [], \"deleteWords\": []}" }; await context.Servers.AddAsync(server); foreach (var user in e.Guild.Members) { var newUser = new User { UserSnow = user.Id, Name = user.Username, Discriminator = user.Discriminator, Guild = server, Strikes = 0 }; await context.Users.AddAsync(newUser); } await context.SaveChangesAsync(); } }; discord.MessageCreated += async e => { using (var context = new BotContext()) { var servers = context.Servers .Include(b => b.Users) .ToList(); Server s = servers.Find(x => x.ServerSnow == e.Guild.Id); User u = s.Users.ToList().Find(x => x.UserSnow == e.Message.Author.Id); if (u.Muted == true) { await e.Message.DeleteAsync(); return; } } }; discord.MessageCreated += async e => { if (e.Author.IsBot == false) { try { var member = (DiscordMember)e.Author; bool admin = false; foreach (DiscordRole role in member.Roles) { if (role.Permissions.HasPermission(Permissions.ManageGuild)) { admin = true; } } if (admin == false) { await CheckMessageClean(e); } } catch (System.Exception) { return; } } }; commands = discord.UseCommandsNext(new CommandsNextConfiguration { StringPrefix = "<>", CaseSensitive = false }); commands.RegisterCommands <Commands>(); await discord.ConnectAsync(); await Task.Delay(-1); }
public static async Task CheckMessageClean(DSharpPlus.EventArgs.MessageCreateEventArgs args) { try { using var context = new BotContext(); var servers = context.Servers .Include(b => b.Users) .ToList(); Server server = new Server() { SettingsJson = "{ \"warnWords\": [], \"deleteWords\": []}", Users = new User[] { new User() { UserSnow = 0 } } }; foreach (Server s in servers) { if (s.ServerSnow == args.Guild.Id) { server = s; break; } } var users = server.Users; foreach (User u in users) { if (u.UserSnow == args.Author.Id) { if (u.Strikes >= 3) { await args.Message.DeleteAsync(); await args.Guild.Owner.SendMessageAsync($"User {args.Author.Username}#{args.Author.Discriminator} has just hit 3 strikes for time number {u.TotalMutes}. They're being auto-muted until you or an admin remove it."); u.Strikes = 0; } } } var settings = Setting.FromJson(server.SettingsJson); foreach (string word in settings.WarnWords) { if (args.Message.Content.Contains(word)) { await args.Message.RespondAsync("Hey now, that's not allowed here. I'm gonna give you a warning for now. Three warnings will give a temporary mute, so be careful!"); foreach (User u in server.Users) { if (u.UserSnow == args.Author.Id) { ++u.Strikes; if (u.Strikes >= 3) { await args.Message.DeleteAsync(); await args.Guild.Owner.SendMessageAsync($"User {args.Author.Username}#{args.Author.Discriminator} has just hit 3 strikes for time number {u.TotalMutes}d. They're being auto-muted until you or an admin remove it."); u.Strikes = 0; } } } break; } } foreach (string word in settings.DeleteWords) { if (args.Message.Content.Contains(word)) { await args.Message.RespondAsync("Wow, that was too much for me to just warn you, that's getting deleted, and the owner of the server will be notified as well."); await args.Message.DeleteAsync($"Language: {word}"); await args.Guild.Owner.SendMessageAsync($"Message deleted in channel #{args.Channel.Name}"); } } await context.SaveChangesAsync(); } catch (System.Exception e) { System.Console.WriteLine(e.InnerException); throw; } }