예제 #1
0
        private async Task DoCommand(SocketMessage parameterMessage)
        {
            //Load.Messages++;
            if (!(parameterMessage is SocketUserMessage message))
            {
                return;
            }
            var argPos  = 0;
            var context = new SocketCommandContext(_client, message); //new CommandContext(_client, message);

            if (context.User.IsBot)
            {
                return;
            }
            Initialise();

            var guild = GuildConfig.GetServer(context.Guild);

            if (await CheckMessage(message, context, guild))
            {
                return;
            }
            await AutoMessage(context);

            if (message.HasMentionPrefix(_client.CurrentUser, ref argPos) && guild.chatwithmention)
            {
                var mcontent = context.Message.MentionedUsers.Aggregate(message.Content,
                                                                        (current, mentionedUser) => current.Replace(mentionedUser.Mention, mentionedUser.Username));
                mcontent = context.Message.MentionedRoles.Aggregate(mcontent,
                                                                    (current, mentionedRole) => current.Replace(mentionedRole.Mention, mentionedRole.Name));
                mcontent = context.Message.MentionedChannels.Aggregate(mcontent,
                                                                       (current, mentionedChannel) =>
                                                                       current.Replace(((ITextChannel)mentionedChannel).Mention, mentionedChannel.Name));
                try
                {
                    var response = _apiAi.TextRequest(mcontent);
                    if (response.Result.Fulfillment.Speech != "")
                    {
                        await context.Channel.SendMessageAsync(response.Result.Fulfillment.Speech);
                    }
                }
                catch
                {
                    //
                }

                return;
            }

            if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) ||
                  message.HasStringPrefix(Load.Pre, ref argPos) ||
                  message.HasStringPrefix(GuildConfig.GetServer(context.Guild).Prefix, ref argPos)))
            {
                return;
            }

            var HS = Homeserver.Load();

            if (HS.GlobalBans.Any(x => x.ID == context.User.Id))
            {
                return;
            }


            var result = await _commands.ExecuteAsync(context, argPos, Provider);

            var server = context.Channel is IPrivateChannel ? "Direct Message " : context.Guild.Name;

            if (result.IsSuccess)
            {
                await LogHandler.In3($"{context.Message}", 'S', $"{server}", 'U', $"{context.User}"); //if there is no error log normally

                var srch = _commands.Search(context, argPos);
                if (srch.IsSuccess)
                {
                    var name = srch.Commands.Select(x => x.Command.Name).FirstOrDefault();
                    if (name != null)
                    {
                        if (CommandUses.Any(x => string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase)))
                        {
                            var cmd = CommandUses.First(x => string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase));
                            cmd.Uses++;
                        }
                        else
                        {
                            CommandUses.Add(new CMD
                            {
                                Name = name.ToLower(),
                                Uses = 1
                            });
                        }
                    }
                }
            }
            else
            {
                try
                {
                    if (!(result.Error == CommandError.UnknownCommand || result.Error == CommandError.BadArgCount || result.Error == CommandError.UnmetPrecondition))
                    {
                        if (context.Client.GetChannel(HS.Error) is IMessageChannel ErrorChannel)
                        {
                            var embed = new EmbedBuilder
                            {
                                Title       = $"ERROR: {context.Message}",
                                Description = "REASON:\n" +
                                              $"{result.ErrorReason}",
                                Color = Color.Red
                            };
                            embed.WithFooter(x => { x.Text = $"{context.Message.CreatedAt} || {context.Guild.Name}"; });
                            await ErrorChannel.SendMessageAsync("", false, embed.Build());
                        }
                    }
                }
                catch
                {
                    //
                }

                try
                {
                    //if in server error responses are enabled reply on error

                    var desc = "";
                    if (result.Error != CommandError.UnknownCommand)
                    {
                        var srch = _commands.Search(context, argPos);
                        var cmd  = srch.Commands.FirstOrDefault();

                        desc += $"Command: {cmd.Command.Name}\n";
                        if (cmd.Command.Parameters.Any())
                        {
                            desc += $"Parameters: {string.Join(" ", cmd.Command.Parameters.Select(x => x.IsOptional ? $" `<(Optional){x.Name}>` " : $" `<{x.Name}>` "))}\n";
                        }
                        else
                        {
                            desc += "Parameters: N/A\n";
                        }

                        desc += $"Summary: `{cmd.Command?.Summary}`\n" +
                                $"Remarks: `{cmd.Command?.Remarks}`\n";

                        if (cmd.Command.Aliases.Any())
                        {
                            desc += $"Aliases: {string.Join(" ", cmd.Command.Aliases)}\n";
                        }

                        desc += "Error Reason:\n" +
                                $"{result.ErrorReason}";
                    }
                    else
                    {
                        desc += "Command: N/A";
                    }

                    var errmsg = new EmbedBuilder
                    {
                        Title       = $"**Error, {context.User.Username}**",
                        Description = desc
                    };

                    try
                    {
                        await context.Channel.SendMessageAsync("", false, errmsg.Build());
                    }
                    catch
                    {
                        //
                    }

                    await LogHandler.In3Error($"{context.Message}", 'S', $"{context.Guild.Name}", 'E', $"{result.ErrorReason}"); // log errors as arrors
                }