Ejemplo n.º 1
0
 public void RemoveMessageListener(object sender, ulong guildId, IMessageListener messageListener)
 {
     if (GuildMessageListeners.ContainsKey(guildId))
     {
         GuildMessageListeners[guildId].Remove(messageListener);
     }
     if (messageListener is IDisposable disposable)
     {
         disposable.Dispose();
     }
 }
Ejemplo n.º 2
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);
     }
 }
Ejemplo n.º 3
0
        public async Task RemoveCommand(object o, ulong guildId, string classTerm)
        {
            var command = GuildCommands[guildId][classTerm];
            int messageListenerCount  = 0;
            int reactionListenerCount = 0;

            if (GuildMessageListeners.ContainsKey(guildId))
            {
                foreach (var messageListener in GuildMessageListeners[guildId].Where(ml => ml.Command == command).ToList())
                {
                    RemoveMessageListener(o, guildId, messageListener);
                    ++messageListenerCount;
                }
            }
            if (MessageReactionListeners.ContainsKey(guildId))
            {
                foreach (var messageId in MessageReactionListeners[guildId].Keys.ToList())
                {
                    foreach (var reactionListener in MessageReactionListeners[guildId][messageId].Where(rl => rl.Command == command).ToList())
                    {
                        RemoveReactionListener(o, guildId, messageId, reactionListener);
                        ++reactionListenerCount;
                    }
                }
            }
            GuildCommands[guildId].Remove(classTerm);
            if (command is IDisposable disposable)
            {
                disposable.Dispose();
            }
            Log(this, new LogEventArgs {
                Type    = LogType.Log,
                Message = $"Successfully unloaded module {command.GetType().FullName}, {messageListenerCount} message listener{(messageListenerCount == 1 ? string.Empty : "s")}, and {reactionListenerCount} reaction listener{(reactionListenerCount == 1 ? string.Empty : "s")}"
            });
            await Task.Delay(0);
        }
Ejemplo n.º 4
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()}"
            });
        }