我目前在this tutorial的服务器上运行一个discord.js机器人。但是,当我将if语句用于侦听消息时,node.js拒绝启动它。说if语句:
if (message.content === '!ping') {
message.channel.send('Pong.');
}
编辑:
完整代码
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.login('login token AAAAA');
client.on('message', message => {
console.log(message.content);
});
if (message.content === '!ping') {
message.channel.send('Pong.');
}
引发错误
if (message.content === '!ping') {
^
ReferenceError: message is not defined
at Object.<anonymous> (C:\Users\epopova1\Desktop\poserBot\index.js:13:1)
[90m at Module._compile (internal/modules/cjs/loader.js:1133:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:977:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:877:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)[39m
[90m at internal/main/run_main_module.js:18:47[39m
很抱歉这个问题,我在这里是完全陌生的
答案 0 :(得分:0)
您需要将if
语句放入message
事件处理程序中,如下所示:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.login('login token AAAAA');
client.on('message', message => {
console.log(message.content);
if (message.content === '!ping') {
message.channel.send('Pong.');
}
});