Mr.Loki

[TUT] Adding a Discord bot to your server. [Newbie friendly]

Recommended Posts

OK so after many requests from people asking me to install discord bots to their servers I have decided to make the simplest tutorial on setting up a bot for your servers.

[NOTE]: THIS IS A VPS TUTORIAL. IF YOUR SERVER ISN'T RUNNING ON A VPS THIS WILL NOT WORK FOR YOU UNLESS YOU UNDERSTAND HOW NODE.JS WORKS.

Part 1: Bot creation

Spoiler

Go to this site. Then you will need to login with your discord account.

Click My Apps to the left under applications then click the New App button.

Spoiler

VI07P5B.png

 

Then give your bot a name and hit Create App

Spoiler

gXDmkZR.png

 

After creating the app you will see some basic setting for the app. Scroll down till you see the create bot user button and click it

Spoiler

hK3MShr.png

 

The app will be converted to a bot now we will copy the token and store it somewhere for later use.

Spoiler

P4ZWUor.png

 

Do not share this token with anyone else they will be able to access your bot.

Next you scroll down to the bottom and hit save changes.

 

Now we need to invite the bot to join our discord server.

To the left under Topics click on OAuth2 then scroll down till you see Bot Authorization Flow and copy the URL example

Spoiler

MofgkWh.png

 

Paste that link into the address bar in your browser and where it says client_id=... you need to change it to the Client id of your bot then hit enter.

Client ID can be found here:

Spoiler

xDQUODE.png

 

 

 

If done correctly you should get this page

Spoiler

NE2WjTu.png

 

 

 

Next select your server from the drop down list and authorize the bot to the server you want it in

 

If done correctly you will now see the bot offline in your server:

Spoiler

wKmvytp.png

 


Part 2: Files Configuration

Spoiler

Firstly you will need the mo_sockets module installed on your server.

Now we need to download the files needed to get the bot running.

Spoiler

ScwJeVR.png

 

Now extract those files and you should get a folder called "mta-discord-bot-master"

In that folder we will rename the 2 folders called mta and src

Rename mta to [Discord] and src to Discordbot.

Upload the [Discord] folder to your resources folder in your server.

Spoiler

yvjCqVT.png

 

open the discord resource folder and edit the config.xml.

Set the name of the channel to the name of the channel you want your bot to speak in

Set the passphrase to anything you want


<discord>
    <channel>name-of-your-channel</channel>
    <passphrase>equal-to-the-server-passphrase</passphrase>
    <hostname>localhost</hostname>
    <port>8100</port>
</discord>

 

Now upload the Discordbot folder to somewhere in your VPS. I uploaded to the same folder that my server is in for easy access.

Spoiler

asFvIZX.png

 

Rename the example.config.json file to config.json and open it


{
    "port": 8100,
    "passphrase": "key",
    "guild": "guild.id",
    "bots": [{
        "channel": "channel.name",
        "token": "bot.token"
    }]
}

 

Set the passphrase to the same as your discord resource passphrase we set earlier.

Your guild id is located in your server settings > widgets

Spoiler

cnhGC4w.png

 

Set the channel to the same as the channel you set earlier. This is where the bot will read messages.

Input the token we got earlier when we created the bot.

Spoiler

P4ZWUor.png

 

 

Part 3: SSH commands
 

Spoiler

Now that we have everything setup and ready here comes the fun part

Login to your VPS's console and install node.js

Here's a site containing different OS and how to install nodejs on those systems.

Once it's installed perform these commands to check if you have node installed correctly along with npm

"npm -v" and "node -v"

Spoiler

SrS8MLw.png

Now that we have them installed we need to install process manager 2 aka pm2

To install pm2 we run this command "npm install pm2 -g"

to make sure it's installed do a version check like we did with  npm and node

 

Navigate to your Discordbot directory with the command "cd path/to/folder"

Now that we are in the Discordbot directory we run the command "ls" to list the items in that directory.

Spoiler

0Ggghnd.png

Now we use the command "npm install" to install all discord modules needed for the bot.

 

Listed in the directory we can see app.js which we must run with pm2 so we use the command "pm2 start app.js"

Spoiler

hxqB8wd.png

 

Make sure the bot is running correctly by checking the logs on the app by doing "pm2 logs app"

You should now see these messages:

DWkXojD.png

And you will now see your bot online in the server

O23pQmZ.png

 

Now all that's left to do is to start the discord resource in your server and once you do the bot will test it's connection to the discord by sending a message back and forth in the channel you set the bot

B8j3xia.png

 

 

That should be all now go have fun annoying players in your server.

Some feedback would be appreciated xD

Good luck :P

 

Edited by Mr.Loki
Forgot a step :o
  • Like 8
  • Thanks 1

Share this post


Link to post

Very nice Loki! Hopefully this will help out everyone who needs help setting this whole thing up. It's not an easy thing to do if you're completely new to all of this.

  • Thanks 1

Share this post


Link to post

Well mostly don't know about this they think it will work from discord without vps but by this tutoring they will know how to add discord for there server thanks for sharing this tutorial help full !

Meanwhile you had used connecting instead of adding xD

Edited by Khadeer143
  • Thanks 1

Share this post


Link to post

@Equis Yes it can work on a local server. Pretty much the same steps.

I mentioned VPS because i know some people have their server based on slots and not a VPS.

Share this post


Link to post
4 hours ago, Mr.Loki said:

@Equis Yes it can work on a local server. Pretty much the same steps.

I mentioned VPS because i know some people have their server based on slots and not a VPS.

Thanks for the respond!

Share this post


Link to post

Can anyone give me an example for sending a command from Discord to MTA.
 

E.G:

!ban random123 reason123
!onlineplayers
 
Btw thank you in advance if anyone could reply.

 

Share this post


Link to post

@JamesB

You will need to modify the onDiscordPacket event at line 75 in the discord-events resource.

 

Share this post


Link to post
5 hours ago, Mr.Loki said:

@JamesB

You will need to modify the onDiscordPacket event at line 75 in the discord-events resource.

 

Could you enlighten it, because I don't understand why I should modify that event?

Share this post


Link to post
14 hours ago, JamesB said:

Could you enlighten it, because I don't understand why I should modify that event?

That event captures every thing that happens in the discord server it is connected to.

By monitoring the packed and payload parameters you can see what gets sent to the event.

You can monitor it by using the iprint function.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.