How to make a Discord bot with Node.js? – Complete Guide 2021

What does a discord bot do?

A Discord bot can do many things like play music, post images or links to the channel it’s in, even make fun of you if you ask it nicely enough (or not). There are hundreds of discord bots now available with different strengths and capabilities; some will read out your Twitch chat (very meta), others might tell jokes or keep track of who’s talking the most in a channel. You can create a discord bot that quotes movies, plays games, and interact with websites too – the possibilities are endless! To find out how to make a bot on discord, keep reading. 

discord, discord bot, JS, discord bot js

How to make a Discord bot?

Creating a Discord bot is a great way to automate tasks or provide services in a Discord server. In this article, we’ll show you how to make a discord bot using the Node.js platform and the Discord.io module. We’ll also take a look at some of the basics of bot development, including how to create discord bot interactive and useful for your server members.

  • First, create a new Discord server and invite your friends to join. This will be your testing ground for your bot.
  • Next, head over to https://discordapp.com/developers/applications/me and click “Create an App.”
  • Enter a name for your app and select “Bot” from the Type menu. Click “Create App” and take note of the Client ID and Client Secret that are generated. You’ll need these later.
  • Back in Discord, navigate to the Settings tab of the server you just created, and click “Open Authorized Apps” in the right-hand menu. On that page, click the “Add a New App” button.
  • Fill out all fields with your bot’s information – give it a name and set its icon (it will be displayed next to messages sent by the bot). Enter your Client ID into the Token field, then press “Save Changes.”

Head back into Visual Studio Code, where you left off on creating your first Discord bot. Delete everything inside of Program. cs, as we’ll need to start from scratch now that our app is configured properly! Your code should look something like this:

@ { string token = “” ; } @functions { }

In the code above, we’re declaring a string variable called a token. This will store our Token of discord bot, which we got from Discord earlier. We’ll need to use this later when authenticating with Discord.

Next, we’ll define a function that will be called when our Discord bot starts up. This function will take two parameters – the first is an object containing information about the server, and the second is an array of messages. We’ll use this function to initialize our bot and subscribe to the server’s channels. Add the following code to your Functions block:

private void Start ( DiscordServer server , IEnumerable < Message > messages ) { }

Inside the Start function, we’ll first add a bit of code to subscribe our bot to the specified server’s channels. Add this below your Start function:

@ foreach ( var message in messages ) { if ( !( message is MessageDiscord ) &&! ( message is MessageUser )) continue ; var id = ServerID . FromUniqueId ( message . UniqueID ); var client = new DiscordClient (); client . Connect (); client . SubscribeChannels ( id , UpdateMessageReceived ); }

@discord_channel_update = async data => { };

In the first part, we take a look at all incoming messages and filter out those that are not from a user or from the server itself. In other words, only messages that come from other users on this server will be processed.

Next, we use the DiscordClient class to connect to Discord and subscribe to the channels that were specified earlier. We also define a function called discord_channel_update which will be used to handle messages that are received from Discord. Add this below your Start function:

