diff --git a/CompatBot/Commands/Attributes/TriggersTyping.cs b/CompatBot/Commands/Attributes/TriggersTyping.cs index 7c2a2180..8ecfcd94 100644 --- a/CompatBot/Commands/Attributes/TriggersTyping.cs +++ b/CompatBot/Commands/Attributes/TriggersTyping.cs @@ -1,23 +1,16 @@ using System; -using System.Threading.Tasks; using DSharpPlus.CommandsNext; -using DSharpPlus.CommandsNext.Attributes; namespace CompatBot.Commands.Attributes { [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)] - internal class TriggersTyping: CheckBaseAttribute + internal class TriggersTyping: Attribute { public bool InDmOnly { get; set; } - public override async Task ExecuteCheckAsync(CommandContext ctx, bool help) + public bool ExecuteCheck(CommandContext ctx) { - if (help) - return true; - - if (!InDmOnly || ctx.Channel.IsPrivate) - await ctx.TriggerTypingAsync().ConfigureAwait(false); - return true; + return !InDmOnly || ctx.Channel.IsPrivate; } } } \ No newline at end of file diff --git a/CompatBot/Commands/CustomBaseCommand.cs b/CompatBot/Commands/CustomBaseCommand.cs index b739c417..ca2092dc 100644 --- a/CompatBot/Commands/CustomBaseCommand.cs +++ b/CompatBot/Commands/CustomBaseCommand.cs @@ -1,6 +1,8 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using CompatBot.Commands.Attributes; using CompatBot.Database.Providers; +using CompatBot.Utils; using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.Entities; @@ -18,7 +20,23 @@ namespace CompatBot.Commands throw new DSharpPlus.CommandsNext.Exceptions.ChecksFailedException(ctx.Command, ctx, new CheckBaseAttribute[] {new RequiresDm()}); } + if (TriggersTyping(ctx)) + await ctx.ReactWithAsync(Config.Reactions.PleaseWait).ConfigureAwait(false); + await base.BeforeExecutionAsync(ctx).ConfigureAwait(false); } + + public override async Task AfterExecutionAsync(CommandContext ctx) + { + if (TriggersTyping(ctx)) + await ctx.RemoveReactionAsync(Config.Reactions.PleaseWait).ConfigureAwait(false); + + await base.AfterExecutionAsync(ctx).ConfigureAwait(false); + } + + private static bool TriggersTyping(CommandContext ctx) + { + return ctx.Command.CustomAttributes.OfType().FirstOrDefault() is TriggersTyping a && a.ExecuteCheck(ctx); + } } } \ No newline at end of file diff --git a/CompatBot/Commands/Pr.cs b/CompatBot/Commands/Pr.cs index 1df3a138..318701e9 100644 --- a/CompatBot/Commands/Pr.cs +++ b/CompatBot/Commands/Pr.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using AppveyorClient.POCOs; using CompatApiClient.Utils; +using CompatBot.Commands.Attributes; using CompatBot.Utils; using CompatBot.Utils.ResultFormatters; using DSharpPlus.CommandsNext; @@ -12,7 +13,7 @@ using GithubClient.POCOs; namespace CompatBot.Commands { - [Group("pr")] + [Group("pr"), TriggersTyping] [Description("Commands to list opened pull requests information")] internal sealed class Pr: BaseCommandModuleCustom { diff --git a/CompatBot/Config.cs b/CompatBot/Config.cs index b088302c..cee62e14 100644 --- a/CompatBot/Config.cs +++ b/CompatBot/Config.cs @@ -73,6 +73,7 @@ namespace CompatBot public static readonly DiscordEmoji Starbucks = DiscordEmoji.FromUnicode("☕"); public static readonly DiscordEmoji Moderated = DiscordEmoji.FromUnicode("🔨"); public static readonly DiscordEmoji No = DiscordEmoji.FromUnicode("😐"); + public static readonly DiscordEmoji PleaseWait = DiscordEmoji.FromUnicode("👀"); } public static class Moderation diff --git a/CompatBot/Utils/DiscordClientExtensions.cs b/CompatBot/Utils/DiscordClientExtensions.cs index eeb58c23..d773e08a 100644 --- a/CompatBot/Utils/DiscordClientExtensions.cs +++ b/CompatBot/Utils/DiscordClientExtensions.cs @@ -48,6 +48,18 @@ namespace CompatBot.Utils } } + public static async Task RemoveReactionAsync(this DiscordMessage message, DiscordEmoji emoji) + { + try + { + await message.DeleteOwnReactionAsync(emoji).ConfigureAwait(false); + } + catch (Exception e) + { + Config.Log.Warn(e); + } + } + public static async Task ReactWithAsync(this DiscordMessage message, DiscordClient client, DiscordEmoji emoji, string fallbackMessage = null, bool showBoth = false) { try @@ -64,6 +76,11 @@ namespace CompatBot.Utils } } + public static Task RemoveReactionAsync(this CommandContext ctx, DiscordEmoji emoji) + { + return RemoveReactionAsync(ctx.Message, emoji); + } + public static Task ReactWithAsync(this CommandContext ctx, DiscordEmoji emoji, string fallbackMessage = null, bool showBoth = false) { return ReactWithAsync(ctx.Message, ctx.Client, emoji, fallbackMessage, showBoth);