Search code examples
c#discord.net

`EnableInDm(false)` attribute does not disable the commands when applied to a module


Assume I have a module that only contains commands for guilds, like the following:

using Discord;
using Discord.Interactions;
using System.Linq;
using System.Threading.Tasks;

namespace MyDiscordBot;

[EnabledInDm(false)]
public class GuildActionsModule : InteractionModuleBase<SocketInteractionContext>
{
    [SlashCommand("guild-action", "Perform an action in a guild")]
    public async Task GuildActionAsync()
    {
        var guild = Context.Guild;

        await RespondAsync("Performing a very serious action in a guild...");

        await Task.Delay(2000);
        await ModifyOriginalResponseAsync(m => m.Content = "The very serious action was performed!");
    }
}

I have specified the [EnabledInDm(false)] attribute, but when I register the command module using

var entryAssembly = Assembly.GetEntryAssembly();
InteractionService = new InteractionService(RestClient);
InteractionService.AddTypeConverters(entryAssembly);

await InteractionService.AddModulesAsync(entryAssembly, null);

await InteractionService.RegisterCommandsGloballyAsync();

the command still appears in the autocomplete section in my DMs. How do I disable an entire module's commands from appearing in DMs?


Solution

  • The problem is that the module does not properly capture the EnabledInDm attribute. You have to apply the EnabledInDm attribute to every one of your commands.

    Note that this works if your module has a Group attribute, grouping subcommands.

    This seems to be a bug/oversight in Discord.Net's end. If you do encounter issues like this, it's safe to assume that directly applying the attributes of your choice to your slash command method should be more likely to work properly.