DEV Community

Cover image for Discord Developer Cheat Sheet
Emīls Oto Leimanis
Emīls Oto Leimanis

Posted on

Discord Developer Cheat Sheet

A quick reference guide for creating, managing, and troubleshooting Discord bots and applications.


Bot Basics

1. Creating a Discord Application

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");
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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!"
  }
Enter fullscreen mode Exit fullscreen mode

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!");
      }
  });
Enter fullscreen mode Exit fullscreen mode

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] });
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Tips and Best Practices

  1. Secure Your Tokens:

    • Use environment variables to store sensitive information.
    • Example: process.env.DISCORD_TOKEN
  2. Test Locally Before Deploying:

    • Use tools like nodemon for live testing.
  3. Rate Limits:

    • Discord enforces rate limits. Avoid spamming API calls.
  4. 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)