The Ultimate Guide to Building a Powerful Discord Bot with Comprehensive API Examples

Introduction to Building a Discord Bot

Creating a Discord bot allows you to add fun, moderation, and other functionalities to your Discord server. In this guide, we will explore various APIs provided by Discord.js, the most popular library for interacting with the Discord API. We will also provide many API examples and a comprehensive app example that utilizes several of these APIs.

Getting Started

First, you need to set up your development environment. Create a new directory for your bot, and then install Discord.js:

  
    mkdir discord-bot
    cd discord-bot
    npm init -y
    npm install discord.js
  

Creating a Simple Bot

The following code creates a simple bot that logs in and responds to a “ping” command with “pong”:

  
    const { Client, GatewayIntentBits } = require('discord.js');
    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

    client.once('ready', () => {
      console.log('Bot is online!');
    });

    client.on('messageCreate', message => {
      if (message.content === 'ping') {
        message.channel.send('pong');
      }
    });

    client.login('YOUR_BOT_TOKEN');
  

Useful APIs and Examples

Here are some useful APIs along with code snippets:

Fetching Server Info

This API call fetches information about the server:

  
    client.on('messageCreate', async message => {
      if (message.content === '!serverinfo') {
        const { guild } = message;
        message.channel.send(`Server name: ${guild.name}\nTotal members: ${guild.memberCount}`);
      }
    });
  

Kick a Member

This API call kicks a member from the server:

  
    client.on('messageCreate', async message => {
      if (message.content.startsWith('!kick')) {
        if (!message.member.permissions.has('KICK_MEMBERS')) return message.reply('You do not have permission to kick members!');
        const member = message.mentions.members.first();
        if (member) {
          await member.kick();
          message.channel.send(`${member} has been kicked`);
        } else {
          message.channel.send('Please mention a valid member to kick.');
        }
      }
    });
  

Ban a Member

This API call bans a member from the server:

  
    client.on('messageCreate', async message => {
      if (message.content.startsWith('!ban')) {
        if (!message.member.permissions.has('BAN_MEMBERS')) return message.reply('You do not have permission to ban members!');
        const member = message.mentions.members.first();
        if (member) {
          await member.ban();
          message.channel.send(`${member} has been banned`);
        } else {
          message.channel.send('Please mention a valid member to ban.');
        }
      }
    });
  

Complete Bot Example

Here is a comprehensive example that includes several of the APIs mentioned above:

  
    const { Client, GatewayIntentBits } = require('discord.js');
    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

    client.once('ready', () => {
      console.log('Bot is online!');
    });

    client.on('messageCreate', message => {
      if (message.content === 'ping') {
        message.channel.send('pong');
      } else if (message.content === '!serverinfo') {
        const { guild } = message;
        message.channel.send(`Server name: ${guild.name}\nTotal members: ${guild.memberCount}`);
      } else if (message.content.startsWith('!kick')) {
        if (!message.member.permissions.has('KICK_MEMBERS')) return message.reply('You do not have permission to kick members!');
        const member = message.mentions.members.first();
        if (member) {
          member.kick().then(() => message.channel.send(`${member} has been kicked`));
        } else {
          message.channel.send('Please mention a valid member to kick.');
        }
      } else if (message.content.startsWith('!ban')) {
        if (!message.member.permissions.has('BAN_MEMBERS')) return message.reply('You do not have permission to ban members!');
        const member = message.mentions.members.first();
        if (member) {
          member.ban().then(() => message.channel.send(`${member} has been banned`));
        } else {
          message.channel.send('Please mention a valid member to ban.');
        }
      }
    });

    client.login('YOUR_BOT_TOKEN');
  

And there you go! You’ve built a feature-rich Discord bot that can respond to commands, fetch server information, kick, and ban members!

Hash: a73dbaeaf36c5ec584074d946b9543d5126aad17316d79f1d6a59d38d041f174

Leave a Reply

Your email address will not be published. Required fields are marked *