using Newtonsoft.Json.Linq; using System; using System.Threading.Tasks; namespace Kerobot { /// /// Base class for a Kerobot 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/instance variables to store data. Make use of /// and . /// public abstract class ModuleBase { private readonly Kerobot _kb; /// /// Retrieves the Kerobot instance. /// public Kerobot Kerobot => _kb; private ModuleBase(Kerobot kb) => _kb = kb; /// /// Gets the module name. /// This value is derived from the class's name. It is used in configuration and logging. /// public string Name => GetType().Name; /// /// Called when a guild becomes available. The implementing class should construct an object to hold /// data specific to the corresponding guild for use during runtime. /// /// JSON token holding module configuration specific to this guild. /// /// An object containing state and/or configuration information for the guild currently being processed. /// public abstract Task CreateGuildStateAsync(JToken config); /// /// Retrieves the state object that corresponds with the given guild. /// /// The state object's type. /// The guild ID for which to retrieve the state object. /// The state object cast in the given type, or Default(T) if none exists. /// /// Thrown if the stored state object cannot be cast as specified. /// protected T GetGuildState(ulong guildId) => Kerobot.GetGuildState(guildId, GetType()); } /// /// Represents errors that occur when a module attempts to create a new guild state object. /// public class ModuleLoadException : Exception { /// /// Initializes this exception class with the specified error message. /// /// public ModuleLoadException(string message) : base(message) { } } }