コード例 #1
0
 public BotOwnerCommandHandler(
     ICommandContext context, IOptionsMonitor <BotConfiguration> options, IDatabaseActionFactory dbActionFactory)
 {
     this.Context = context;
     this.DatabaseActionFactory = dbActionFactory;
     this.Options = options;
 }
コード例 #2
0
        private void CreateHandler(
            IOptionsMonitor <BotConfiguration> options,
            IFileScoresheetGenerator scoresheetGenerator,
            out ReaderCommandHandler handler,
            out GameState game,
            out MessageStore messageStore)
        {
            messageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                userId: DefaultReaderId,
                updateMockGuild: UpdateMockGuild,
                out _);
            GameStateManager manager = new GameStateManager();

            manager.TryCreate(DefaultChannelId, out game);
            game.TeamManager = new ByCommandTeamManager();
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);

            handler = new ReaderCommandHandler(commandContext, manager, options, dbActionFactory, scoresheetGenerator);
        }
コード例 #3
0
 public GeneralCommands(
     GameStateManager manager,
     IOptionsMonitor <BotConfiguration> options,
     IDatabaseActionFactory dbActionFactory)
 {
     this.DatabaseActionFactory = dbActionFactory;
     this.Manager = manager;
     this.Options = options;
 }
コード例 #4
0
 public AdminCommandHandler(
     ICommandContext context,
     IDatabaseActionFactory dbActionFactory,
     IGoogleSheetsGeneratorFactory googleSheetsGeneratorFactory)
 {
     this.Context = context;
     this.DatabaseActionFactory        = dbActionFactory;
     this.GoogleSheetsGeneratorFactory = googleSheetsGeneratorFactory;
 }
コード例 #5
0
 public GeneralCommandHandler(
     ICommandContext context,
     GameStateManager manager,
     IOptionsMonitor <BotConfiguration> options,
     IDatabaseActionFactory dbActionFactory)
 {
     this.Context = context;
     this.DatabaseActionFactory = dbActionFactory;
     this.Manager = manager;
     this.Options = options;
 }
コード例 #6
0
 public ReaderCommands(
     GameStateManager manager,
     IOptionsMonitor <BotConfiguration> options,
     IDatabaseActionFactory dbActionFactory,
     IFileScoresheetGenerator scoresheetGenerator)
 {
     this.Manager = manager;
     this.Options = options;
     this.DatabaseActionFactory = dbActionFactory;
     this.ScoresheetGenerator   = scoresheetGenerator;
 }
コード例 #7
0
        public Bot(IOptionsMonitor <BotConfiguration> options, IHubContext <MonitorHub> hubContext)
        {
            this.gameStateManager  = new GameStateManager();
            this.options           = options ?? throw new ArgumentNullException(nameof(options));
            this.dbActionFactory   = new SqliteDatabaseActionFactory(this.options.CurrentValue.DatabaseDataSource);
            this.readerRejoinedMap = new Dictionary <IGuildUser, bool>();

            DiscordSocketConfig clientConfig = new DiscordSocketConfig()
            {
                // May not be needed
                MessageCacheSize = 1024 * 16
            };

            this.client = new DiscordSocketClient(clientConfig);
            IServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(this.client);
            serviceCollection.AddSingleton(this.gameStateManager);
            serviceCollection.AddSingleton(this.options);
            serviceCollection.AddSingleton(this.dbActionFactory);
            serviceCollection.AddSingleton <IFileScoresheetGenerator>(new ExcelFileScoresheetGenerator());

            IGoogleSheetsApi googleSheetsApi = new GoogleSheetsApi(this.options);

            serviceCollection.AddSingleton <IGoogleSheetsGeneratorFactory>(
                new GoogleSheetsGeneratorFactory(googleSheetsApi));

            this.serviceProvider = serviceCollection.BuildServiceProvider();

            this.commandService = new CommandService(new CommandServiceConfig()
            {
                CaseSensitiveCommands = false,
                LogLevel       = LogSeverity.Info,
                DefaultRunMode = RunMode.Async,
            });
            this.commandService.Log += this.OnLogAsync;

            this.logger = Log.ForContext(this.GetType());
            this.discordNetEventLogger = new DiscordNetEventLogger(this.client, this.commandService);

            Task.WaitAll(this.commandService.AddModulesAsync(Assembly.GetExecutingAssembly(), this.serviceProvider));

            this.messageHandler = new MessageHandler(
                this.options, this.dbActionFactory, hubContext, this.logger);

            this.client.MessageReceived    += this.OnMessageCreated;
            this.client.GuildMemberUpdated += this.OnPresenceUpdated;
            this.client.JoinedGuild        += this.OnGuildJoined;

            this.configurationChangeCallback = this.options.OnChange((configuration, value) =>
            {
                this.logger.Information("Configuration has been reloaded");
            });
        }
