[Tutorial] How to make a simple discord bot, In Javascript.

Started by SA:MP, May 05, 2023, 05:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SA:MP

[Tutorial] How to make a simple discord bot, In Javascript.

_________________



Introduction




Hey there! I released a moderation bot here in the forums today and a lot of people were asking on how I made it, so here I am making a tutorial for everyone. To make a discord bot you can use python or javascript, in this case, we will be using javascript as it is easier (I think so at least). So, I am sure most of you know this but just to make sure ill say it again. Discord is a real-time messaging platform that bills itself as an "all-in-one voice and text chat for gamers." Due to its slick interface, ease of use, and extensive features, Discord has experienced rapid growth and is becoming increasingly popular even among those with little interest in video games, including the SAMP community. For this bot you can use any kind of editor you want, i recommend Visual Studio Code. Let's start making our bot.



1 -Create an application in the developer portal




Go to - https://discord.com/developers/applications

After you, Login/Signup create an application.






2 - Create a bot for your application


Go to your new application and create a bot for it, after that copy the token from it.












3 - Copy the client ID and invite the bot




Go to - https://discordapi.com/permissions.html

Enter your Client ID and enable all permissions, then click the link to invite the bot to the desired server.






4 - Making the actual bot




Now read carefully. First of all, you need to download Node.js - https://nodejs.org/en/download/

Then you create a folder in your desktop and using the command prompt use "cd" to go to the folder you created.

Then you can do "npm init", you may skip all the steps by pressing enter if you want. This will create a son file.

After that we need to install the discord.js modules, we can do that by typing "npm install discord.js" in the command prompt. Now you will find a folder named "node_modules" in your discord bot's folder. (Ignore the warnings that it will show)

Next, we need to create a new file in our folder called index.js. now we can start making the actual script.

I will only give you the basics, the rest you will have to learn by yourself (I can assist at any time if you need help). Try to write it instead of just copy/pasting, it really helps.






PHP Code:




const Discord = require("discord.js"// We require discord.js's library

const client = new Discord.Client() // We initialize it by calling Client()

client.on("ready", () => { // Then we listen for the ready event via the on method and we tell how to handle this event with a callback function.

  
console.log(`Logged in as ${client.user.tag}!`) // Send's a message in the console when the bot starts

})

client.on("message"msg => { // // Now we listen for the message event via the on method and we tell how to handle this event with a callback function again.

  
if (msg.content === "ping") { // This tells the bot that if a user types "ping", he should respond with "Pong!"

    
msg.reply("Pong!")

  }

})

client.login("token-goes-here")// Here you should put the token you copied from the discord's developer website. 






To start the bot you can do "node index.js"(or any other name u used), or you can create a .bat file and put in it the following code:


PHP Code:




echo off

cls

echo Starting Bot

node index
.js

pause 






Enjoy!




Now this is the most basic bot you may create, there are a lot of other features you may add, economy systems, new commands, etc. No-one was born as a developer It only takes concentration, put some time into it and you will reach perfection. Feel free to ask me any questions you have, ill be glad to respond and assist you.

Source: [Tutorial] How to make a simple discord bot, In Javascript.