Pycord Documentation

repository·master·Indexed 25 days ago

https://github.com/pycord-development/pycord

A modern, async-ready Python API wrapper for Discord. Pycord provides a feature-rich interface for building bots, with comprehensive support for Slash Commands, User Commands, Message Context Menu commands, and Voice. It includes tools for managing ApplicationContext, implementing autocomplete suggestions, handling long-running commands via deferral, and configuring command permissions and cooldowns.

Tokens
54.2K
Snippets
74
Records
415
Agent score
84%

What's inside Pycord

  1. Explore Pycord Extensions

    master

    Pycord provides several extension modules to simplify common development tasks:

    • Bot commands framework: Use ext.commands for creating structured bot commands.
    • asyncio.Task helpers: Use ext.tasks for managing background tasks.
    • Pagination extension: Use ext.pages to implement paginated messages.
    • Slash to Prefix Bridge: Use ext.bridge to bridge slash commands to prefixed commands.
  2. Use Data Classes as containers

    master

    Pycord provides several data classes designed to act as containers for attributes. Unlike models, you can manually instantiate most of these classes to hold data.

    Note on Attributes: Most data classes use __slots__, meaning you cannot add dynamic attributes to them. The only exception is Object, which is designed to support dynamic attributes.

  3. Configure Gateway Intents

    master

    Intents allow your bot to subscribe to specific buckets of events. You must pass an intents object to the constructor of discord.Client or commands.Bot.

    If no intents are provided, the library defaults to all intents being enabled except the privileged intents: Intents.members, Intents.presences, and Intents.message_content.

    To use specific intents, you can either use discord.Intents.default() and toggle specific attributes, or instantiate discord.Intents with specific boolean flags.

    import discord
    
    # Option 1: Using default and disabling specific ones
    intents = discord.Intents.default()
    intents.typing = False
    intents.presences = False
    
    # Option 2: Explicitly enabling specific intents
    intents = discord.Intents(messages=True, guilds=True)
    intents.reactions = True
    
    # Apply to Client or Bot
    client = discord.Client(intents=intents)
    # OR
    from discord.ext import commands
    bot = commands.Bot(command_prefix='!', intents=intents)
  4. Invite Your Bot to a Server

    master

    Once a bot account is created, you must generate an OAuth2 invite URL to add it to a Discord server:

    1. Go to the Discord Developer Applications page and select your bot.
    2. Expand the OAuth2 tab and select URL Generator.
    3. Under Scopes, select:
      • bot
      • applications.commands
    4. Under Bot Permissions, select the specific permissions your bot requires to function.
    5. Copy the generated URL, paste it into your browser, select a server (where you have Manage Server permissions), and click Authorize.

    Note: Requiring Administrator permissions carries significant security implications for the server.

  5. Set up basic logging for Pycord

    master

    Pycord uses the standard Python logging module to output errors and debug information. It is strongly recommended to configure logging at the start of your application; otherwise, no errors or warnings will be visible.

    To output logs to the console, use logging.basicConfig(). You can specify the level argument to control the verbosity. Available levels are CRITICAL, ERROR, WARNING, INFO, and DEBUG. If no level is specified, it defaults to WARNING.

    import logging
    
    logging.basicConfig(level=logging.INFO)
  6. Handle Message Edits (Cached vs Raw)

    master

    Pycord provides two ways to handle message updates (edits).

    Cached Events

    Requires Intents.messages. Only fires if the message is in the internal cache.

    • on_message_edit(before, after): Provides the message state before and after the edit.

    Raw Events

    Requires Intents.messages. Fires regardless of cache state.

    • on_raw_message_edit(payload): Access the message state before the edit via payload.cached_message.

    Note on Partial Data: Because raw events use the Discord gateway data, the payload can be partial. For example, if only embeds were updated, the 'content' key might be inaccessible in the payload dictionary.

  7. Migrate to Pycord v2.0: Event Changes

    master

    The following changes apply to event dispatching in v2.0:

    • on_presence_update replaces on_member_update for tracking changes to Member.status and Member.activities.
    • on_private_channel_create and on_private_channel_delete are no longer dispatched.
    • on_socket_raw_receive now always passes a decompressed and decoded str value, and is no longer dispatched for incomplete data.
  8. Understand Pycord's versioning and breaking change guarantees

    master

    Pycord follows Semantic Versioning (SemVer). Major version updates are used for incompatible API changes.

    Crucial Rule: Breaking change guarantees apply only to publicly documented functions and classes.

    If a function, class, or attribute is not listed in the official documentation, it is considered part of the internal API and is subject to change without notice. This includes:

    • Attributes starting with an underscore (e.g., _internal_attr).
    • Functions without an underscore that are not documented.

    When upgrading, assume that any undocumented behavior or attribute might break.

  9. Migrate to Pycord v2.0: Privileged Message Content Intent

    master
    The Intents.message_content intent is now a privileged intent. If this intent is not enabled, Message.content, Message.embeds, Message.components, and Message.attachments will return empty values, which will prevent ext.commands.Command from functioning correctly.
  10. Use integer snowflakes for IDs

    master

    Starting from v1.0, all snowflakes (the id attribute) are of type int. Previously, they were strings. When fetching objects or comparing IDs, ensure you use integers.

    Example:

    # Before v1.0
    ch = client.get_channel('84319995256905728')
    if message.author.id == '80528701850124288':
        ...
    
    # After v1.0
    ch = client.get_channel(84319995256905728)
    if message.author.id == 80528701850124288:
        ...
    ch = client.get_channel(84319995256905728)
    if message.author.id == 80528701850124288:
        ...