Making a discord bot with javascript. part1 - Paperytxt/bot.txt GitHub Wiki
hi everyone, I just wanted to make this to tell you how to make a one paged discord bot with JavaScript. Before we get started, we will things to set it up. You'll need
- A code editing software (eg. visual studio - atom. NOT notepad)
- Nodejs
- A folder. Any name, just don't make it have spaces.
- In that folder and file called index.js or bot.js
- A bot profile
Firstly, go to the folder you put your js file in and hold shift and right click. then click Open PowerShell window here.
Type npm init -y
and then npm install discord.js
.
Next, create a bot profile at this link. Give it a name and on the side go to Bot, then to Build a bot then click add bot. click OAuth2 on the side the scroll down to the very bottom and then click "bot". go to that link and add the bot to your server. do you see it?
Now, Open up your js file in that folder. Its empty right? at the very top of the script put in
const Discord = require('discord.js');
and then
const bot = new Discord.Client();
We are ready for the bot to start doing stuff. Lets have a start up trigger. imput
bot.on('ready', () => {
bot.user.setActivity("Being a bot is hard.")
console.log("Bot is online.")
});
below the consts and put
bot.login("your-veryepic&token");
at the bottom. inside it put your bot token in it. DON'T SHARE THIS WITH ANYONE. If anyone else gets ahold of it they can do stuff with your bot without your consent. if this ever happens, regenerate the token and replace it.
You have to save it or it wont work. In that powershell window put in node .
to start the bot. wait until you see Bot is online.
or whatever you put in it. then see if you can see if the bot is online. it cant do anything now, but im gonna get to that now.
bot.on('message', function(message){
if(message.content == 'trigger message')
{
// you can choose message.reply or message.channel.send.
message.reply("response message")
}
});
whats the difference between message.channel.send and message.reply? well, nothing really its just reply is mentioning the person at the start of your message. Save it and type in trigger message and the bot should respond with <@you> response message.
So thats all im gonna do today. ill do more later like making commands.
finishing product
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('ready', () => {
bot.user.setActivity("Being a bot is hard.")
console.log("Bot is online.")
});
bot.on('message', function(message){
if(message.content == 'hello')
{
message.reply("Hi there!")
}
});
bot.login("your-veryepic&token");