Example #1
0
 public void AddMessageListener(object sender, ulong guildId, IMessageListener messageListener)
 {
     if (!GuildMessageListeners.ContainsKey(guildId))
     {
         GuildMessageListeners.Add(guildId, new List <IMessageListener> {
             messageListener
         });
     }
     else
     {
         GuildMessageListeners[guildId].Add(messageListener);
     }
 }
Example #2
0
        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>());
            }

            var adminCommand   = new Admin(guildId, this);
            var systemCommands = new CommandBase[] { adminCommand, new Help(guildId, this), new About(guildId, this) };

            foreach (var command in systemCommands)
            {
                await command.Startup();
            }

            foreach (var command in systemCommands)
            {
                string term = adminCommand.RenameCommandTermFromDatabase(command);
                GuildCommands[guildId].Add(term, command);
                Log(this, new LogEventArgs {
                    Type    = LogType.Log,
                    Message = $"Successfully loaded internal module {command.Name} ({term}) for guild {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())
                        {
                            commandInstance.Term = adminCommand.RenameCommandTermFromDatabase(commandInstance);
                            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}, 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()}"
            });
        }