/// <summary> /// Returns and instance of a command given the guild ID given the type. /// Returns null if the command could not be found. /// </summary> /// <param name="sender"></param> /// <param name="guildId"></param> /// <returns></returns> public T GetCommand <T>(object sender, ulong guildId) where T : CommandBase { if (GuildCommands.ContainsKey(guildId)) { return(GuildCommands[guildId].First(c => c.Value is T).Value as T); } return(null); }
/// <summary> /// Returns and instance of a command given the guild ID that it belongs to and the term used to invoke it. /// Returns null if the command could not be found. /// </summary> /// <param name="guildId"></param> /// <param name="commandTerm"></param> /// <returns></returns> public CommandBase GetCommand(object _, ulong guildId, string commandTerm) { if (GuildCommands.ContainsKey(guildId)) { if (GuildCommands[guildId].ContainsKey(commandTerm)) { return(GuildCommands[guildId][commandTerm]); } } return(null); }
public List <CommandBase> GetCommands(object _, ulong guildId) { if (GuildCommands.ContainsKey(guildId)) { return(GuildCommands[guildId].Values.ToList()); } else { return(new List <CommandBase>()); } }
/// <summary> /// Returns and instance of a command given the guild ID that it belongs to and the term used to invoke it. /// Returns null if the command could not be found. /// If the command cannot be found, the unknown command prefix is applied and then checked again. /// E.g. if "help" is the commandTerm, and "?" is the unknown command prefix, "?help" will also be looked for. /// This is specifically only if "help" is not already an existing command though. /// </summary> /// <param name="guildId"></param> /// <param name="commandTerm"></param> /// <returns></returns> public CommandBase GetCommand(object _, ulong guildId, string commandTerm) { if (GuildCommands.ContainsKey(guildId)) { if (GuildCommands[guildId].ContainsKey(commandTerm)) { return(GuildCommands[guildId][commandTerm]); } else { var adminCommand = GetCommand <Admin>(this, guildId); if (GuildCommands[guildId].ContainsKey($"{adminCommand.UnknownCommandPrefix}{commandTerm}")) { return(GuildCommands[guildId][$"{adminCommand.UnknownCommandPrefix}{commandTerm}"]); } } } return(null); }
private async Task InstantiateCommandsForGuild(ulong guildId) { if (GuildCommands.ContainsKey(guildId)) { return; } else { GuildCommands.Add(guildId, new Dictionary <string, CommandBase>()); } if (GuildMessageListeners.ContainsKey(guildId)) { GuildMessageListeners[guildId].Clear(); } else { GuildMessageListeners.Add(guildId, new List <IMessageListener>()); } foreach (var commandType in SystemCommands.Values) { var commandInstance = Activator.CreateInstance(commandType, new object[] { guildId, this }) as CommandBase; GuildCommands[guildId].Add(commandInstance.Term, commandInstance); await commandInstance.Startup(); Log(this, new LogEventArgs { Type = LogType.Log, Message = $"Successfully loaded internal module {commandInstance.Name} ({commandInstance.Term}) for guild {guildId}" }); } var adminCommand = GetCommand <Admin>(this, guildId); foreach (var commandType in LoadedCommands.Values) { if (!adminCommand.IsCommandNameDisabled(commandType.FullName)) { try { var commandInstance = Activator.CreateInstance(commandType, new object[] { guildId, this }) as CommandBase; if (await commandInstance.Startup()) { GuildCommands[guildId].Add(commandInstance.Term, commandInstance); Log(this, new LogEventArgs { Type = LogType.Log, Message = $"Successfully loaded external module {commandInstance.Name} ({commandInstance.Term}) for guild {guildId}" }); } else { Log(this, new LogEventArgs { Type = LogType.Error, Message = $"Could not load module {commandInstance.Name} ({commandInstance.Term}), startup failed" }); } } catch (Exception exc) { Log(this, new LogEventArgs { Type = LogType.Error, Message = $"Could not load module {commandType.FullName}, instantiation failed and exception thrown\n{exc}" }); } } else { Log(this, new LogEventArgs { Type = LogType.Log, Message = $"Module {commandType.FullName} is disabled and will not be instatiated" }); } } Log(this, new LogEventArgs { Type = LogType.Log, Message = $"All modules have finished loading for guild {guildId.ToString()}" }); }