Search code examples
pythondiscorddiscord.py

How to sync slash command to Group Chat or User DM?


How to make my Discord Bot sync to Group Chat or User DM? There is a Discord Bot able to use the commands without inviting to Discord Server, and it works in Discord Group Chat and User DM's. Is there a way to make my Slash Command sync to my own Group Chat?

Here is my Code:

@bot.event
async def on_ready():
    await bot.tree.sync()

@bot.tree.command(name="randomness number",description="used to giveaway thing")
async def qr(interaction:discord.Interaction,amount:int):
    await interaction.response.send_message(f"result is: {random.randint(1,amount)}")

And the Slash Command only works on invited Discord Server but not on my Group Chat or User DM. I already authorized my bot to my Discord Account, and it still doesn't show Slash Command.

Slash Command that works in Group Chat, Not my Discord Bot.png


Solution

  • In discord.py 2.4+ you can make slash commands be usable in DMs and Group DMs by:

    1. Allowing to install the command into the user profile using @discord.app_commands.allowed_installs decorator, and
    2. Allowing the command to be used in contexts of DMs and GDMs (latter are called private_channels) using @discord.app_commands.allowed_contexts decorator

    This example allows a command to be installed and used everywhere:

    @discord.app_commands.allowed_installs(guilds=True, users=True)
    @discord.app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
    @bot.tree.command(name="randomness number", description="used to giveaway thing")
    async def qr(interaction: discord.Interaction, amount: int):
    

    @discord.app_commands.allowed_installs

    @discord.app_commands.allowed_contexts