2017-08-06 20:05:44 +00:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
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-11-12 03:12:24 +00:00
|
|
|
|
/// ModTools module object.
|
|
|
|
|
/// Implements moderation commands that are individually defined and enabled in configuration.
|
2017-08-06 20:05:44 +00:00
|
|
|
|
/// </summary>
|
2017-10-09 18:54:12 +00:00
|
|
|
|
// We are not using Discord.Net's Commands extension, as it does not allow for changes during runtime.
|
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)
|
|
|
|
|
{
|
|
|
|
|
// Disregard if not in a guild
|
|
|
|
|
SocketGuild g = (arg.Author as SocketGuildUser)?.Guild;
|
|
|
|
|
if (g == null) return;
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
// Check for and invoke command...
|
|
|
|
|
string cmdchk;
|
|
|
|
|
int spc = arg.Content.IndexOf(' ');
|
|
|
|
|
if (spc != -1) cmdchk = arg.Content.Substring(0, spc);
|
|
|
|
|
else cmdchk = arg.Content;
|
|
|
|
|
if (((IDictionary<string, CommandBase>)GetConfig(g.Id)).TryGetValue(cmdchk, out var c))
|
|
|
|
|
{
|
|
|
|
|
// ...on the thread pool.
|
|
|
|
|
await Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ConfigSection("modtools")]
|
|
|
|
|
public override async Task<object> ProcessConfiguration(JToken configSection)
|
|
|
|
|
{
|
2017-10-09 18:54:12 +00:00
|
|
|
|
var commands = new Dictionary<string, CommandBase>(StringComparer.OrdinalIgnoreCase);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
|
2017-10-09 18:54:12 +00:00
|
|
|
|
foreach (var def in configSection.Children<JProperty>())
|
|
|
|
|
{
|
|
|
|
|
string label = def.Name;
|
|
|
|
|
var cmd = CommandBase.CreateInstance(this, def);
|
|
|
|
|
if (commands.ContainsKey(cmd.Command))
|
2017-08-06 20:05:44 +00:00
|
|
|
|
throw new RuleImportException(
|
|
|
|
|
$"{label}: 'command' value must not be equal to that of another definition. " +
|
2017-10-09 18:54:12 +00:00
|
|
|
|
$"Given value is being used for {commands[cmd.Command].Label}.");
|
2017-08-06 20:05:44 +00:00
|
|
|
|
|
2017-10-09 18:54:12 +00:00
|
|
|
|
commands.Add(cmd.Command, cmd);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
2017-10-09 18:54:12 +00:00
|
|
|
|
await Log($"Loaded {commands.Count} command definition(s).");
|
|
|
|
|
return new ReadOnlyDictionary<string, CommandBase>(commands);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new Task Log(string text) => base.Log(text);
|
|
|
|
|
}
|
|
|
|
|
}
|