A quick reference guide for creating, managing, and troubleshooting Discord bots and applications.
Bot Basics
1. Creating a Discord Application
- Go to the Discord Developer Portal.
- Click New Application > Name your application > Save.
2. Generating a Bot Token
- Navigate to Bot > Click Add Bot.
- Copy the token. Keep it secure.
3. Setting Bot Permissions
- Go to OAuth2 > URL Generator.
- Select bot scope and assign required permissions.
- Use the generated URL to invite the bot.
Key Libraries
1. Discord.js (JavaScript)
- Install:
npm install discord.js
- Example Initialization:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
client.once('ready', () => console.log('Bot is online!'));
client.login('YOUR_TOKEN');
2. Discord.py (Python)
- Install:
pip install discord.py
- Example Initialization:
import discord
client = discord.Client()
@client.event
async def on_ready():
print('Bot is online!')
client.run('YOUR_TOKEN')
Useful API Endpoints
1. List Guilds
-
GET
/users/@me/guilds
- Returns a list of guilds the bot is in.
2. Send Message
-
POST
/channels/{channel.id}/messages
- JSON Payload:
{
"content": "Hello, World!"
}
3. React to Message
-
PUT
/channels/{channel.id}/messages/{message.id}/reactions/{emoji}/@me
Common Bot Features
1. Command Handling
- Discord.js Example:
client.on('messageCreate', (message) => {
if (message.content === '!ping') {
message.reply('Pong!');
}
});
2. Embed Messages
- Discord.js Example:
const { MessageEmbed } = require('discord.js');
const embed = new MessageEmbed()
.setTitle('Hello!')
.setDescription('This is an embed.')
.setColor('#0099ff');
message.channel.send({ embeds: [embed] });
3. Role Assignment
- Discord.js Example:
const role = message.guild.roles.cache.find(r => r.name === 'Member');
const member = message.mentions.members.first();
member.roles.add(role);
Tips and Best Practices
-
Secure Your Tokens:
- Use environment variables to store sensitive information.
- Example:
process.env.DISCORD_TOKEN
-
Test Locally Before Deploying:
- Use tools like
nodemon
for live testing.
- Use tools like
-
Rate Limits:
- Discord enforces rate limits. Avoid spamming API calls.
-
Logging:
- Add logs to monitor bot activity and errors.
Troubleshooting
1. Bot Not Responding
- Ensure the bot is online and has the necessary permissions.
- Check token validity.
2. Permission Errors
- Revisit OAuth2 permission settings.
- Ensure the bot has role-based permissions in the server.
3. Rate Limit Errors
- Add delays between API calls.
- Respect Discord’s rate limits to avoid bans.
This cheat sheet serves as a starting point for Discord development. Explore the Discord API Documentation for advanced features!
Top comments (0)