using RegexBot.Common; using System.Diagnostics; namespace RegexBot; /// /// Base class for a RegexBot module. A module implements a user-facing feature and is expected to directly handle /// user input (both by means of configuration and incoming Discord events) and process it accordingly. /// /// /// Implementing classes should not rely on local variables to store runtime or state data for guilds. /// Instead, use and . ///

/// Additionally, do not assume that is available during the constructor. ///
public abstract class RegexbotModule(RegexbotClient bot) { /// /// Retrieves the bot instance. /// public RegexbotClient Bot { get; } = bot; /// /// Retrieves the Discord client instance. /// public DiscordSocketClient DiscordClient { get => Bot.DiscordClient; } /// /// Gets the name of this module. /// /// If not overridden, this defaults to the class's name. public virtual string Name => GetType().Name; /// /// Called when a guild becomes available during initial load or configuration reload. /// The implementing class should construct an instance to hold data specific to the corresponding guild for use during runtime. /// /// Corresponding guild ID for the state data being used. May be useful when reloading. /// JSON token holding module configuration specific to this guild. /// /// An object instance containing state and/or configuration information for the guild currently being processed. /// public abstract Task CreateGuildStateAsync(ulong guildID, JToken? config); /// /// Retrieves the state object that corresponds with the given guild. /// /// The state instance's type. /// The guild ID for which to retrieve the state object. /// The state instance cast in the given type, or Default(T) if none exists. /// /// Thrown if the instance cannot be cast as specified. /// [DebuggerStepThrough] protected T? GetGuildState(ulong guildId) => Bot._svcGuildState.DoGetStateObj(guildId, GetType()); /// /// Returns the list of moderators defined in the current guild configuration. /// /// /// An with corresponding moderator configuration data. /// In case none exists, an empty list will be returned. /// protected EntityList GetModerators(ulong guild) => Bot._svcGuildState.DoGetModlist(guild); /// /// Emits a log message to the bot console that is associated with the specified guild. /// /// The guld for which this log message is associated with. /// The log message to send. Multi-line messages are acceptable. protected void Log(SocketGuild guild, string? message) { var gname = guild.Name ?? $"Guild ID {guild.Id}"; Bot._svcLogging.DoLog($"{gname}] [{Name}", message); } /// /// Emits a log message to the bot console and, optionally, the logging webhook. /// /// The log message to send. Multi-line messages are acceptable. protected void Log(string message) => Bot._svcLogging.DoLog(Name, message); }