[英]How do I make my discord bot auto respond to a message?
我想要做的是設置一個自動響應不和諧機器人,如果有人說“我贏了”,它就會回復。 我無法弄清楚為什么它沒有出現任何不和諧的反應。 我附上了一張它正在做什么的圖片。
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');
}
});
});
這就是我目前擁有的代碼。
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');
}
});
是不會全部用完的代碼。 有人說我贏了,它說不,你不知道。 (據說)
在第二次閱讀時,在我看來,您用於評估消息的第二個客戶端掛鈎在第一個中:
//...
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');
}
});
});
不應該這樣(考慮:如何在事件處理函數期間注冊事件處理函數?)。 您有幾個選擇:
第一個,最簡單的就是刪除第二個 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") {
// ...
}
}
});
但是,如果您有很多命令,那么將命令編寫為單獨的文件可能是值得的: https : //discordjs.guide/command-handling/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.