From 0cbe074151cd49370e9ef8adfa20c407eaeff179 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Sun, 15 Oct 2017 17:22:21 -0700 Subject: [PATCH] Added 'say' command type --- Feature/ModTools/CommandBase.cs | 3 +- Feature/ModTools/Commands/Say.cs | 75 ++++++++++++++++++++++++++++++++ RegexBot.csproj | 2 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 Feature/ModTools/Commands/Say.cs diff --git a/Feature/ModTools/CommandBase.cs b/Feature/ModTools/CommandBase.cs index c496277..2f8e720 100644 --- a/Feature/ModTools/CommandBase.cs +++ b/Feature/ModTools/CommandBase.cs @@ -42,7 +42,8 @@ namespace Noikoio.RegexBot.Feature.ModTools { // Define all command types and their corresponding Types here { "ban", typeof(Commands.Ban) }, - { "kick", typeof(Commands.Kick) } + { "kick", typeof(Commands.Kick) }, + { "say", typeof(Commands.Say) } }); public static CommandBase CreateInstance(ModTools root, JProperty def) diff --git a/Feature/ModTools/Commands/Say.cs b/Feature/ModTools/Commands/Say.cs new file mode 100644 index 0000000..8138891 --- /dev/null +++ b/Feature/ModTools/Commands/Say.cs @@ -0,0 +1,75 @@ +using Discord; +using Discord.WebSocket; +using Newtonsoft.Json.Linq; +using System; +using System.Threading.Tasks; + +namespace Noikoio.RegexBot.Feature.ModTools.Commands +{ + class Say : CommandBase + { + public Say(ModTools l, string label, JObject conf) : base(l, label, conf) { } + + // TODO: Whitelist/blacklist - to limit which channels it can "say" into + + 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."); + return; + } + if (line.Length <= 2 || string.IsNullOrWhiteSpace(line[2])) + { + await SendUsageMessage(msg, ":x: You must specify a message."); + return; + } + + var ch = GetTextChannelFromString(g, line[1]); + if (ch == null) await SendUsageMessage(msg, ":x: Unable to find given channel."); + await ch.SendMessageAsync(line[2]); + } + + private async Task SendUsageMessage(SocketMessage m, string message) + { + string desc = $"{this.Command} [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 + // Note: SocketGuild.GetTextChannel(ulong) returns null if no match. + var m = ChannelMention.Match(input); + if (m.Success) + { + ulong channelId = ulong.Parse(m.Groups["snowflake"].Value); + return g.GetTextChannel(channelId); + } + + // Method 2: Check if specified in string, scan manually + if (input.StartsWith('#')) + { + input = input.Substring(1); + if (input.Length <= 0) return null; + foreach (var c in g.Channels) + { + if (string.Equals(c.Name, input, StringComparison.InvariantCultureIgnoreCase)) + { + return c as SocketTextChannel; + } + } + } + + return null; + } + } +} diff --git a/RegexBot.csproj b/RegexBot.csproj index 82b8819..b89ca07 100644 --- a/RegexBot.csproj +++ b/RegexBot.csproj @@ -4,7 +4,7 @@ Exe netcoreapp2.0 Noikoio.RegexBot - 2.1.0.0 + 2.1.1.0 Highly configurable Discord moderation bot Noikoio