pythondiscord.py

Hiding Grouped Slash Commands in DMs


How can I hide grouped slash commands in DMs? I provided a little python code sample below, with a normal slash command (bot.tree) and a grouped slash command (class TestGroup). @discord.app_commands.guild_only() works perfectly fine with the hidden-test command but not the other one, I've tried many approaches to the situation and nothing seems to work for me.

import discord
from discord.ext import commands


# Setup
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents=intents)

# Simple login confirmation
@bot.event
async def on_ready():
    await bot.tree.sync()
    print(f"{bot.user} is online!")

# @discord.app_commands.guild_only() hides the command if the user try's to use it in DMs.
@bot.tree.command(name="hidden-test", description="This command can only be used in guilds!")
@discord.app_commands.guild_only()
async def hidden_command(interaction: discord.Interaction):
    await interaction.response.send_message("This command can only be used in guilds!", ephemeral=True)

# This is a discord slash command group, I tried testing "@discord.app_commands.guild_only()" here, it doesn't work.
class TestGroup(discord.app_commands.Group):
    def __init__(self):
        super().__init__(name="hidden", description="These command can only be used in guilds!")

    @discord.app_commands.command(name="true", description="This command can only be used in guilds!")
    @discord.app_commands.guild_only()
    async def true_command(self, interaction: discord.Interaction):
        await interaction.response.send_message("This command can only be used in guilds! (not really :d)", ephemeral=True)

bot.tree.add_command(TestGroup())

# Replace with your bot token, if you're going to test it.
bot.run("YOUR-TOKEN")

Tried using the app_commands decorator any many more things.

Result: Slash command showing up in my DM with the bot.

Expected Result: Slash command not showing up in my DMs with the bot, just like the "hidden-test" command.


Solution

  • You were using the decorator @discord.app_commands.guild_only() on group made using subclass method, however it applies only on individual commands-group or GroupCog method, instead use guild_only=True in super().__init__() for your method of making group,not the decorator. That's the only change I have made

    import discord
    from discord import app_commands
    from discord.ext import commands
    
    # Setup
    intents = discord.Intents.all()
    bot = commands.Bot(command_prefix="!", intents=intents)
    
    # Simple login confirmation
    @bot.event
    async def on_ready():
        await bot.tree.sync()
        print(f"{bot.user} is online!")
    
    # @discord.app_commands.guild_only() hides the command if the user try's to use it in DMs.
    @bot.tree.command(name="hidden-test", description="This command can only be used in guilds!")
    @discord.app_commands.guild_only()
    async def hidden_command(interaction: discord.Interaction):
        await interaction.response.send_message("This command can only be used in guilds!", ephemeral=True)
    
    # This is a discord slash command group, I tried testing "@discord.app_commands.guild_only()" here, it doesn't work.
    class TestGroup(discord.app_commands.Group):
        def __init__(self):
            super().__init__(name="hidden", description="These command can only be used in guilds!",guild_only=True)
    
        @discord.app_commands.command(name="true", description="This command can only be used in guilds!")
        @discord.app_commands.guild_only()
        async def true_command(self, interaction: discord.Interaction):
            await interaction.response.send_message("This command can only be used in guilds! (not really :d)", ephemeral=True)
    
    bot.tree.add_command(TestGroup())
    
    # Replace with your bot token, if you're going to test it.
    bot.run("token")
    

    References: