SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 12/05/2020, 03:39 PM   #1
JasonTheMan
Big Clucker
 
JasonTheMan's Avatar
 
Join Date: Apr 2020
Posts: 55
Reputation: 2
Lightbulb 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.
__________________
Owner/Founder - DesignUX
New Project - RedWood High
JasonTheMan is offline   Reply With Quote
Old 12/05/2020, 04:16 PM   #2
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 440
Reputation: 20
Default Re: How to make a simple discord bot, In Javascript.

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
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 12/05/2020, 06:39 PM   #3
JasonTheMan
Big Clucker
 
JasonTheMan's Avatar
 
Join Date: Apr 2020
Posts: 55
Reputation: 2
Default Re: How to make a simple discord bot, In Javascript.

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.
__________________
Owner/Founder - DesignUX
New Project - RedWood High

Last edited by JasonTheMan; 12/05/2020 at 07:59 PM.
JasonTheMan is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[FilterScript] Discord Admin Control Panel - Manage players through discord! Exhibit Filterscripts 29 14/05/2020 05:45 PM
[FilterScript] Discord Chat - Real time chatting ( Discord + In Game) Exhibit Filterscripts 36 16/10/2019 08:32 PM
javascript. Banditukas Everything and Nothing 18 04/04/2015 05:00 PM
Use PHP in javascript Banditukas Everything and Nothing 5 08/11/2014 05:44 AM
How to learn Javascript fast and effective to make use in business website? Bennysmith Help Archive 6 29/12/2010 03:44 AM


All times are GMT. The time now is 01:55 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.