Moved (created) SendUsageMessageAsync to base class

This commit is contained in:
Noikoio 2018-03-09 23:16:30 -08:00
parent bfb699d62f
commit 99fe2967b6
4 changed files with 58 additions and 60 deletions

View file

@ -1,5 +1,4 @@
using Discord; using Discord.WebSocket;
using Discord.WebSocket;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem; using Noikoio.RegexBot.ConfigItem;
using System; using System;
@ -9,9 +8,9 @@ using System.Threading.Tasks;
namespace Noikoio.RegexBot.Module.ModCommands.Commands 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 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 } protected enum CommandMode { Ban, Kick }
private readonly CommandMode _mode; private readonly CommandMode _mode;
@ -49,6 +48,13 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
if (string.IsNullOrWhiteSpace(val)) _notifyMsg = null; // empty value - disable message if (string.IsNullOrWhiteSpace(val)) _notifyMsg = null; // empty value - disable message
else _notifyMsg = val; 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 #region Strings
@ -71,7 +77,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
string reason; string reason;
if (line.Length < 2) if (line.Length < 2)
{ {
await SendUsageMessage(msg, null); await SendUsageMessageAsync(msg.Channel, null);
return; return;
} }
targetstr = line[1]; targetstr = line[1];
@ -86,7 +92,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
// No reason given // No reason given
if (_forceReason) if (_forceReason)
{ {
await SendUsageMessage(msg, ReasonRequired); await SendUsageMessageAsync(msg.Channel, ReasonRequired);
return; return;
} }
reason = null; reason = null;
@ -112,7 +118,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
} }
if (qres == null) if (qres == null)
{ {
await SendUsageMessage(msg, TargetNotFound); await SendUsageMessageAsync(msg.Channel, TargetNotFound);
return; return;
} }
ulong targetuid = qres.UserId; ulong targetuid = qres.UserId;
@ -122,7 +128,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
if (_mode == CommandMode.Kick && targetobj == null) if (_mode == CommandMode.Kick && targetobj == null)
{ {
// Can't kick without obtaining the user object // Can't kick without obtaining the user object
await SendUsageMessage(msg, TargetNotFound); await SendUsageMessageAsync(msg.Channel, TargetNotFound);
return; return;
} }
@ -188,22 +194,6 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
return true; 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) private string BuildSuccessMessage(string targetstr)
{ {
const string defaultmsgBan = ":white_check_mark: Banned user **$target**."; const string defaultmsgBan = ":white_check_mark: Banned user **$target**.";

View file

@ -10,40 +10,36 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
{ {
// No configuration at the moment. // No configuration at the moment.
// TODO: Whitelist/blacklist - to limit which channels it can "say" into // 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) public override async Task Invoke(SocketGuild g, SocketMessage msg)
{ {
string[] line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries); string[] line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
if (line.Length <= 1) if (line.Length <= 1)
{ {
await SendUsageMessage(msg, ":x: You must specify a channel."); await SendUsageMessageAsync(msg.Channel, ChannelRequired);
return; return;
} }
if (line.Length <= 2 || string.IsNullOrWhiteSpace(line[2])) if (line.Length <= 2 || string.IsNullOrWhiteSpace(line[2]))
{ {
await SendUsageMessage(msg, ":x: You must specify a message."); await SendUsageMessageAsync(msg.Channel, MessageRequired);
return; return;
} }
var ch = GetTextChannelFromString(g, line[1]); 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]); 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) private SocketTextChannel GetTextChannelFromString(SocketGuild g, string input)
{ {
// Method 1: Check for channel mention // Method 1: Check for channel mention

View file

@ -1,5 +1,4 @@
using Discord; using Discord.WebSocket;
using Discord.WebSocket;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.Linq; using System.Linq;
@ -13,7 +12,10 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
// No configuration. // No configuration.
// TODO bring in some options from BanKick. Particularly custom success msg. // TODO bring in some options from BanKick. Particularly custom success msg.
// TODO when ModLogs fully implemented, add a reason? // 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 #region Strings
const string FailPrefix = ":x: **Unable to unban:** "; const string FailPrefix = ":x: **Unable to unban:** ";
@ -33,7 +35,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
string targetstr; string targetstr;
if (line.Length < 2) if (line.Length < 2)
{ {
await SendUsageMessage(msg, null); await SendUsageMessageAsync(msg.Channel, null);
return; return;
} }
targetstr = line[1]; targetstr = line[1];
@ -59,7 +61,7 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
if (qres == null) if (qres == null)
{ {
await SendUsageMessage(msg, TargetNotFound); await SendUsageMessageAsync(msg.Channel, TargetNotFound);
return; 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);
}
} }
} }

View file

@ -1,4 +1,5 @@
using Discord.WebSocket; using Discord;
using Discord.WebSocket;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem; using Noikoio.RegexBot.ConfigItem;
using System; using System;
@ -95,5 +96,27 @@ namespace Noikoio.RegexBot.Module.ModCommands.Commands
protected static readonly Regex ChannelMention = new Regex(@"<#(?<snowflake>\d+)>", RegexOptions.Compiled); protected static readonly Regex ChannelMention = new Regex(@"<#(?<snowflake>\d+)>", RegexOptions.Compiled);
protected static readonly Regex EmojiMatch = new Regex(@"<:(?<name>[A-Za-z0-9_]{2,}):(?<ID>\d+)>", RegexOptions.Compiled); protected static readonly Regex EmojiMatch = new Regex(@"<:(?<name>[A-Za-z0-9_]{2,}):(?<ID>\d+)>", RegexOptions.Compiled);
#endregion #endregion
#region Usage message
protected string DefaultUsageMsg { get; set; }
/// <summary>
/// Sends out the default usage message (<see cref="DefaultUsageMsg"/>) within an embed.
/// An optional message can be included, for uses such as notifying users of incorrect usage.
/// </summary>
/// <param name="target">Target channel for sending the message.</param>
/// <param name="message">The message to send alongside the default usage message.</param>
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
} }
} }