Pycord

2.7.2 · active · verified Thu Apr 16

Pycord is a modern, easy-to-use, and feature-rich Python wrapper for the Discord API. It enables developers to build Discord bots with support for slash commands, UI components, voice functionality, and more. The library is actively maintained with frequent minor updates and occasional major version releases to align with Discord API changes and introduce new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Pycord bot that comes online and responds to a slash command '/hello'. It highlights the use of `discord.Bot`, `Intents`, and retrieving the bot token from an environment variable for security. Remember to enable the 'Message Content' privileged intent in your bot's Discord Developer Portal settings for `intents.message_content = True` to function correctly.

import discord
import os

# Enable necessary intents. message_content is privileged for text commands.
intents = discord.Intents.default()
intents.message_content = True # Required for reading message content in commands or on_message events

bot = discord.Bot(intents=intents)

@bot.event
async def on_ready():
    print(f"{bot.user} is ready and online!")

@bot.slash_command(name="hello", description="Say hello to the bot")
async def hello(ctx: discord.ApplicationContext, name: str = None):
    """Says hello to the user or a specified name."""
    name = name or ctx.author.name
    await ctx.respond(f"Hello {name}!")

# Run the bot with the token from environment variables
# Make sure to set a 'TOKEN' environment variable with your bot's token
bot.run(os.environ.get('TOKEN'))

view raw JSON →