Making a discord bot with javascript. part2 - Paperytxt/bot.txt GitHub Wiki
Part 2 time. lets make commands. But first, we need a place to put the commands. Put in the following:
bot.on("message", async message => {
});
lets make it ignore messages not starting with your prefix and if they are a bot. this prevents botceptions.
bot.on("message", async message => {
const prefix = ">"
if(message.content.indexOf(prefix) !== 0) return;
if(message.author.bot) return;
});
You can replace the >
with your bot prefix.
Now lets add the command listeners. add these to your code.
bot.on("message", async message => {
const prefix = ">"
if(message.content.indexOf(prefix) !== 0) return;
if(message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(" ")
const command = args.shift().toLowerCase();
});
now lets make some commands. first, lets try a say command
bot.on("message", async message => {
const prefix = ">"
if(message.content.indexOf(prefix) !== 0) return;
if(message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(" ")
const command = args.shift().toLowerCase();
if(command === "say") {
// gives the message a better look, it would look like hello,world but adding this changes it to hello world.
const sayMessage = args.join(" ");
// this deletes the user message and ignores the error it generates.
message.delete().catch(O_o=>{});
//sends the message you sent.
message.channel.send(says)
});
There, not so hard isn't it? lets do one more command. a playstatus command. but we should make it not trigger by anyone now you? well, we can do that. at the top of your script put this in.
const ownerid = "youruserid889834"
now if you want to make it only you, put if(message.author.id !== ownerid) return message.reply("sorry you cant do that");
now lets do this
bot.on("message", async message => {
const prefix = ">"
if(message.content.indexOf(prefix) !== 0) return;
if(message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(" ")
const command = args.shift().toLowerCase();
if(command === "say") {
// gives the message a better look, it would look like hello,world but adding this changes it to hello world.
const sayMessage = args.join(" ");
// this deletes the user message and ignores the error it generates.
message.delete().catch(O_o=>{});
//sends the message you sent.
message.channel.send(says)
}
if(command == 'playstatus'){
// like I said, blocks other users.
if(message.author.id !== ownerid) return message.reply("sorry you cant do that");
// same thing as sayMessage
const playStatus = args.join(" ");
// sets the playstatus
bot.user.setActivity(`${playStatus} | any thing else you wanna add.`)
// guess what this does?
message.reply("sure thing.")
}
});
that's it for part 2. in part 3 ill cover JSON databases and how to make a coin system.
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.on("message", async message => {
const prefix = ">"
if(message.content.indexOf(prefix) !== 0) return;
if(message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(" ")
const command = args.shift().toLowerCase();
if(command === "say") {
// gives the message a better look, it would look like hello,world but adding this changes it to hello world.
const sayMessage = args.join(" ");
// this deletes the user message and ignores the error it generates.
message.delete().catch(O_o=>{});
//sends the message you sent.
message.channel.send(says)
}
if(command == 'playstatus'){
// like I said, blocks other users.
if(message.author.id !== ownerid) return message.reply("sorry you cant do that");
// same thing as sayMessage
const playStatus = args.join(" ");
// sets the playstatus
bot.user.setActivity(`${playStatus} | any thing else you wanna add.`)
// guess what this does?
message.reply("sure thing.")
}
});
bot.login("your-veryepic&token");