Slash command starter (Python)
Registers /ping and /hello and answers them. A clean base for your own commands.
Python 3.13
♥ 0 likes
Environment variables you'll need
| Key | What it is |
|---|---|
DISCORD_TOKEN | Your bot's token from the Discord Developer Portal. Paste it on the Secrets tab; we store it encrypted. |
GUILD_ID | Optional. Register the commands in this server only, which is instant |
Files
README.md
# Slash command starter
A Python bot built on discord.py. Your bot token is set on the Secrets tab as
DISCORD_TOKEN and reaches the bot as an environment variable.
## Commands
- `/ping` replies with the bot's latency.
- `/hello` greets you.
- Without GUILD_ID the commands are registered for every server, which can take up to an hour to appear.
## Setup
1. No privileged gateway intents are needed. Leave Message Content, Server Members
and Presence off in the Discord Developer Portal.
2. Invite the bot with the `bot` and `applications.commands` scopes and these
permissions: Send Messages.
3. Set these values on the Secrets tab. The ones marked optional can stay empty:
- `GUILD_ID`: Optional. Register the commands in this server only, which is instant.
4. Press Deploy. The console shows a line with "Logged in as" once the bot is online.
main.py
import os
import discord
from discord import app_commands
class StarterBot(discord.Client):
def __init__(self):
super().__init__(intents=discord.Intents.default())
self.tree = app_commands.CommandTree(self)
async def setup_hook(self):
# Registers the commands below with Discord every time the bot starts.
synced = await self.tree.sync()
print(f"Synced {len(synced)} slash commands")
bot = StarterBot()
@bot.tree.command(name="ping", description="Check that the bot is alive")
async def ping(interaction: discord.Interaction):
await interaction.response.send_message(f"Pong! {round(bot.latency * 1000)} ms")
@bot.tree.command(name="hello", description="Say hello")
async def hello(interaction: discord.Interaction):
await interaction.response.send_message(f"Hello, {interaction.user.mention}!")
@bot.tree.error
async def on_app_command_error(interaction, error):
print(f"Command error: {error!r}")
try:
if interaction.response.is_done():
await interaction.followup.send("Something went wrong.", ephemeral=True)
else:
await interaction.response.send_message("Something went wrong.", ephemeral=True)
except discord.HTTPException:
pass
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ready)")
bot.run(os.environ["DISCORD_TOKEN"])
requirements.txt
discord.py