コード例 #8
0
        private void InitializeHandler(
            ulong voiceChannelId    = 9999,
            string voiceChannelName = "Voice",
            IGoogleSheetsGeneratorFactory googleSheetsGeneratorFactory = null)
        {
            this.MessageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                this.MessageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                DefaultReaderId,
                (mockGuild, textChannel) =>
            {
                Mock <IVoiceChannel> mockVoiceChannel = new Mock <IVoiceChannel>();
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Id).Returns(voiceChannelId);
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Name).Returns(voiceChannelName);
                mockGuild
                .Setup(guild => guild.GetVoiceChannelAsync(It.IsAny <ulong>(), It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult(mockVoiceChannel.Object));

                List <IVoiceChannel> voiceChannels = new List <IVoiceChannel>()
                {
                    mockVoiceChannel.Object
                };
                mockGuild
                .Setup(guild => guild.GetVoiceChannelsAsync(It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult <IReadOnlyCollection <IVoiceChannel> >(voiceChannels));
                mockGuild
                .Setup(guild => guild.Roles)
                .Returns(DefaultRoles.Select((role, index) =>
                {
                    Mock <IRole> mockRole = new Mock <IRole>();
                    mockRole
                    .Setup(r => r.Name)
                    .Returns(role);
                    mockRole
                    .Setup(r => r.Id)
                    .Returns((ulong)index);
                    return(mockRole.Object);
                }).ToArray());
            },
                out IGuildTextChannel guildTextChannel);

            this.GuildTextChannel = guildTextChannel;
            IOptionsMonitor <BotConfiguration> options         = CommandMocks.CreateConfigurationOptionsMonitor();
            IDatabaseActionFactory             dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);

            this.GoogleSheetsGeneratorFactory = googleSheetsGeneratorFactory ?? (new Mock <IGoogleSheetsGeneratorFactory>()).Object;

            this.Handler = new AdminCommandHandler(commandContext, dbActionFactory, this.GoogleSheetsGeneratorFactory);
        }
        private void CreateHandler(out BotOwnerCommandHandler handler, out MessageStore messageStore)
        {
            messageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                DefaultReaderId);
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);
            IOptionsMonitor <BotConfiguration> options = CommandMocks.CreateConfigurationOptionsMonitor();

            handler = new BotOwnerCommandHandler(commandContext, options, dbActionFactory);
        }
コード例 #10
0
 public ReaderCommandHandler(
     ICommandContext context,
     GameStateManager manager,
     IOptionsMonitor <BotConfiguration> options,
     IDatabaseActionFactory dbActionFactory,
     IFileScoresheetGenerator scoresheetGenerator,
     IGoogleSheetsGeneratorFactory googleSheetsGeneratorFactory)
 {
     this.Context = context;
     this.Manager = manager;
     this.Options = options;
     this.DatabaseActionFactory        = dbActionFactory;
     this.ScoresheetGenerator          = scoresheetGenerator;
     this.GoogleSheetsGeneratorFactory = googleSheetsGeneratorFactory;
 }
コード例 #11
0
        private void CreateHandler(
            out MessageHandler handler,
            out GameState state,
            out IGuildUser playerUser,
            out IGuildUser readerUser,
            out IGuildTextChannel channel,
            out MessageStore messageStore)
        {
            messageStore = new MessageStore();
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);
            IOptionsMonitor <BotConfiguration> options = CommandMocks.CreateConfigurationOptionsMonitor();

            handler = new MessageHandler(
                options, dbActionFactory, CommandMocks.CreateHubContext(), new Mock <ILogger>().Object);

            playerUser = CommandMocks.CreateGuildUser(DefaultPlayerId);
            readerUser = CommandMocks.CreateGuildUser(DefaultReaderId);
            CommandMocks.CreateGuild(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                (mockGuild, textChannel) =>
            {
                Mock <IVoiceChannel> mockVoiceChannel = new Mock <IVoiceChannel>();
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Id).Returns(DefaultVoiceChannelId);
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Name).Returns("Voice");
                mockGuild
                .Setup(guild => guild.GetVoiceChannelAsync(It.IsAny <ulong>(), It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult(mockVoiceChannel.Object));

                List <IVoiceChannel> voiceChannels = new List <IVoiceChannel>()
                {
                    mockVoiceChannel.Object
                };
                mockGuild
                .Setup(guild => guild.GetVoiceChannelsAsync(It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult <IReadOnlyCollection <IVoiceChannel> >(voiceChannels));
            },
                null,
                out channel);
            state = new GameState()
            {
                ReaderId    = DefaultReaderId,
                TeamManager = new ByCommandTeamManager()
            };
        }
