Exemple #1
0
        public async Task ProcessCommandAsync(SocketMessage msg)
        {
            var message = msg as SocketUserMessage;

            if (message == null)
            {
                return;
            }

            int argPos  = 0;
            var context = new SocketCommandContext(Client, message);
            List <DiscordCustomPrefix> guildPrefixes = await PrefixService.GetGuildPrefixes(context.Guild);

            string FinalPrefix = Config.Prefix;

            foreach (DiscordCustomPrefix prefix in guildPrefixes)
            {
                argPos = 0;
                if (message.HasStringPrefix(prefix.Prefix, ref argPos))
                {
                    FinalPrefix = prefix.Prefix;
                    break;
                }
                argPos = 0;
            }
            if (message.HasStringPrefix(FinalPrefix, ref argPos))
            {
                string commandExecuted = message.Content.Split(' ')[0].Replace(FinalPrefix, "");
                if (!Provider.GetService <CooldownService>().IsCooldownExpired(context.Guild.Id, commandExecuted))
                {
                    CommandInfo cmdInfo       = Commands.FindInfoByName(commandExecuted);
                    Embed       cooldownEmbed = Provider.GetService <EmbedService>().MakeFailFeedbackEmbed($"The command `{cmdInfo.Name}` cannot be used for another {Math.Max(Provider.GetService<CooldownService>().GetRemainingTime(context.Guild.Id, cmdInfo), 1)} seconds!");
                    await context.Channel.SendMessageAsync(cooldownEmbed, lifeTime : Provider.GetService <Config>().FeedbackMessageLifeTime);

                    return;
                }
                var result = await Commands.ExecuteAsync(context, argPos, Provider);

                if (result.IsSuccess)
                {
                    CommandInfo cmdInfo = Commands.FindInfoByName(commandExecuted);
                    if (cmdInfo != null)
                    {
                        foreach (var attr in cmdInfo.Preconditions)
                        {
                            if (attr.GetType() == typeof(RequireCooldownAttribute))
                            {
                                RequireCooldownAttribute cooldownAttr = attr as RequireCooldownAttribute;
                                Provider.GetService <CooldownService>().SetCooldown(context.Guild.Id, cmdInfo, cooldownAttr.Seconds);
                            }
                        }

                        using (BotDBContext DBContext = Provider.GetService <DBContextFactory>().Create <BotDBContext>())
                        {
                            string arguments = message.Content.Replace($"{FinalPrefix}{commandExecuted} ", "").Replace($"{FinalPrefix}{commandExecuted}", "");
                            DBContext.CommandLogs.Add(new DiscordCommandLog(message.Author.Id.ToString(), cmdInfo.Name, arguments, context.Channel.Id.ToString(), context.Guild.Id.ToString(), DateTime.Now));
                            await DBContext.SaveChangesAsync();
                        }
                    }
                }
                else
                {
                    ConsoleEx.WriteColoredLine(LogSeverity.Error, ConsoleTextFormat.TimeAndText, ConsoleColor.Red, $"[Command Error - {result.Error?.ToString()}] ", ConsoleColor.White, result.ErrorReason?.ToString());
                    int          lifeTime     = Provider.GetService <Config>().FeedbackMessageLifeTime;
                    EmbedService embedService = Provider.GetService <EmbedService>();
                    switch (result.Error?.ToString())
                    {
                    case "UnmetPrecondition":
                        Embed unmetEmbed = embedService.MakeFailFeedbackEmbed(result.ErrorReason);
                        await context.Channel.SendMessageAsync("", embed : unmetEmbed, lifeTime : lifeTime);

                        break;

                    case "UnknownCommand":
                        string err = "";
                        err = result.ErrorReason?.ToString();
                        bool badArgs = (err.Contains("This input does not match any overload."));
                        if (badArgs)
                        {
                            await TriggerBadArgEmbed(commandExecuted, context, FinalPrefix);

                            return;
                        }
                        List <FuzzyMatch> matches = Commands.FuzzySearch(commandExecuted);
                        if (matches.Count > 0)
                        {
                            string builtString = "";
                            foreach (FuzzyMatch match in matches)
                            {
                                builtString += $"{match.TextValue.Bold()}, ";
                            }

                            builtString = builtString.Substring(0, builtString.Length - 2);
                            Embed suggestEmbed = embedService.MakeFeedbackEmbed($"That command wasn't found.\nDid you mean {builtString}?");
                            await context.Channel.SendMessageAsync("", embed : suggestEmbed);
                        }
                        break;

                    case "BadArgCount":
                        await TriggerBadArgEmbed(commandExecuted, context, FinalPrefix);

                        break;

                    case "ParseFailed":
                        Embed badParseEmbed = embedService.MakeFailFeedbackEmbed("Failed to parse one of the arguments for that command.");
                        await context.Channel.SendMessageAsync("", embed : badParseEmbed, lifeTime : lifeTime);

                        break;

                    case "MultipleMatches":
                        Embed multiMatchEmbed = embedService.MakeFailFeedbackEmbed("Multiple matches were found.");
                        await context.Channel.SendMessageAsync("", embed : multiMatchEmbed, lifeTime : lifeTime);

                        break;

                    case "ObjectNotFound":
                        Embed notFoundEmbed = embedService.MakeFailFeedbackEmbed("One of your specified objects could not be found.");
                        await context.Channel.SendMessageAsync("", embed : notFoundEmbed, lifeTime : lifeTime);

                        break;
                    }
                }
            }
            else if (message.HasMentionPrefix(Provider.GetService <DiscordSocketClient>().CurrentUser, ref argPos))
            {
                using (var typingState = message.Channel.EnterTypingState())
                {
                    string query = message.Content.Replace(Provider.GetService <DiscordSocketClient>().CurrentUser.Mention + " ", "");
                    string resp  = await CleverbotAPI.AskAsync(query);

                    await message.Channel.SendMessageAsync(resp);

                    typingState.Dispose();
                }
            }
        }