2017-12-03 06:14:00 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-11-12 03:12:24 +00:00
|
|
|
|
namespace Noikoio.RegexBot.Module.ModTools
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-12-03 06:14:00 +00:00
|
|
|
|
/// ModTools module.
|
|
|
|
|
/// This class manages reading configuration and creating instances based on it.
|
2017-08-06 20:05:44 +00:00
|
|
|
|
/// </summary>
|
2017-11-12 03:12:24 +00:00
|
|
|
|
class ModTools : BotModule
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
|
|
|
|
public override string Name => "ModTools";
|
|
|
|
|
|
2017-10-09 18:54:12 +00:00
|
|
|
|
public ModTools(DiscordSocketClient client) : base(client)
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
|
|
|
|
client.MessageReceived += Client_MessageReceived;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task Client_MessageReceived(SocketMessage arg)
|
|
|
|
|
{
|
2017-12-10 19:11:37 +00:00
|
|
|
|
// Always ignore bots
|
2017-12-08 18:38:40 +00:00
|
|
|
|
if (arg.Author.IsBot) return;
|
|
|
|
|
|
2017-12-19 06:05:37 +00:00
|
|
|
|
if (arg.Channel is IGuildChannel) await CommandCheckInvoke(arg);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
2017-12-08 21:57:35 +00:00
|
|
|
|
|
2017-12-10 19:42:34 +00:00
|
|
|
|
[ConfigSection("ModTools")]
|
2017-08-06 20:05:44 +00:00
|
|
|
|
public override async Task<object> ProcessConfiguration(JToken configSection)
|
|
|
|
|
{
|
2017-12-08 21:57:35 +00:00
|
|
|
|
// Constructor throws exception on config errors
|
|
|
|
|
var conf = new ConfigItem(this, configSection);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
|
2017-12-08 21:57:35 +00:00
|
|
|
|
// Log results
|
|
|
|
|
if (conf.Commands.Count > 0)
|
|
|
|
|
await Log(conf.Commands.Count + " command definition(s) loaded.");
|
2017-12-08 18:30:41 +00:00
|
|
|
|
|
2017-12-08 21:57:35 +00:00
|
|
|
|
return conf;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-08 21:57:35 +00:00
|
|
|
|
private new ConfigItem GetConfig(ulong guildId) => (ConfigItem)base.GetConfig(guildId);
|
2017-12-05 22:23:01 +00:00
|
|
|
|
|
2017-08-06 20:05:44 +00:00
|
|
|
|
public new Task Log(string text) => base.Log(text);
|
2017-12-03 06:14:00 +00:00
|
|
|
|
|
|
|
|
|
private async Task CommandCheckInvoke(SocketMessage arg)
|
|
|
|
|
{
|
2017-12-05 22:23:01 +00:00
|
|
|
|
SocketGuild g = ((SocketGuildUser)arg.Author).Guild;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
|
|
|
|
|
// Get guild config
|
|
|
|
|
ServerConfig sc = RegexBot.Config.Servers.FirstOrDefault(s => s.Id == g.Id);
|
|
|
|
|
if (sc == null) return;
|
|
|
|
|
|
|
|
|
|
// Disregard if not a bot moderator
|
2017-08-08 19:48:30 +00:00
|
|
|
|
if (!sc.Moderators.ExistsInList(arg)) return;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
|
|
|
|
|
// Disregard if the message contains a newline character
|
|
|
|
|
if (arg.Content.Contains("\n")) return;
|
|
|
|
|
|
2017-12-03 06:14:00 +00:00
|
|
|
|
// Check for and invoke command
|
2017-08-06 20:05:44 +00:00
|
|
|
|
string cmdchk;
|
|
|
|
|
int spc = arg.Content.IndexOf(' ');
|
|
|
|
|
if (spc != -1) cmdchk = arg.Content.Substring(0, spc);
|
|
|
|
|
else cmdchk = arg.Content;
|
2017-12-08 21:57:35 +00:00
|
|
|
|
if (GetConfig(g.Id).Commands.TryGetValue(cmdchk, out var c))
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
2017-12-03 06:14:00 +00:00
|
|
|
|
try
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
2017-12-03 06:14:00 +00:00
|
|
|
|
await Log($"'{c.Label}' invoked by {arg.Author.ToString()} in {g.Name}/#{arg.Channel.Name}");
|
|
|
|
|
await c.Invoke(g, arg);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await Log($"Encountered an error for the command '{c.Label}'. Details follow:");
|
|
|
|
|
await Log(ex.ToString());
|
|
|
|
|
}
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|