Skip to content

What it is

Pycord is a Python library for building Discord bots, providing an easy-to-use interface for interacting with Discord’s API. It allows developers to handle events, send messages, create commands, manage servers, and automate workflows within Discord.

Pycord allows you to define bot commands, listen to events, manage servers and channels, interact with users, and build complex automation for Discord servers. It supports both synchronous and asynchronous programming.

Installation

pip install py-cord

Getting started

The smallest useful thing you can do with it, and what each part means.

Simple bot responding to /hello
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')

@bot.command()
async def hello(ctx):
    await ctx.send('Hello! I am your Pycord bot.')

bot.run('YOUR_BOT_TOKEN')
Creates a bot that responds with a greeting when the user sends the !hello command.
Sending a direct message
user = await bot.fetch_user(USER_ID)
await user.send('Hello! This is a DM.')
Sends a private message to a specific user by their ID.

Advanced usage

Where the library earns its place over a simpler alternative.

Using slash commands
from discord import Option

@bot.slash_command(name='greet', description='Greet someone')
async def greet(ctx, name: Option(str, 'Enter a name')):
    await ctx.respond(f'Hello {name}!')
Defines a slash command /greet with an input option 'name' that responds with a greeting.
Handling reactions
@bot.event
async def on_reaction_add(reaction, user):
    if user != bot.user:
        await reaction.message.channel.send(f'{user.name} reacted with {reaction.emoji}')
Replies in the channel whenever a user reacts to a message.
Embeds and rich messages
from discord import Embed
embed = Embed(title='Sample Embed', description='This is an embed', color=0x00ff00)
await ctx.send(embed=embed)
Sends a rich embedded message with a title, description, and color.
Cog for modular commands
from discord.ext import commands

class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def ping(self, ctx):
        await ctx.send('Pong!')

bot.add_cog(MyCog(bot))
Organizes bot commands into a cog (class) for modularity and cleaner code structure.
Using tasks for periodic events
from discord.ext import tasks

@tasks.loop(minutes=1)
async def periodic_task():
    channel = bot.get_channel(CHANNEL_ID)
    await channel.send('This runs every minute!')

periodic_task.start()
Executes a task every minute and sends a message to a specified channel.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

discord.errors.LoginFailure
Ensure your bot token is correct and valid.
discord.errors.Forbidden
Check that your bot has the necessary permissions to perform the action.
discord.errors.HTTPException
Handle network errors, invalid requests, or rate-limiting issues gracefully.

Best practices

  • Use cogs to organize commands and events for maintainable bot code.
  • Leverage asynchronous functions for better performance with multiple events.
  • Secure your bot token using environment variables.
  • Monitor your bot and handle exceptions to prevent crashes.
  • Follow Discord’s API rate limits to avoid bans.

Background

Why it exists, and what it was reacting to.

Pycord was created as a community-maintained fork of discord.py to continue its development after discord.py became inactive for some time. It focuses on stability, ease of use, and compatibility with modern Python features, making it a popular choice for creating Discord bots.