function UpdateMessageReceived ( message ) { if (! string . IsNullOrEmpty ( message . Text )) { // Do something with the message here } }

This function will be called every time a new message is received from one of our subscribed channels. We can then do whatever we want with the message inside of this function, such as displaying it on-screen or sending it to another server.

Now that our Discord bot is up and running, let’s take a look at how to add some basic functionality.

Function 1 : 

First, we’ll want to create a way for users to send commands to our bot. We can do this by adding a new function called HandleCommand. This function will take one parameter – the command that was typed in by the user. Add the following code to your Functions block:

private void HandleCommand ( string command ) { // Do something with the command here }

In this function, we can check what command was entered by the user and then execute whatever code we want based on that command. For example, if the user enters “Hello,” we could reply back with “Hello there!”

Function 2 : 

Next, let’s add a way for us to call the HandleCommand function. We’ll do this by adding a new command called “hello” and then calling our HandleCommand with that command. Add this below your Start function:

@ { var hello_command = “” ; } @functions { private void HandleCommand ( string command ) { if ( command == null || ! String . IsNullOrEmpty ( command )) { switch ( command ){ case “hello” : case “hi” : MessageResponse = value => client . SendMessageAsync ( value , MESSAGE_TYPES . Chat ); break ; default : // Send an error message to the user } } } private void Start () { MessagesApi . SubscribeToChannelByNameAsync ( channel_name , HandleMessageReceived ); } private void MessagesApi_HandleMessageReceived ( string message ) { UpdateMessageReceived ( new MessageDiscord () { UniqueID = ServerID . FromUniqueId ( message . UniqueID ), Channel = channel_name , From = “Bot” , Text = “<@” + UsernamePrefix + “/” + Channel . Name + “> ” }) }; }

I’ve commented on the default case for our switch statement so that only the name of the command will be used. This way, if you enter anything other than hello or hi, it will send back an error to tell you what commands are available.

Now that we have a way to call this function, let’s add it to our command list. Add the following code below your HandleCommand function:

private void RegisterHelloCommand () { // Add the “hello” command to our list of available commands CommandList . Add ( hello_command ); }

This code will add our “hello” command to the list of available commands. Now, when users type in “hello,” our Discord bot will respond back with “Hello there!”

Function 3 : 

Finally, let’s add a way for us to exit our bot. We can do this by adding a new function called Quit and then calling it from our main function. Add the following code to your Functions block:

private void Quit ( string reason ) { // Do something with the reason here }

In this function, we can perform whatever action is necessary when the user wants to quit our bot. For example, you could add a way for users to leave a reason by adding something like “reason:” at the end of your command.

@ { var quit_command = “” ; } @functions { private void HandleCommand ( string command ) { if ( command == null || ! String . IsNullOrEmpty ( command )) { switch ( command ){ case “quit” : // Do something with the reason here Quit (); break ; case “reason” : MessageResponse = value => client . SendMessageAsync ( chatHelper . CreateReplyWithReasonText ( value , Request ), MESSAGE_TYPES . Chat ); break ; default : // Send an error message to the user } } } private void Start () { MessagesApi . SubscribeToChannelByNameAsync ( channel_name , HandleMessageReceived ); RegisterHelloCommand (); } private void MessagesApi_HandleMessageReceived ( string message ) { UpdateMessageReceived ( new MessageDiscord () { UniqueID = ServerID . FromUniqueId ( message . UniqueID ), Channel = channel_name , From = “Bot” , Text = “<@” + UsernamePrefix + “/” + Channel . Name + “> ” }) }; private void RegisterHelloCommand () { CommandList . Add ( hello_command ); } private void Quit ( string reason ) { if ( reason != null && ! String . IsNullOrEmpty ( reason )) { // Do something with the reason here } Quit ( reason ); } }

In this code, we’ve added a new function called Quit that takes a string as an argument. This string will be the reason why the user is quitting our bot. We can then use this information to do something with it, such as displaying it in a message or logging it somewhere.

Finally, let’s add the following line of code to our main function:

Quit ( “My bot has stopped working!” );

This will call our Quit function and pass in the string “My bot has stopped working!” as the reason. When users type in this command, they’ll see the message “User said: My bot has stopped working!”

Conclusion 

This is how you would build a Discord bot to respond to specific sentences. This can be useful for creating chatbots to handle responses automatically instead of having to type it all yourself.

About Roniyal Devid

blank
With a passion for all things tech, Roniyal Devid has established himself as a leading voice in the world of technology blogging. As the founder and chief editor of a renowned technology blog, Roniyal offers insightful, in-depth, and up-to-date content that caters to both tech novices and experts alike.

Check Also

Instagram Bio For Boys

10 Best Instagram Bio For Boys

You need to have a fantastic Instagram bio to make your profile stand out above …