[英]How do I make my discord bot auto respond to a message?
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 事件并使用 if/else 在评估命令或消息之间切换。
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/但是,如果您有很多命令,那么将命令编写为单独的文件可能是值得的: https : //discordjs.guide/command-handling/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.