コード例 #12
0
        public MessageHandler(
            IOptionsMonitor <BotConfiguration> options,
            IDatabaseActionFactory dbActionFactory,
            IHubContext <MonitorHub> hubContext,
            ILogger logger)
        {
            Verify.IsNotNull(options, nameof(options));

            this.Options = options;
            this.DatabaseActionFactory = dbActionFactory;
            this.HubContext            = hubContext;
            this.Logger = logger;

            // TODO: Rewrite this so that
            // #1: buzz emojis are server-dependent, since emojis are
            // #2: This can be updated if the config file is refreshed.
            // Alternatively, this should be a guild-dependent setting
            this.BuzzEmojisRegex = BuildBuzzEmojiRegexes(options.CurrentValue);
        }
コード例 #13
0
        public async Task ClearAllRemovesGame()
        {
            GameStateManager manager = new GameStateManager();

            manager.TryCreate(DefaultChannelId, out GameState currentGame);
            MessageStore    messageStore   = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore, DefaultIds, DefaultGuildId, DefaultChannelId, DefaultReaderId);
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);
            IOptionsMonitor <BotConfiguration> options             = CommandMocks.CreateConfigurationOptionsMonitor();
            IFileScoresheetGenerator           scoresheetGenerator = (new Mock <IFileScoresheetGenerator>()).Object;

            ReaderCommandHandler handler = new ReaderCommandHandler(
                commandContext, manager, options, dbActionFactory, scoresheetGenerator);

            await handler.ClearAllAsync();

            Assert.IsFalse(
                manager.TryGet(DefaultChannelId, out _),
                "Game should have been removed from the manager.");
            Assert.AreEqual(1, messageStore.ChannelMessages.Count, "Unexpected number of messages sent.");
        }
コード例 #14
0
        private void CreateHandler(
            ulong voiceChannelId,
            string voiceChannelName,
            out AdminCommandHandler handler,
            out MessageStore messageStore,
            out IGuildTextChannel guildTextChannel)
        {
            messageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                DefaultReaderId,
                (mockGuild, textChannel) =>
            {
                Mock <IVoiceChannel> mockVoiceChannel = new Mock <IVoiceChannel>();
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Id).Returns(voiceChannelId);
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Name).Returns(voiceChannelName);
                mockGuild
                .Setup(guild => guild.GetVoiceChannelAsync(It.IsAny <ulong>(), It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult(mockVoiceChannel.Object));

                List <IVoiceChannel> voiceChannels = new List <IVoiceChannel>()
                {
                    mockVoiceChannel.Object
                };
                mockGuild
                .Setup(guild => guild.GetVoiceChannelsAsync(It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult <IReadOnlyCollection <IVoiceChannel> >(voiceChannels));
            },
                out guildTextChannel);
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);

            handler = new AdminCommandHandler(commandContext, dbActionFactory);
        }
コード例 #15
0
 public AdminCommands(IDatabaseActionFactory dbActionFactory)
 {
     this.DatabaseActionFactory = dbActionFactory;
 }
コード例 #16
0
 public BotOwnerCommands(IOptionsMonitor <BotConfiguration> options, IDatabaseActionFactory dbActionFactory)
 {
     this.Options = options;
     this.DatabaseActionFactory = dbActionFactory;
 }
コード例 #17
0
 public AdminCommands(
     IDatabaseActionFactory dbActionFactory, IGoogleSheetsGeneratorFactory googleSheetsGeneratorFactory)
 {
     this.DatabaseActionFactory        = dbActionFactory;
     this.GoogleSheetsGeneratorFactory = googleSheetsGeneratorFactory;
 }
コード例 #18
0
 public AdminCommandHandler(ICommandContext context, IDatabaseActionFactory dbActionFactory)
 {
     this.Context = context;
     this.DatabaseActionFactory = dbActionFactory;
 }