What I am trying to do is set up an autoresponse discord bot that replies if someone says "I win". I cannot figure out why it is not showing up with any responses in discord. I have attached an image of what it is doing.
const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();
client.login(config.BOT_TOKEN);
const prefix = '+';
client.on('message', function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const commandBody = message.content.slice(prefix.length);
const args = commandBody.split(' ');
const command = args.shift().toLowerCase();
if (command === 'ping') {
const timeTaken = Date.now() - message.createdTimestamp;
message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
}
if (command === 'cat') {
message.reply(`https://media.giphy.com/media/ExN8bEghwc8Ced5Yss/giphy.gif`);
}
if (command === 'cat2') {
message.reply(`image`);
}
if (command === 'trashcan') {
message.reply(
`https://www.trashcanswarehouse.com/assets/images/product-photos/witt/wcd24cl.jpg`
);
}
if (command === 'trashcan2') {
message.reply(
`https://marinedebris.noaa.gov/sites/default/files/styles/max-width600/public/IMG_1187_0.JPG?itok=iFHb98S3`
);
}
if (command === 'rock') {
var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
var rps = Math.floor(Math.random() * rockpaperscissors.length);
message.channel.send(rockpaperscissors[rps]);
}
if (command === 'paper') {
var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
var rps = Math.floor(Math.random() * rockpaperscissors.length);
message.channel.send(rockpaperscissors[rps]);
}
if (command === 'scissors') {
var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
var rps = Math.floor(Math.random() * rockpaperscissors.length);
message.channel.send(rockpaperscissors[rps]);
}
client.on('message', (message) => {
// If message is i win
if (message.content === 'i win') {
// Send no you dont back
message.channel.send('no you dont');
}
});
});
That is the code that I currently have.
client.on('message', (message) => {
// If message is i win
if (message.content === 'i win') {
// Send no you dont back
message.channel.send('no you dont');
}
});
Is the code that will not run out of all of it. It says no you don't after someone says I win. (supposedly)
On second read, it appears to me that your second client hook for evaluating messages is within the first:
//...
client.on('message', function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const commandBody = message.content.slice(prefix.length);
const args = commandBody.split(' ');
const command = args.shift().toLowerCase();
if (command === 'ping') {
const timeTaken = Date.now() - message.createdTimestamp;
message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
}
//...
client.on('message', (message) => {
// If message is i win
if (message.content === 'i win') {
// Send no you dont back
message.channel.send('no you dont');
}
});
});
It should not be this way (consider: how do you register an event handling function during an event handling function?). You have a few options:
The first, simplest would just be to remove the second client.on event and use if/else to switch between evaluating for commands or messages.
client.on("message", function (message) {
if (message.author.bot) return;
if (message.content.startsWith(prefix)) {
let command = // parse prefix out, normalize, etc
if (command === "ping") {
// ...
}
} else {
if (message.content === "i win") {
// ...
}
}
});
However, if you are going to have a lot of commands, it might be worth it to look into writing commands as individual files: https://discordjs.guide/command-handling/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.