2017-07-26 22:36:59 +00:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Noikoio.RegexBot
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-11-12 03:12:24 +00:00
|
|
|
|
/// Base class for bot modules
|
2017-07-26 22:36:59 +00:00
|
|
|
|
/// </summary>
|
2017-11-12 03:12:24 +00:00
|
|
|
|
abstract class BotModule
|
2017-07-26 22:36:59 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly DiscordSocketClient _client;
|
|
|
|
|
private readonly AsyncLogger _logger;
|
|
|
|
|
|
|
|
|
|
public abstract string Name { get; }
|
2017-08-26 17:24:37 +00:00
|
|
|
|
protected DiscordSocketClient Client => _client;
|
2017-07-26 22:36:59 +00:00
|
|
|
|
|
2017-11-12 03:12:24 +00:00
|
|
|
|
public BotModule(DiscordSocketClient client)
|
2017-07-26 22:36:59 +00:00
|
|
|
|
{
|
|
|
|
|
_client = client;
|
|
|
|
|
_logger = Logger.GetLogger(this.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-11-12 03:12:24 +00:00
|
|
|
|
/// Processes module-specific configuration.
|
2017-07-26 22:36:59 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
2017-11-12 03:12:24 +00:00
|
|
|
|
/// Module code <i>should not</i> hold on to this data, but instead use <see cref="GetConfig(ulong)"/> to retrieve
|
2017-07-26 22:36:59 +00:00
|
|
|
|
/// them. This is in the event that configuration is reverted to an earlier state and allows for the
|
2017-11-12 03:12:24 +00:00
|
|
|
|
/// all modules to revert to previously used configuration values with no effort on the part of the
|
|
|
|
|
/// module code itself.
|
2017-07-26 22:36:59 +00:00
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// Processed configuration data prepared for later use.
|
|
|
|
|
/// </returns>
|
|
|
|
|
/// <exception cref="ConfigItem.RuleImportException">
|
|
|
|
|
/// This method should throw RuleImportException in the event of any error.
|
|
|
|
|
/// The exception message will be properly logged.
|
|
|
|
|
/// </exception>
|
|
|
|
|
public abstract Task<object> ProcessConfiguration(JToken configSection);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-11-12 03:12:24 +00:00
|
|
|
|
/// Gets this module's relevant configuration data associated with the given Discord guild.
|
2017-07-26 22:36:59 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// The stored configuration data, or null if none exists.
|
|
|
|
|
/// </returns>
|
|
|
|
|
protected object GetConfig(ulong guildId)
|
|
|
|
|
{
|
|
|
|
|
var sc = RegexBot.Config.Servers.FirstOrDefault(g => g.Id == guildId);
|
|
|
|
|
if (sc == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("There is no known configuration associated with the given Guild ID.");
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 03:12:24 +00:00
|
|
|
|
if (sc.ModuleConfigs.TryGetValue(this, out var item)) return item;
|
2017-07-26 22:36:59 +00:00
|
|
|
|
else return null;
|
|
|
|
|
}
|
2017-08-26 17:24:37 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if the given message author or channel is in the server configuration's moderator list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected bool IsModerator(ulong guildId, SocketMessage m)
|
|
|
|
|
{
|
|
|
|
|
var sc = RegexBot.Config.Servers.FirstOrDefault(g => g.Id == guildId);
|
|
|
|
|
if (sc == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("There is no known configuration associated with the given Guild ID.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sc.Moderators.ExistsInList(m);
|
|
|
|
|
}
|
2017-07-26 22:36:59 +00:00
|
|
|
|
|
|
|
|
|
protected async Task Log(string text)
|
|
|
|
|
{
|
|
|
|
|
await _logger(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed override bool Equals(object obj) => base.Equals(obj);
|
|
|
|
|
public sealed override int GetHashCode() => base.GetHashCode();
|
|
|
|
|
public sealed override string ToString() => base.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates which section under an individual Discord guild configuration should be passed to the
|
2017-11-12 03:12:24 +00:00
|
|
|
|
/// module's <see cref="BotModule.ProcessConfiguration(JToken)"/> method during configuration load.
|
2017-07-26 22:36:59 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
|
|
|
|
public class ConfigSectionAttribute : Attribute
|
|
|
|
|
{
|
|
|
|
|
private readonly string _sectionName;
|
|
|
|
|
|
|
|
|
|
public string SectionName => _sectionName;
|
|
|
|
|
|
|
|
|
|
public ConfigSectionAttribute(string sectionName)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(sectionName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Configuration section name cannot be blank.");
|
|
|
|
|
}
|
|
|
|
|
_sectionName = sectionName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|