示例#1
0
        public async Task RunCommand(SocketMessage msg)
        {
            try
            {
                var content = msg.Content.Trim().ToLower();

                if (msg is SocketUserMessage userMsg &&
                    userMsg.Channel is SocketGuildChannel guildChannel && userMsg.Author is SocketGuildUser author && content.StartsWith(_prefix))
                {
                    var isBlacklisted = await _context.BlacklistedChannels.AnyAsync(x =>
                                                                                    x.GuildId == guildChannel.Guild.Id && x.ChannelId == guildChannel.Id);

                    if (!isBlacklisted)
                    {
                        var cmdTxt = content.Split(' ', StringSplitOptions.RemoveEmptyEntries).First();
                        _logger.LogInformation($"User \"{_msgContext.Author.Username}#{_msgContext.Author.DiscriminatorValue}\" is executing command \"{cmdTxt}\" on guild \"{_msgContext.Guild.Name}\"");


                        const StringComparison ignoreCase = StringComparison.InvariantCultureIgnoreCase;

                        var command = _commands.SingleOrDefault(x =>
                                                                cmdTxt.Equals(_prefix + x.Name, ignoreCase) ||
                                                                x.Aliases.Any(y => cmdTxt.Equals(_prefix + y, ignoreCase)));

                        var customCommand = _customCommandRepository.Get().Where(x => x.GuildId == _msgContext.Guild.Id).ToList()
                                            .SingleOrDefault(x => cmdTxt.Equals(_prefix + x.Name, ignoreCase));
                        if (command != null)
                        {
                            //Check if command needs authorization
                            var commandRequiresAuthentication =
                                _commandCache.GetAuthorizedTypes().Any(x => x.IsInstanceOfType(command));
                            if (commandRequiresAuthentication)
                            {
                                await msg.Channel.SendMessageAsync("This is a spooky command");

                                if (await _roleService.UserIsAdmin(userMsg))
                                {
                                    await command.Invoke(userMsg);
                                }
                                else
                                {
                                    await msg.Channel.SendMessageAsync(BotResponses.AccessDenied(author));
                                }
                            }
                            else
                            {
                                await command.Invoke(userMsg);
                            }
                        }
                        else if (customCommand != null)
                        {
                            await msg.Channel.SendMessageAsync(customCommand.Response);
                        }
                        else
                        {
                            throw new ArgumentException("Invalid Command: " + cmdTxt);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error executing command");
                switch (ex)
                {
                case ArgumentException _:
                    _logger.LogWarning(ex.Message);
                    break;

                default:
                    await msg.Channel.SendMessageAsync(BotResponses.Error);

                    break;
                }
            }
        }