From 99fe2967b687574a4b58e4223121a41cd87e4a44 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Fri, 9 Mar 2018 23:16:30 -0800 Subject: [PATCH] Moved (created) SendUsageMessageAsync to base class --- Module/ModCommands/Commands/BanKick.cs | 36 ++++++++------------- Module/ModCommands/Commands/Say.cs | 32 ++++++++---------- Module/ModCommands/Commands/Unban.cs | 25 ++++---------- Module/ModCommands/Commands/_CommandBase.cs | 25 +++++++++++++- 4 files changed, 58 insertions(+), 60 deletions(-) diff --git a/Module/ModCommands/Commands/BanKick.cs b/Module/ModCommands/Commands/BanKick.cs index 1b9de33..73b69c7 100644 --- a/Module/ModCommands/Commands/BanKick.cs +++ b/Module/ModCommands/Commands/BanKick.cs @@ -1,5 +1,4 @@ -using Discord; -using Discord.WebSocket; +using Discord.WebSocket; using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using System; @@ -9,9 +8,9 @@ using System.Threading.Tasks; namespace Noikoio.RegexBot.Module.ModCommands.Commands { + // Ban and kick commands are highly similar in implementation, and thus are handled in a single class. class BanKick : Command { - // Ban and kick commands are highly similar in implementation, and thus are handled in a single class. protected enum CommandMode { Ban, Kick } private readonly CommandMode _mode; @@ -49,6 +48,13 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands if (string.IsNullOrWhiteSpace(val)) _notifyMsg = null; // empty value - disable message else _notifyMsg = val; } + + // Building usage message here + DefaultUsageMsg = $"{this.Trigger} [user or user ID] " + (_forceReason ? "[reason]" : "*[reason]*") + "\n" + + "Removes the given user from this server and prevents the user from rejoining. " + + (_forceReason ? "L" : "Optionally l") + "ogs the reason for the ban to the Audit Log."; + if (_purgeDays > 0) + DefaultUsageMsg += $"\nAdditionally removes the user's post history for the last {_purgeDays} day(s)."; } #region Strings @@ -71,7 +77,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands string reason; if (line.Length < 2) { - await SendUsageMessage(msg, null); + await SendUsageMessageAsync(msg.Channel, null); return; } targetstr = line[1]; @@ -86,7 +92,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands // No reason given if (_forceReason) { - await SendUsageMessage(msg, ReasonRequired); + await SendUsageMessageAsync(msg.Channel, ReasonRequired); return; } reason = null; @@ -112,7 +118,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands } if (qres == null) { - await SendUsageMessage(msg, TargetNotFound); + await SendUsageMessageAsync(msg.Channel, TargetNotFound); return; } ulong targetuid = qres.UserId; @@ -122,7 +128,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands if (_mode == CommandMode.Kick && targetobj == null) { // Can't kick without obtaining the user object - await SendUsageMessage(msg, TargetNotFound); + await SendUsageMessageAsync(msg.Channel, TargetNotFound); return; } @@ -188,22 +194,6 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands return true; } - private async Task SendUsageMessage(SocketMessage m, string message) - { - string desc = $"{this.Trigger} [user or user ID] " + (_forceReason ? "[reason]" : "*[reason]*") + "\n"; - desc += "Removes the given user from this server and prevents the user from rejoining. "; - desc += (_forceReason ? "L" : "Optionally l") + "ogs the reason for the ban to the Audit Log."; - if (_purgeDays > 0) - desc += $"\nAdditionally removes the user's post history for the last {_purgeDays} day(s)."; - - var usageEmbed = new EmbedBuilder() - { - Title = "Usage", - Description = desc - }; - await m.Channel.SendMessageAsync(message ?? "", embed: usageEmbed); - } - private string BuildSuccessMessage(string targetstr) { const string defaultmsgBan = ":white_check_mark: Banned user **$target**."; diff --git a/Module/ModCommands/Commands/Say.cs b/Module/ModCommands/Commands/Say.cs index c2a103e..a5895df 100644 --- a/Module/ModCommands/Commands/Say.cs +++ b/Module/ModCommands/Commands/Say.cs @@ -10,40 +10,36 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands { // No configuration at the moment. // TODO: Whitelist/blacklist - to limit which channels it can "say" into - public Say(CommandListener l, string label, JObject conf) : base(l, label, conf) { } - + public Say(CommandListener l, string label, JObject conf) : base(l, label, conf) { + DefaultUsageMsg = $"{this.Trigger} [channel] [message]\n" + + "Displays the given message exactly as specified to the given channel."; + } + + #region Strings + const string ChannelRequired = ":x: You must specify a channel."; + const string MessageRequired = ":x: You must specify a message."; + const string TargetNotFound = ":x: Unable to find given channel."; + #endregion + public override async Task Invoke(SocketGuild g, SocketMessage msg) { string[] line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries); if (line.Length <= 1) { - await SendUsageMessage(msg, ":x: You must specify a channel."); + await SendUsageMessageAsync(msg.Channel, ChannelRequired); return; } if (line.Length <= 2 || string.IsNullOrWhiteSpace(line[2])) { - await SendUsageMessage(msg, ":x: You must specify a message."); + await SendUsageMessageAsync(msg.Channel, MessageRequired); return; } var ch = GetTextChannelFromString(g, line[1]); - if (ch == null) await SendUsageMessage(msg, ":x: Unable to find given channel."); + if (ch == null) await SendUsageMessageAsync(msg.Channel, TargetNotFound); await ch.SendMessageAsync(line[2]); } - private async Task SendUsageMessage(SocketMessage m, string message) - { - string desc = $"{this.Trigger} [channel] [message]\n"; - desc += "Displays the given message exactly as specified to the given channel."; - - var usageEmbed = new EmbedBuilder() - { - Title = "Usage", - Description = desc - }; - await m.Channel.SendMessageAsync(message ?? "", embed: usageEmbed); - } - private SocketTextChannel GetTextChannelFromString(SocketGuild g, string input) { // Method 1: Check for channel mention diff --git a/Module/ModCommands/Commands/Unban.cs b/Module/ModCommands/Commands/Unban.cs index e38cc86..8ebd5be 100644 --- a/Module/ModCommands/Commands/Unban.cs +++ b/Module/ModCommands/Commands/Unban.cs @@ -1,5 +1,4 @@ -using Discord; -using Discord.WebSocket; +using Discord.WebSocket; using Newtonsoft.Json.Linq; using System; using System.Linq; @@ -13,7 +12,10 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands // No configuration. // TODO bring in some options from BanKick. Particularly custom success msg. // TODO when ModLogs fully implemented, add a reason? - public Unban(CommandListener l, string label, JObject conf) : base(l, label, conf) { } + public Unban(CommandListener l, string label, JObject conf) : base(l, label, conf) { + DefaultUsageMsg = $"{this.Trigger} [user or user ID]\n" + + "Unbans the given user, allowing them to rejoin the server."; + } #region Strings const string FailPrefix = ":x: **Unable to unban:** "; @@ -33,7 +35,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands string targetstr; if (line.Length < 2) { - await SendUsageMessage(msg, null); + await SendUsageMessageAsync(msg.Channel, null); return; } targetstr = line[1]; @@ -59,7 +61,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands if (qres == null) { - await SendUsageMessage(msg, TargetNotFound); + await SendUsageMessageAsync(msg.Channel, TargetNotFound); return; } @@ -90,18 +92,5 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands } } } - - private async Task SendUsageMessage(SocketMessage m, string message) - { - string desc = $"{this.Trigger} [user or user ID]\n"; - desc += "Unbans the given user, allowing them to rejoin the server."; - - var usageEmbed = new EmbedBuilder() - { - Title = "Usage", - Description = desc - }; - await m.Channel.SendMessageAsync(message ?? "", embed: usageEmbed); - } } } diff --git a/Module/ModCommands/Commands/_CommandBase.cs b/Module/ModCommands/Commands/_CommandBase.cs index 3896365..1be47f0 100644 --- a/Module/ModCommands/Commands/_CommandBase.cs +++ b/Module/ModCommands/Commands/_CommandBase.cs @@ -1,4 +1,5 @@ -using Discord.WebSocket; +using Discord; +using Discord.WebSocket; using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using System; @@ -95,5 +96,27 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands protected static readonly Regex ChannelMention = new Regex(@"<#(?\d+)>", RegexOptions.Compiled); protected static readonly Regex EmojiMatch = new Regex(@"<:(?[A-Za-z0-9_]{2,}):(?\d+)>", RegexOptions.Compiled); #endregion + + #region Usage message + protected string DefaultUsageMsg { get; set; } + /// + /// Sends out the default usage message () within an embed. + /// An optional message can be included, for uses such as notifying users of incorrect usage. + /// + /// Target channel for sending the message. + /// The message to send alongside the default usage message. + protected async Task SendUsageMessageAsync(ISocketMessageChannel target, string message = null) + { + if (DefaultUsageMsg == null) + throw new InvalidOperationException("DefaultUsage was not defined."); + + var usageEmbed = new EmbedBuilder() + { + Title = "Usage", + Description = DefaultUsageMsg + }; + await target.SendMessageAsync(message ?? "", embed: usageEmbed); + } + #endregion } }