Ejemplo n.º 1
0
        public override async Task BeforeExecutionAsync(CommandContext ctx)
        {
            var disabledCmds = DisabledCommandsProvider.Get();

            if (disabledCmds.Contains(ctx.Command.QualifiedName) && !disabledCmds.Contains("*"))
            {
                await ctx.RespondAsync(embed : new DiscordEmbedBuilder {
                    Color = Config.Colors.Maintenance, Description = "Command is currently disabled"
                }).ConfigureAwait(false);

                throw new DSharpPlus.CommandsNext.Exceptions.ChecksFailedException(ctx.Command, ctx, new CheckBaseAttribute[] { new RequiresDm() });
            }

            await base.BeforeExecutionAsync(ctx).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        public override async Task BeforeExecutionAsync(CommandContext ctx)
        {
            executionStart = DateTimeOffset.UtcNow;
            try
            {
                if (ctx.Prefix == Config.AutoRemoveCommandPrefix && ModProvider.IsMod(ctx.User.Id))
                {
                    DeletedMessagesMonitor.RemovedByBotCache.Set(ctx.Message.Id, true, DeletedMessagesMonitor.CacheRetainTime);
                    await ctx.Message.DeleteAsync().ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                Config.Log.Warn(e, "Failed to delete command message with the autodelete command prefix");
            }

            if (ctx.Channel.Name == "media" && ctx.Command.QualifiedName != "warn" && ctx.Command.QualifiedName != "report")
            {
                Config.Log.Info($"Ignoring command from {ctx.User.Username} (<@{ctx.User.Id}>) in #media: {ctx.Message.Content}");
                if (ctx.Member is DiscordMember member)
                {
                    var dm = await member.CreateDmChannelAsync().ConfigureAwait(false);

                    await dm.SendMessageAsync($"Only `{Config.CommandPrefix}warn` and `{Config.CommandPrefix}report` are allowed in {ctx.Channel.Mention}").ConfigureAwait(false);
                }
                Config.TelemetryClient?.TrackRequest(ctx.Command.QualifiedName, executionStart, DateTimeOffset.UtcNow - executionStart, HttpStatusCode.Forbidden.ToString(), true);
                throw new DSharpPlus.CommandsNext.Exceptions.ChecksFailedException(ctx.Command, ctx, new CheckBaseAttribute[] { new RequiresNotMedia() });
            }

            var disabledCmds = DisabledCommandsProvider.Get();

            if (disabledCmds.Contains(ctx.Command.QualifiedName) && !disabledCmds.Contains("*"))
            {
                await ctx.RespondAsync(embed : new DiscordEmbedBuilder {
                    Color = Config.Colors.Maintenance, Description = "Command is currently disabled"
                }).ConfigureAwait(false);

                Config.TelemetryClient?.TrackRequest(ctx.Command.QualifiedName, executionStart, DateTimeOffset.UtcNow - executionStart, HttpStatusCode.Locked.ToString(), true);
                throw new DSharpPlus.CommandsNext.Exceptions.ChecksFailedException(ctx.Command, ctx, new CheckBaseAttribute[] { new RequiresDm() });
            }

            if (TriggersTyping(ctx))
            {
                await ctx.ReactWithAsync(Config.Reactions.PleaseWait).ConfigureAwait(false);
            }

            await base.BeforeExecutionAsync(ctx).ConfigureAwait(false);
        }
Ejemplo n.º 3
0
        private static void EnableSubcommands(CommandContext ctx, Command cmd)
        {
            if (cmd.QualifiedName.StartsWith(ctx.Command.Parent.QualifiedName))
            {
                return;
            }

            DisabledCommandsProvider.Enable(cmd.QualifiedName);
            if (cmd is CommandGroup group)
            {
                foreach (var subCmd in group.Children)
                {
                    EnableSubcommands(ctx, subCmd);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task List(CommandContext ctx)
        {
            var list = DisabledCommandsProvider.Get();

            if (list.Count > 0)
            {
                var result = new StringBuilder("Currently disabled commands:").AppendLine().AppendLine("```");
                foreach (var cmd in list)
                {
                    result.AppendLine(cmd);
                }
                await ctx.SendAutosplitMessageAsync(result.Append("```")).ConfigureAwait(false);
            }
            else
            {
                await ctx.RespondAsync("All commands are enabled").ConfigureAwait(false);
            }
        }
Ejemplo n.º 5
0
        public async Task Disable(CommandContext ctx, [RemainingText, Description("Fully qualified command to disable, e.g. `explain add` or `sudo mod *`")] string?command)
        {
            command ??= "";
            var isPrefix = command.EndsWith('*');

            if (isPrefix)
            {
                command = command.TrimEnd('*', ' ');
            }

            if (string.IsNullOrEmpty(command) && !isPrefix)
            {
                await ctx.ReactWithAsync(Config.Reactions.Failure, "You need to specify the command").ConfigureAwait(false);

                return;
            }

            if (command.StartsWith(ctx.Command.Parent.QualifiedName))
            {
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Cannot disable command management commands").ConfigureAwait(false);

                return;
            }

            var cmd = GetCommand(ctx, command);

            if (isPrefix)
            {
                if (cmd == null && !string.IsNullOrEmpty(command))
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, $"Unknown group `{command}`").ConfigureAwait(false);

                    return;
                }

                try
                {
                    if (cmd == null)
                    {
                        foreach (var c in ctx.CommandsNext.RegisteredCommands.Values)
                        {
                            DisableSubcommands(ctx, c);
                        }
                    }
                    else
                    {
                        DisableSubcommands(ctx, cmd);
                    }
                    if (ctx.Command.Parent.QualifiedName.StartsWith(command))
                    {
                        await ctx.RespondAsync("Some subcommands cannot be disabled").ConfigureAwait(false);
                    }
                    else
                    {
                        await ctx.ReactWithAsync(Config.Reactions.Success, $"Disabled `{command}` and all subcommands").ConfigureAwait(false);
                    }
                    await List(ctx).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Config.Log.Error(e);
                    await ctx.RespondAsync("Error while disabling the group").ConfigureAwait(false);
                }
            }
            else
            {
                if (cmd == null)
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, $"Unknown command `{command}`").ConfigureAwait(false);

                    return;
                }

                command = cmd.QualifiedName;
                DisabledCommandsProvider.Disable(command);
                await ctx.ReactWithAsync(Config.Reactions.Success, $"Disabled `{command}`").ConfigureAwait(false);
            }
        }
Ejemplo n.º 6
0
        public async Task Enable(CommandContext ctx, [RemainingText, Description("Fully qualified command to enable, e.g. `explain add` or `sudo mod *`")] string?command)
        {
            if (command == "*")
            {
                DisabledCommandsProvider.Clear();
                await ctx.ReactWithAsync(Config.Reactions.Success, "Enabled all the commands").ConfigureAwait(false);

                return;
            }

            command ??= "";
            var isPrefix = command.EndsWith('*');

            if (isPrefix)
            {
                command = command.TrimEnd('*', ' ');
            }

            if (string.IsNullOrEmpty(command))
            {
                await ctx.ReactWithAsync(Config.Reactions.Failure, "You need to specify the command").ConfigureAwait(false);

                return;
            }

            var cmd = GetCommand(ctx, command);

            if (isPrefix)
            {
                if (cmd == null)
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, $"Unknown group `{command}`").ConfigureAwait(false);

                    return;
                }

                try
                {
                    EnableSubcommands(ctx, cmd);
                    await ctx.ReactWithAsync(Config.Reactions.Success, $"Enabled `{command}` and all subcommands").ConfigureAwait(false);
                    await List(ctx).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Config.Log.Error(e);
                    await ctx.RespondAsync("Error while enabling the group").ConfigureAwait(false);
                }
            }
            else
            {
                if (cmd == null)
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, $"Unknown command `{command}`").ConfigureAwait(false);

                    return;
                }

                command = cmd.QualifiedName;
                DisabledCommandsProvider.Enable(command);
                await ctx.ReactWithAsync(Config.Reactions.Success, $"Enabled `{command}`").ConfigureAwait(false);
            }
        }