private async Task ProcessCommandAsync(SocketMessage arg)
        {
            // Source filter
            if (arg.Source != MessageSource.User)
            {
                return;
            }
            var message = (SocketUserMessage)arg;

            // Get the prefix
            string pfx;

            try
            {
                pfx = await GetPrefix(arg);
            }
            catch (Exception e)
            {
                string src = message.Channel is IGuildChannel gc ? $"{gc.Guild.Name} ({gc.Guild.Id})" : $"{message.Channel.Name}";
                Logger.GetLogger("Commands").Warning($"Failed to get prefix. {src}", e);
                return;
            }

            // Command check
            int argPos = 0;

            if (message.HasStringPrefix(pfx, ref argPos))
            {
                // Refresh the cached prefix
                if (message.Channel is IGuildChannel c)
                {
                    pm.RestoreCache(c.GuildId).Release();
                }
                var context = new SocketCommandContext(client, message);
                // Log message for debugging (doesn't check if the command exists)
                Logger.GetLogger("Commands").Debug($"Executing command: {GetExecutionInfo(context)}");
                // Execute command
                await commands.ExecuteAsync(context, argPos, services);
            }
            else if (message.Content.TrimEnd() == $"{pm.DefaultPrefix}prefix")
            {
                // Info
                var context = new SocketCommandContext(client, message);
                Logger.GetLogger("Commands").Debug($"Executing prefix get with default prefix: {GetExecutionInfo(context)}");
                var ts = await TranslationManager.CreateFor(context.Channel);

                message.Channel.SendMessageAsync(embed: EmbedFactory.CreateSuccess()
                                                 .WithTitle(ts.GetMessage("commands/prefix:embed_title"))
                                                 .WithDescription(MessageFormatter.Format(ts.GetMessage("commands/prefix:my_prefix_is"),
                                                                                          PrefixManagerService.PrettyPrefix(pfx)))
                                                 .Build()).Release();
            }
        }
示例#2
0
        public async Task ResetPrefix()
        {
            // Prepare reply
            var ts = await TranslationManager.CreateFor(Context.Channel);

            var eb = EmbedFactory.CreateSuccess()
                     .WithTitle(ts.GetMessage("commands/prefix:embed_title"));

            var messageSend = ReplyAsync(embed: eb.Build());

            await pm.CachePrefix(Context.Guild.Id, pm.DefaultPrefix);

            await pm.SetPrefixAsync(Context.Guild.Id, pm.DefaultPrefix);

            await messageSend;
        }
示例#3
0
        public async Task Prefix([Summary("commands/prefix:parameter_prefix")][Remainder] string prefix = null)
        {
            // Prepare reply
            EmbedBuilder eb;
            var          reply = new StringBuilder();
            var          ts    = await TranslationManager.CreateFor(Context.Channel);

            // Get the prefix
            if (prefix is null)
            {
                // Get prefix
                var prefixGet = pm.GetPrefixAsync(Context.Guild?.Id);

                // Prepare message
                eb = EmbedFactory.CreateSuccess()
                     .WithTitle(ts.GetMessage("commands/prefix:embed_title"));

                // Send a message
                reply.AppendLine(MessageFormatter.Format(ts.GetMessage("commands/prefix:my_prefix_is"), PrefixManagerService.PrettyPrefix(await prefixGet)));
                if (Context.IsPrivate)
                {
                    reply.AppendLine(ts.GetMessage("commands/prefix:error_private_messages"));
                }
                eb.Description = reply.ToString();
                await ReplyAsync(embed : eb.Build());

                // Return!
                return;
            }

            // Check if in guild
            if (Context.Guild is null)
            {
                // No
                eb = EmbedFactory.CreateError()
                     .WithTitle(ts.GetMessage("errors:permission_denied"));
                reply.AppendLine(ts.GetMessage("commands/prefix:error_private_messages"));
            }
            else
            {
                // In guild
                string currentPrefix = await pm.GetPrefixAsync(Context.Guild.Id);

                if (currentPrefix == prefix)
                {
                    eb = EmbedFactory.CreateError()
                         .WithTitle(ts.GetMessage("errors:permission_denied"));
                    reply.AppendLine(ts.GetMessage("commands/prefix:error_same_prefix"));
                }
                else
                {
                    if (!Regex.IsMatch(prefix, @"[A-Za-zÅÖÄåöä0-9!?+\/%$#]*"))
                    {
                        eb = EmbedFactory.CreateError()
                             .WithTitle(ts.GetMessage("commands/prefix:embed_title"))
                             .WithDescription("commands/prefix:error_invalid_prefix");
                        await ReplyAsync(embed : eb.Build());

                        return;
                    }

                    // Update the prefix cache
                    await pm.CachePrefix(Context.Guild.Id, prefix);

                    // Send the info message here for seemingly better performance. The prefix is cached in Redis so it will still work.
                    eb = EmbedFactory.CreateSuccess()
                         .WithTitle(ts.GetMessage("commands/prefix:embed_title"));
                    reply.AppendLine(MessageFormatter.Format(ts.GetMessage("commands/prefix:prefix_set_message"),
                                                             PrefixManagerService.PrettyPrefix(prefix)));

                    // Send message
                    eb.Description = reply.ToString();
                    ReplyAsync(embed: eb.Build()).Release();

                    try
                    {
                        // Set prefix in the actual database.
                        await pm.SetPrefixAsync(Context.Guild.Id, prefix);
                    }
                    catch (Exception e)
                    {
                        // Log the error so that it can be debugged
                        var log = Logger.GetLogger(this);
                        log.Warning($"Failed to store prefix for guild {Context.Guild.Name} ({Context.Guild.Id}): pfx: {prefix} Exception:", e);
                    }
                    return;
                }
            }

            // Send message
            eb.Description = reply.ToString();
            await ReplyAsync(embed : eb.Build());
        }