blog.

Build a Discord Bot 👾

* this post is apart of a workshop i hosted for Starhacks 💫 *

Discord is a powerful messaging platform with great chat capabilities originally built to serve gaming communities. it has grown to a massive popularity and offers a lot of customization and autonomy via bots, custom moderation tools and more, which I’ll show you how to work with! in this tutorial I’ll show you how to set up a fully customizable Discord bot that can be running on your server in under 15 minutes. this tutorial is taught using Javascript, but if you are a beginner don’t worry! We’ll walk through it together.

i set this repository up so that anyone can fork or clone it and add their own tokens and capabilities for their own bot easily, so if you don’t want to start from scratch, fork the repo!

prerequisites

workshop resources

steps

1.) Creating the Discord bot

2.) Adding the bot to your server

3.) Creating our Project

4.) DiscordJS basics

{
"prefix": "!",
"token": "your-token"
}
const Discord = require('discord.js');
const {
	prefix,
	token,
} = require('./config.json');
const { Client, Intents, MessageEmbed } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.login(token);
client.once('ready', () => {
    console.log('Ready!');
});
client.once('reconnecting', () => {
    console.log('Reconnecting!');
});
client.once('disconnect', () => {
    console.log('Disconnect!');
});

5.) Reading and Writing Message Commands

client.on('messageCreate', async message => {
  
}
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
if (message.content.startsWith(`${prefix}name`)){
    message.reply(`Your name is ${message.author.username}`); // sends reply in channel to author
    return;
} else if (message.content.startsWith(`${prefix}greeting`)){
    message.channel.send("Hello world!"); // sends general message to channel
    return;
} else if (message.content.startsWith(`${prefix}secret`)){
    message.author.send("Shhh! This is a secret message for you"); // sends direct message to author
    return;
} else if (message.content.startsWith(`${prefix}quote`)){
    const quoteEmbed = new MessageEmbed().setColor("ORANGE").setTitle(`Quote for ${message.author.username}`).setURL("https://discord.js.org/#/docs/discord.js/stable/class/Client").setDescription("To be, or not to be. That is the question.")
    message.channel.send({embeds: [quoteEmbed]}); // sends embedded message to channel
    return;
} else {
    message.channel.send("You need to enter a valid command!"); // if invalid command, send this error message to the channel
}

6.) Run Locally 📡

7.) Pushing to your repository

If you push your repo as is, your secret bot token will be exposed publicly will cause Discord to flag you down and your token will no longer work. To make sure your token stays secure, we need to tell our project to ignore when pushing to our repository. We will also include the node_modules folder since that is also not needed for pushing our project

TOKEN='your-bot-token'
PREFIX='your-prefix'

other resources

conclusion

if you have any questions or feedback, let me know by messaging me in Discord! discord (racheletc#1212)