How to Build a Discord Bot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for years now people have been asking me to create a discord server well i'm happy to inform you that today's the day the fire ship discord server goes live it's open to everyone and when you sign up you'll be welcomed by a custom bot that i built that will assign you a special role if you happen to be a fireship pro member the bot also registers a slash command that allows you to request a sticker or t-shirt if you're a pro member i built this bot from scratch with node.js and in this video i will show you how i did it there are many different types of bots out there you've got dank memor to post the dankest of memes many tools focused on productivity and many others that can gamify your server but a couple of others that we're using include a bot for support tickets and another one that will automatically post my new videos into a special channel and we also have a bot that's going to give away some free t-shirts to a few lucky people who join the discord server today the problem is that if there's not a good existing discord bot that suits your needs like if you need to integrate with your own user data then you'll have to build one on your own first of all what is a discord bot a bot is like a user that can listen to different events that happen in your server in order to automate basically anything you can imagine a bot can respond to messages create ui elements register commands and stuff like that to build your own you'll first want to go to the discord developer dashboard and create a new application from there go to the bot panel and click on the add bot button it'll ask you to confirm so go ahead and say yes do it and then it should confirm that a wild bot has appeared now this bot can be installed on multiple servers or guilds as they're called to add it to your server you'll need to create an invite link do that by going to oauth2 then url generator set the scope to bot and then give it administrator privileges if you want it to do everything or if you know exactly what it needs to do you can refine these permissions now go ahead and copy the link into the browser and it will take you to a confirmation screen to install that bot onto a server that you're an admin of and now we can actually get into our code to create a bot you'll need an actual dedicated server building a discord bot in a server-less environment is not really feasible because it requires a real-time connection to the server i deployed mine for free on google app engine but like any small vm would work like a 5 droplet on digitalocean to create that server in node.js first run npm init y to create a new project create an index.js file for the code then install a library called discord.js to simplify the process of interacting with the api to initialize the package we'll need some environment variables which means we'll also want to install.env after that go ahead and create a dot env file then create a variable for your discord token go back to your discord app to reveal this value and make sure not to expose it publicly because then a hacker could have full control over your bot now in the javascript we can initialize.env and then import the client from discord.js it takes an array of intents for the types of data that you want access to like guilds which refers to the server itself guild messages all the messages that happen there members direct messages and so on and finally use the client to log in with your discord token now one of the main things you'll do with the client is listen to events for example if you want to listen to every message that's created in the server you could do that by listening to the message create event it takes a callback function as its argument that will be fired on every message in the server now if you want to create something really annoying you could check to see if that message was sent by a bot and if not you could have the bot respond to that message saying the exact same thing basically a copycat for every message that's pretty cool but it's generally not useful to just send text messages back and forth with a bot it's a lot easier to create interactions like buttons or pop-up modals to get the exact information you need from a user the library has a whole bunch of built-in classes like button builder to create custom interactions it relies on the object oriented builder pattern which i'm not a huge fan of but basically you chain methods together to create these ui elements most importantly each element has a custom id the id is a string value that can be whatever you want that you can intercept later to handle the interaction but first we need to send this button to a user in a message when sending a message you have the option to add multiple components so let's go ahead and add our button there and now we can use the client to listen to the interaction create event which will be triggered when the user clicks on the button inside the function we can use a conditional or switch statement to identify the custom id we're looking for and then handle it accordingly the easiest thing to do is just reply with a message and it's also useful to know that you can make messages ephemeral which means they'll be short-lived and only seen by the user who triggered that interaction but instead of a reply you could also trigger another interaction here like bring up a modal to collect additional information but that's pretty much all it takes to build a discord bot if you have any questions head over to discord and chat with me about it there thanks for watching and i will see you in the next one
Info
Channel: Beyond Fireship
Views: 191,491
Rating: undefined out of 5
Keywords:
Id: Oy5HGvrxM4o
Channel Id: undefined
Length: 4min 40sec (280 seconds)
Published: Mon Sep 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.