/// <summary> /// Called when a plugin has been unloaded /// </summary> /// <param name="sender"></param> internal void OnPluginUnloaded(Plugin sender) { List <DirectMessageCommand> dmCommands = new List <DirectMessageCommand>(); List <GuildCommand> guildCommands = new List <GuildCommand>(); // Remove all discord commands which were registered by the plugin foreach (DirectMessageCommand cmd in _directMessageCommands.Values) { if (cmd.Plugin.Name == sender.Name) { dmCommands.Add(cmd); } } foreach (GuildCommand cmd in _guildCommands.Values) { if (cmd.Plugin.Name == sender.Name) { guildCommands.Add(cmd); } } for (int index = 0; index < dmCommands.Count; index++) { DirectMessageCommand cmd = dmCommands[index]; RemoveDmCommand(cmd); } for (int index = 0; index < guildCommands.Count; index++) { GuildCommand cmd = guildCommands[index]; RemoveGuildCommand(cmd); } }
private void RemoveGuildCommand(GuildCommand command) { GuildCommand guildCommand = _guildCommands[command.Name]; guildCommand.OnRemoved(); _guildCommands.Remove(command.Name); }
public void RemoveDiscordCommand(string command, Plugin plugin) { DirectMessageCommand dmCommand = _directMessageCommands[command]; if (dmCommand != null && dmCommand.Plugin == plugin) { RemoveDmCommand(dmCommand); } GuildCommand guildCommand = _guildCommands[command]; if (guildCommand != null && guildCommand.Plugin == plugin) { RemoveGuildCommand(guildCommand); } }
/// <summary> /// Adds a discord guild command /// Sourced From Command.cs of OxideMod (https://github.com/OxideMod/Oxide.Rust/blob/develop/src/Libraries/Command.cs#L134) /// </summary> /// <param name="command">Name of the command</param> /// <param name="plugin">Plugin to add the localized command for</param> /// <param name="allowedChannels">Channel or Category Id's this command is allowed in</param> /// <param name="callback">Method name of the callback</param> public void AddGuildCommand(string command, Plugin plugin, List <Snowflake> allowedChannels, Action <DiscordMessage, string, string[]> callback) { string commandName = command.ToLowerInvariant(); if (_guildCommands.TryGetValue(commandName, out GuildCommand cmd)) { string previousPluginName = cmd.Plugin?.Name ?? "an unknown plugin"; string newPluginName = plugin?.Name ?? "An unknown plugin"; DiscordExtension.GlobalLogger.Warning($"{newPluginName} has replaced the '{commandName}' discord guild command previously registered by {previousPluginName}"); } cmd = new GuildCommand(commandName, plugin, allowedChannels, callback); // Add the new command to collections _guildCommands[commandName] = cmd; }
/// <summary> /// Handles the specified direct message command /// Sourced from Command.cs of OxideMod (https://github.com/OxideMod/Oxide.Rust/blob/develop/src/Libraries/Command.cs#L361) /// </summary> /// <param name="client"></param> /// <param name="channel"></param> /// <param name="name"></param> /// <param name="args"></param> /// <param name="message"></param> internal bool HandleGuildCommand(BotClient client, DiscordMessage message, DiscordChannel channel, string name, string[] args) { GuildCommand command = _guildCommands[name]; if (command == null || !command.CanRun(client) || !command.CanHandle(message, channel)) { return(false); } if (!command.Plugin.IsLoaded) { _guildCommands.Remove(name); return(false); } if (!client.IsPluginRegistered(command.Plugin)) { return(false); } command.HandleCommand(message, name, args); return(true); }