pythondiscord.py

I can't use my userbot slash commands in private messages


I can use the bot's slash commands on servers, but I can't in private messages. The bot's commands simply don't appear in the selection menu.

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="/", intents=intents)

@bot.event
async def on_ready():
    try:
        await bot.tree.sync()
        print(f"{bot.user.name} online")
    except Exception as e:
        print(f"Sync error: {e}")

@bot.tree.command(name="ping")
async def ping(ctx):
    await ctx.response.send_message("Pong", ephemeral=True)
    
bot.run("TOKEN")

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="ping")
    async def ping(ctx):
    

    @discord.app_commands.allowed_installs

    @discord.app_commands.allowed_contexts