How to develop your first Discord Javascript Bot Script - Send Messages over a specific channel on Discord..
Hi everybody,
today i wanna show you how to create a simple Javascript Discord Bot. I usually use my bots to make automatic upvote and comment on steem blockchain, but we can create an "info" Bot to send a specific message to Discord users.
Today we speak about how to create a bot with Javascript and how to send a specific Message through a specific Discord Channel, after a specific text is sent.
Requirements
- A discord Server
- A specific Group on discord with permission to read/write messages
- A specific Channel to send messages
- A Discord Bot
- A Server where to run the bot script
- Libraries
1- Discord Server
If you don't have a Discord Server you can create a new one to make this test.
Click on The + botton under the server list and create a new one:
2- Create a Specific Server Role
To prevent the bot make some strange movement on our server, we limit it only to read and write messages on a specific channel. We are going to create a Role called "test" with this permissions:
Right click on Server - Server Settings - Roles
- Read the text channels and see the voice channels
- Send Messages
- Read the message history
3- Create a Specific Channel to send a message
We don't want the bot can spam a message over all channels, we need the bot can send a message only on a specific channel. For example we can create an Info message where we Say on The Test Channel whatever we need:
To create a New Channel Click + under the list of channels
4- Add the test role to the test channel
We created a test role and a test channel. Now we have to combine it to permit the bot to use it.
Go Under channel settings and click on permissions
Click on + and select the Role Test we created before
Now Block all permissions to this Role except:
- Show Role Members on a different category
- Read Messages
- Send Messages
- Read the message history
5- Create a Discord Bot
I suggest to follow this Guide to create a new discord Bot.
We need it because every bot have a specific token to be used inside the script:
https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token
I called it: discord-test-bot
Once the bot is created you have to invite it (changing client Id) on your server as explained on documentation:
https://discordapp.com/oauth2/authorize?&client_id=YOUR_CLIENT_ID_HERE&scope=bot&permissions=0
It will ask on whick Discord server to add it (maybe you have multiple servers)
Select TEST
As you can see the Bot is on your server but is offline because you have not started the script yet.
6 - Install Libraries and start the Bot
To do these tests i used my VPS server, but you can use your own hosted server. For simplicity we will use an Ubuntu Linux Server by using our server terminal with Putty.
The libraries we are using are called Discord.js
- Create a new folder called Test with the command "mkdir test"
- Enter into this folder and create a file called test.js with the command "nano test.js"
- Write your first Bot script
This is the site of Discord.js: https://discord.js.org
Example of a discord.js script
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping' && msg.channel.id == '445320345864503328') {
msg.reply('Pong!');
}
});
client.login('BOT TOKEN');
Now change your token and channel id, then try this script simply copying it on your test.js file.
To see and copy the channel id, you need to enable development mode on Discord.
https://support.discordapp.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-
Start the script with the command: node test.js
Pay attention, because before starting the script you need to install NODE JS and other libraries
Install all this libraries before run the node command:
sudo apt-get install nodejs
sudo apt-get install npm
npm install discord.js
If you have done all correct a message like this should appear on console
And the bot should be seen as started
7 - Add the Bot to a specific Role
As we said before, we created a specific role to prevent any abuse by the javascript Bot if anyone take control of our server bot. Now we have to add our Bot to the Role called Test.
Right click on the Bot - Roles - Test
Now the Bot is under a Role called Test where can only read and write messages.
Our scipt is a simple script where users write a specific text and the bot answer with another text. We write "ping" into a "test" channel and the bot write Pong!
As you can see if we write into another channel the bot don't answer because we created an if statement for it.
Try and report me any problem.
Remember to upvote me as Witness
https://steemit.com/~witnesses
wow!!!
that's a nice information,,I will try it up,,
thanks for sharing..
it's a pleasure
@yanosh01 upvoted this post via @poetsunit!!
Poetsunited - DISCORD - @poetsunited - witness upvote
@yanosh01 upvoted this post via @poetsunit!!
Poetsunited - DISCORD - @poetsunited - witness upvote
@yanosh01 upvoted this post via @poetsunit!!
Poetsunited - DISCORD - @poetsunited - witness upvote
@yanosh01 upvoted this post via @poetsunit!!
Poetsunited - DISCORD - @poetsunited - witness upvote
@yanosh01 upvoted this post via @poetsunit!!
Poetsunited - DISCORD - @poetsunited - witness upvote
@yanosh01 upvoted this post via @poetsunit!!
Poetsunited - DISCORD - @poetsunited - witness upvote
@yanosh01 upvoted this post via @poetsunit!!
Poetsunited - DISCORD - @poetsunited - witness upvote