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")
In discord.py 2.4+ you can make slash commands be usable in DMs and Group DMs by:
@discord.app_commands.allowed_installs
decorator, andprivate_channels
) using @discord.app_commands.allowed_contexts
decoratorThis 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):