Language: Python
Web
discord.py was created by Rapptz to provide a Pythonic interface to the Discord API. It abstracts HTTP requests and WebSocket connections, allowing developers to focus on bot logic and automation. It supports both synchronous and asynchronous programming, enabling scalable and responsive bots.
discord.py is a Python library for interacting with the Discord API. It allows developers to create bots that can interact with Discord servers, send and receive messages, manage roles, and respond to events programmatically.
pip install discord.pyconda install -c conda-forge discord.pyThe library allows you to create bots that respond to messages, commands, reactions, and events. It supports creating commands with decorators, listening to events, handling permissions, and integrating with Discord servers seamlessly.
import discord
from discord.ext import commands
TOKEN = 'YOUR_BOT_TOKEN'
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 bot.')
bot.run(TOKEN)Creates a bot that responds to the command `!hello` with a greeting message.
channel = bot.get_channel(CHANNEL_ID)
await channel.send('Hello, Discord!')Sends a message to a specific channel using the bot instance.
@bot.event
async def on_reaction_add(reaction, user):
if user != bot.user:
await reaction.message.channel.send(f'{user} reacted with {reaction.emoji}')Replies with the reaction added by users, ignoring the bot’s own reactions.
from discord import Embed
embed = Embed(title='Sample Embed', description='This is an embed', color=0x00ff00)
await ctx.send(embed=embed)Creates a rich embedded message with title, description, and color.
@bot.event
async def on_member_join(member):
await member.send('Welcome to the server!')Sends a private welcome message to users when they join the server.
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 in a class (Cog) for modularity and clean code structure.
import asyncio
@bot.event
async def on_ready():
while True:
print('Bot is running')
await asyncio.sleep(60)Runs a repeating asynchronous task every 60 seconds when the bot is ready.
Use cogs to organize commands and events for larger bots.
Handle exceptions in events and commands to prevent bot crashes.
Use asynchronous programming to avoid blocking operations.
Keep bot tokens secure using environment variables.
Respect Discord rate limits to avoid being banned.