From 39c9f166ed6213115448b2db11c2d97fbf05e585 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Mon, 25 Feb 2019 02:05:29 +0500 Subject: [PATCH] you can mark command for deletion now with the new `.` command prefix --- CompatBot/Commands/Attributes/LimitedToHelpChannel.cs | 2 +- CompatBot/Commands/Attributes/LimitedToSpamChannel.cs | 2 +- CompatBot/Commands/BaseCommandModuleCustom.cs | 8 ++++++++ CompatBot/Commands/Explain.cs | 2 +- CompatBot/Commands/Sudo.Mod.cs | 2 +- CompatBot/Config.cs | 1 + CompatBot/EventHandlers/AppveyorLinksHandler.cs | 5 +---- CompatBot/EventHandlers/BotReactionsHandler.cs | 3 +++ CompatBot/EventHandlers/GithubLinksHandler.cs | 5 +---- CompatBot/EventHandlers/IsTheGamePlayableHandler.cs | 5 +---- CompatBot/EventHandlers/LogAsTextMonitor.cs | 5 +---- CompatBot/EventHandlers/LogParsingHandler.cs | 6 +----- CompatBot/EventHandlers/PostLogHelpHandler.cs | 5 +---- CompatBot/EventHandlers/ProductCodeLookup.cs | 5 +---- CompatBot/EventHandlers/TableFlipMonitor.cs | 8 +++----- CompatBot/EventHandlers/UnknownCommandHandler.cs | 4 ++++ CompatBot/Program.cs | 2 +- CompatBot/Utils/DefaultHandlerFilter.cs | 7 +++---- CompatBot/Utils/DiscordClientExtensions.cs | 2 +- 19 files changed, 35 insertions(+), 44 deletions(-) diff --git a/CompatBot/Commands/Attributes/LimitedToHelpChannel.cs b/CompatBot/Commands/Attributes/LimitedToHelpChannel.cs index b1fc8b5a..41961634 100644 --- a/CompatBot/Commands/Attributes/LimitedToHelpChannel.cs +++ b/CompatBot/Commands/Attributes/LimitedToHelpChannel.cs @@ -16,7 +16,7 @@ public override async Task ExecuteCheckAsync(CommandContext ctx, bool help if (ctx.Channel.Name.Equals("help", StringComparison.InvariantCultureIgnoreCase)) return true; - await ctx.RespondAsync($"`{Config.CommandPrefix}{ctx.Command.QualifiedName}` is limited to help channel and DMs").ConfigureAwait(false); + await ctx.RespondAsync($"`{ctx.Prefix}{ctx.Command.QualifiedName}` is limited to help channel and DMs").ConfigureAwait(false); return false; } } diff --git a/CompatBot/Commands/Attributes/LimitedToSpamChannel.cs b/CompatBot/Commands/Attributes/LimitedToSpamChannel.cs index 64d97ec4..93395a4d 100644 --- a/CompatBot/Commands/Attributes/LimitedToSpamChannel.cs +++ b/CompatBot/Commands/Attributes/LimitedToSpamChannel.cs @@ -30,7 +30,7 @@ public override async Task ExecuteCheckAsync(CommandContext ctx, bool help catch {} var spamChannel = await ctx.Client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false); - await ctx.RespondAsync($"`{Config.CommandPrefix}{ctx.Command.QualifiedName}` is limited to {spamChannel.Mention} and DMs").ConfigureAwait(false); + await ctx.RespondAsync($"`{ctx.Prefix}{ctx.Command.QualifiedName}` is limited to {spamChannel.Mention} and DMs").ConfigureAwait(false); return false; } diff --git a/CompatBot/Commands/BaseCommandModuleCustom.cs b/CompatBot/Commands/BaseCommandModuleCustom.cs index 808c939d..97d413fd 100644 --- a/CompatBot/Commands/BaseCommandModuleCustom.cs +++ b/CompatBot/Commands/BaseCommandModuleCustom.cs @@ -20,6 +20,14 @@ internal class BaseCommandModuleCustom : BaseCommandModule public override async Task BeforeExecutionAsync(CommandContext ctx) { + try + { + await ctx.Message.DeleteAsync().ConfigureAwait(false); + } + catch (Exception e) + { + Config.Log.Warn(e, "Failed to delete command message with the autodelete command prefix"); + } var disabledCmds = DisabledCommandsProvider.Get(); if (disabledCmds.Contains(ctx.Command.QualifiedName) && !disabledCmds.Contains("*")) { diff --git a/CompatBot/Commands/Explain.cs b/CompatBot/Commands/Explain.cs index 560eebca..7bb37c77 100644 --- a/CompatBot/Commands/Explain.cs +++ b/CompatBot/Commands/Explain.cs @@ -123,7 +123,7 @@ public async Task ShowExplanation(CommandContext ctx, [RemainingText, Descriptio var spamChannel = await ctx.Client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false); inSpecificLocation = $" in {spamChannel.Mention} or bot DMs"; } - var msg = $"Unknown term `{term.Sanitize()}`. Use `{Config.CommandPrefix}explain list` to look at defined terms{inSpecificLocation}"; + var msg = $"Unknown term `{term.Sanitize()}`. Use `{ctx.Prefix}explain list` to look at defined terms{inSpecificLocation}"; await ctx.RespondAsync(msg).ConfigureAwait(false); } diff --git a/CompatBot/Commands/Sudo.Mod.cs b/CompatBot/Commands/Sudo.Mod.cs index 348f85a3..ced0efc9 100644 --- a/CompatBot/Commands/Sudo.Mod.cs +++ b/CompatBot/Commands/Sudo.Mod.cs @@ -23,7 +23,7 @@ public async Task Add(CommandContext ctx, [Description("Discord user to add to t { await ctx.ReactWithAsync(Config.Reactions.Success, $"{user.Mention} was successfully added as moderator!\n" + - $"Try using `{Config.CommandPrefix}help` to see new commands available to you" + $"Try using `{ctx.Prefix}help` to see new commands available to you" ).ConfigureAwait(false); } else diff --git a/CompatBot/Config.cs b/CompatBot/Config.cs index c2abb786..01400b3c 100644 --- a/CompatBot/Config.cs +++ b/CompatBot/Config.cs @@ -14,6 +14,7 @@ namespace CompatBot internal static class Config { public static readonly string CommandPrefix = "!"; + public static readonly string AutoRemoveCommandPrefix = "."; public static readonly ulong BotGuildId = 272035812277878785; // discord server where the bot is supposed to be public static readonly ulong BotGeneralChannelId = 272035812277878785;// #rpcs3; main or general channel where noobs come first thing public static readonly ulong BotChannelId = 291679908067803136; // #compatbot; this is used for !compat/!top results and new builds announcements diff --git a/CompatBot/EventHandlers/AppveyorLinksHandler.cs b/CompatBot/EventHandlers/AppveyorLinksHandler.cs index ed37fd83..e95a2c3c 100644 --- a/CompatBot/EventHandlers/AppveyorLinksHandler.cs +++ b/CompatBot/EventHandlers/AppveyorLinksHandler.cs @@ -21,10 +21,7 @@ internal sealed class AppveyorLinksHandler public static async Task OnMessageCreated(MessageCreateEventArgs args) { - if (args.Author.IsBot) - return; - - if (string.IsNullOrEmpty(args.Message.Content) || args.Message.Content.StartsWith(Config.CommandPrefix)) + if (DefaultHandlerFilter.IsFluff(args.Message)) return; var matches = BuildLinks.Matches(args.Message.Content); diff --git a/CompatBot/EventHandlers/BotReactionsHandler.cs b/CompatBot/EventHandlers/BotReactionsHandler.cs index 91fe5128..6ffc6123 100644 --- a/CompatBot/EventHandlers/BotReactionsHandler.cs +++ b/CompatBot/EventHandlers/BotReactionsHandler.cs @@ -75,6 +75,9 @@ public static async Task OnMessageCreated(MessageCreateEventArgs args) if (DefaultHandlerFilter.IsFluff(args.Message)) return; + if (args.Message.Channel.IsPrivate) + return; + #if DEBUG if (args.Message.Content == "emoji test") { diff --git a/CompatBot/EventHandlers/GithubLinksHandler.cs b/CompatBot/EventHandlers/GithubLinksHandler.cs index 7e1f9023..c772d390 100644 --- a/CompatBot/EventHandlers/GithubLinksHandler.cs +++ b/CompatBot/EventHandlers/GithubLinksHandler.cs @@ -18,10 +18,7 @@ internal static class GithubLinksHandler public static async Task OnMessageCreated(MessageCreateEventArgs args) { - if (args.Author.IsBot) - return; - - if (string.IsNullOrEmpty(args.Message.Content) || args.Message.Content.StartsWith(Config.CommandPrefix)) + if (DefaultHandlerFilter.IsFluff(args.Message)) return; var lastBotMessages = await args.Channel.GetMessagesBeforeAsync(args.Message.Id, 20, DateTime.UtcNow.AddSeconds(-30)).ConfigureAwait(false); diff --git a/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs b/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs index 58872966..b9dd0909 100644 --- a/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs +++ b/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs @@ -30,7 +30,7 @@ internal static class IsTheGamePlayableHandler public static async Task OnMessageCreated(MessageCreateEventArgs args) { - if (args.Author.IsBot) + if (DefaultHandlerFilter.IsFluff(args.Message)) return; #if !DEBUG @@ -43,9 +43,6 @@ public static async Task OnMessageCreated(MessageCreateEventArgs args) && DateTime.UtcNow - lastCheck < CooldownThreshold) return; - if (string.IsNullOrEmpty(args.Message.Content) || args.Message.Content.StartsWith(Config.CommandPrefix)) - return; - #if !DEBUG if (args.Author.IsSmartlisted(args.Client, args.Guild)) return; diff --git a/CompatBot/EventHandlers/LogAsTextMonitor.cs b/CompatBot/EventHandlers/LogAsTextMonitor.cs index 7a7e4bfa..5eb6a0c3 100644 --- a/CompatBot/EventHandlers/LogAsTextMonitor.cs +++ b/CompatBot/EventHandlers/LogAsTextMonitor.cs @@ -14,10 +14,7 @@ internal static class LogAsTextMonitor public static async Task OnMessageCreated(MessageCreateEventArgs args) { - if (args.Author.IsBot) - return; - - if (string.IsNullOrEmpty(args.Message.Content) || args.Message.Content.StartsWith(Config.CommandPrefix)) + if (DefaultHandlerFilter.IsFluff(args.Message)) return; if (!"help".Equals(args.Channel.Name, StringComparison.InvariantCultureIgnoreCase)) diff --git a/CompatBot/EventHandlers/LogParsingHandler.cs b/CompatBot/EventHandlers/LogParsingHandler.cs index d76db472..e44d0869 100644 --- a/CompatBot/EventHandlers/LogParsingHandler.cs +++ b/CompatBot/EventHandlers/LogParsingHandler.cs @@ -41,11 +41,7 @@ static LogParsingHandler() public static Task OnMessageCreated(MessageCreateEventArgs args) { - var message = args.Message; - if (message.Author.IsBot) - return Task.CompletedTask; - - if (!string.IsNullOrEmpty(message.Content) && message.Content.StartsWith(Config.CommandPrefix)) + if (DefaultHandlerFilter.IsFluff(args.Message)) return Task.CompletedTask; OnNewLog(args.Client, args.Channel, args.Message); diff --git a/CompatBot/EventHandlers/PostLogHelpHandler.cs b/CompatBot/EventHandlers/PostLogHelpHandler.cs index 72112070..c9ef4f7c 100644 --- a/CompatBot/EventHandlers/PostLogHelpHandler.cs +++ b/CompatBot/EventHandlers/PostLogHelpHandler.cs @@ -20,7 +20,7 @@ internal static class PostLogHelpHandler public static async Task OnMessageCreated(MessageCreateEventArgs args) { - if (args.Author.IsBot) + if (DefaultHandlerFilter.IsFluff(args.Message)) return; #if !DEBUG @@ -31,9 +31,6 @@ public static async Task OnMessageCreated(MessageCreateEventArgs args) return; #endif - if (string.IsNullOrEmpty(args.Message.Content) || args.Message.Content.StartsWith(Config.CommandPrefix)) - return; - if (!UploadLogMention.IsMatch(args.Message.Content)) return; diff --git a/CompatBot/EventHandlers/ProductCodeLookup.cs b/CompatBot/EventHandlers/ProductCodeLookup.cs index 1b751e9d..7cd5bb31 100644 --- a/CompatBot/EventHandlers/ProductCodeLookup.cs +++ b/CompatBot/EventHandlers/ProductCodeLookup.cs @@ -25,10 +25,7 @@ internal static class ProductCodeLookup public static async Task OnMessageCreated(MessageCreateEventArgs args) { - if (args.Author.IsBot) - return; - - if (string.IsNullOrEmpty(args.Message.Content) || args.Message.Content.StartsWith(Config.CommandPrefix)) + if (DefaultHandlerFilter.IsFluff(args.Message)) return; var lastBotMessages = await args.Channel.GetMessagesBeforeAsync(args.Message.Id, 20, DateTime.UtcNow.AddSeconds(-30)).ConfigureAwait(false); diff --git a/CompatBot/EventHandlers/TableFlipMonitor.cs b/CompatBot/EventHandlers/TableFlipMonitor.cs index d455f23d..fe1862a7 100644 --- a/CompatBot/EventHandlers/TableFlipMonitor.cs +++ b/CompatBot/EventHandlers/TableFlipMonitor.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using CompatBot.Utils; using DSharpPlus.EventArgs; namespace CompatBot.EventHandlers @@ -10,11 +11,7 @@ internal static class TableFlipMonitor public static async Task OnMessageCreated(MessageCreateEventArgs args) { - if (args.Author.IsBot) - return; - - var content = args.Message.Content; - if (string.IsNullOrEmpty(content) || content.StartsWith(Config.CommandPrefix)) + if (DefaultHandlerFilter.IsFluff(args.Message)) return; /* @@ -33,6 +30,7 @@ public static async Task OnMessageCreated(MessageCreateEventArgs args) */ try { + var content = args.Message.Content; if (!(content.Contains("┻━┻") || content.Contains("┻━┻"))) return; diff --git a/CompatBot/EventHandlers/UnknownCommandHandler.cs b/CompatBot/EventHandlers/UnknownCommandHandler.cs index 1217d46a..df8f9a8b 100644 --- a/CompatBot/EventHandlers/UnknownCommandHandler.cs +++ b/CompatBot/EventHandlers/UnknownCommandHandler.cs @@ -15,6 +15,9 @@ internal static class UnknownCommandHandler { public static async Task OnError(CommandErrorEventArgs e) { + if (e.Context.User.IsBot) + return; + if (!(e.Exception is CommandNotFoundException cnfe)) { Config.Log.Error(e.Exception); @@ -25,6 +28,7 @@ public static async Task OnError(CommandErrorEventArgs e) return; if (e.Context.Prefix != Config.CommandPrefix + && e.Context.Prefix != Config.AutoRemoveCommandPrefix && (e.Context.Message.Content?.EndsWith("?") ?? false) && e.Context.CommandsNext.RegisteredCommands.TryGetValue("8ball", out var cmd)) { diff --git a/CompatBot/Program.cs b/CompatBot/Program.cs index 8208a3d8..f86b0e1e 100644 --- a/CompatBot/Program.cs +++ b/CompatBot/Program.cs @@ -121,7 +121,7 @@ internal static async Task Main(string[] args) { var commands = client.UseCommandsNext(new CommandsNextConfiguration { - StringPrefixes = new[] {Config.CommandPrefix}, + StringPrefixes = new[] {Config.CommandPrefix, Config.AutoRemoveCommandPrefix}, Services = new ServiceCollection().BuildServiceProvider(), }); commands.RegisterConverter(new CustomDiscordChannelConverter()); diff --git a/CompatBot/Utils/DefaultHandlerFilter.cs b/CompatBot/Utils/DefaultHandlerFilter.cs index cea71f06..4cd86c1f 100644 --- a/CompatBot/Utils/DefaultHandlerFilter.cs +++ b/CompatBot/Utils/DefaultHandlerFilter.cs @@ -6,13 +6,12 @@ internal static class DefaultHandlerFilter { internal static bool IsFluff(DiscordMessage message) { - if (message.Channel.IsPrivate) - return true; - if (message.Author.IsBot) return true; - if (string.IsNullOrEmpty(message.Content) || message.Content.StartsWith(Config.CommandPrefix)) + if (string.IsNullOrEmpty(message.Content) + || message.Content.StartsWith(Config.CommandPrefix) + || message.Content.StartsWith(Config.AutoRemoveCommandPrefix)) return true; return false; diff --git a/CompatBot/Utils/DiscordClientExtensions.cs b/CompatBot/Utils/DiscordClientExtensions.cs index 5dcedf55..84c841ce 100644 --- a/CompatBot/Utils/DiscordClientExtensions.cs +++ b/CompatBot/Utils/DiscordClientExtensions.cs @@ -90,7 +90,7 @@ public static Task RemoveReactionAsync(this CommandContext ctx, DiscordEmoji emo public static Task ReactWithAsync(this CommandContext ctx, DiscordEmoji emoji, string fallbackMessage = null, bool? showBoth = null) { - return ReactWithAsync(ctx.Message, ctx.Client, emoji, fallbackMessage, showBoth); + return ReactWithAsync(ctx.Message, ctx.Client, emoji, fallbackMessage, showBoth ?? (ctx.Prefix == Config.AutoRemoveCommandPrefix)); } public static async Task> GetMessagesBeforeAsync(this DiscordChannel channel, ulong beforeMessageId, int limit = 100, DateTime? timeLimit = null)