private async Task OnMessageCreated(SocketMessage message) { // Ignore messages from the bot or from non user messages (in channel or DMs). if (message.Author.Id == this.client.CurrentUser.Id || !(message is IUserMessage userMessage)) { return; } int argPosition = 0; if (userMessage.HasCharPrefix('!', ref argPosition)) { // Make sure the user isn't banned. Don't block unban, in case of an accidental self-ban if (!userMessage.Content.StartsWith("!unban", StringComparison.InvariantCultureIgnoreCase)) { using (DatabaseAction action = this.dbActionFactory.Create()) { if (await action.GetCommandBannedAsync(message.Author.Id)) { return; } } } ICommandContext context = new CommandContext(this.client, userMessage); await this.commandService.ExecuteAsync(context, argPosition, this.serviceProvider); return; } // Some commands may need to be taken in DM channels. Everything for handling buzzes and scoring should be // on the main channel if (!(userMessage.Channel is ITextChannel channel && this.gameStateManager.TryGet(channel.Id, out GameState state) && userMessage.Author is IGuildUser guildUser)) { return; } ulong botId = this.client.CurrentUser.Id; bool answersScored = await this.messageHandler.TryScore(state, guildUser, channel, botId, message.Content); if (answersScored) { return; } // Don't block on this _ = Task.Run(() => this.messageHandler.HandlePlayerMessage( state, guildUser, channel, botId, message.Content)); }
public async Task UnbanUser() { const ulong bannedUser = 123; this.CreateHandler(out BotOwnerCommandHandler handler, out MessageStore messageStore); using (BotConfigurationContext context = this.botConfigurationfactory.Create()) using (DatabaseAction action = new DatabaseAction(context)) { await action.AddCommandBannedUser(bannedUser); } await handler.UnbanUserAsync(bannedUser); using (BotConfigurationContext context = this.botConfigurationfactory.Create()) using (DatabaseAction action = new DatabaseAction(context)) { bool banned = await action.GetCommandBannedAsync(bannedUser); Assert.IsFalse(banned, "User should be unbanned"); } }