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

_________________

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.
Reply
#2

By recommending VSCode, do you have any real reason for recommending it or is it just because you use it? It'd be good for you to explain why you recommend certain editors. Personally, I prefer to use VSC as well, but I know a great amount of people like to use other editors like Sublime, etc.

Also, this isn't necessarily a major issue but a nitpick. Most of this tutorial is text, which makes it less of a convenience to read through it and therefore, I'd suggest using more screenshots.

Furthermore, some lines are poorly explained for people new to this.
Major offenders such as:
Code:
const Discord = require("discord.js") // We require discord.js’s library
const client = new Discord.Client() // We initialize it by calling Client()
There's absolutely no reason for comments here as they say nothing at all: either make them useful or not at all.

I'd recommend adding a section about commands as most bots aren't going to be this simple.
For anyone who wants to know how to make a basic command:
Code:
const Prefix = "!"

if(msg.content === `${Prefix}cmdhere`)
Finally, you should be splitting your message if you're going to use arguments (which most bots are going to use, otherwise it's pretty pointless).

Code:
let array = message.content.split(" ");
let command = array[0]; // changed from your msg
let args = array.slice(1); // slice it again as there may be multiple arguments
Reply
#3

Got it, will add more things, and repost it. Thank you - in fact, instead of just making the thread longer and longer, ill just make a youtube video with a detailed guide, giving information about commands, etc.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)