Language: Python
Web
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.
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.
pip install py-cordconda install -c conda-forge py-cordPycord 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.
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.
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.
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.
@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.
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.
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.
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.
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.