Discord.js消息未定义

时间:2017-12-27 14:59:29

标签: node.js discord.js

const Discord = require('discord.js');
const testBot = new Discord.Client();
const config = require("./config.json");
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();


testBot.on("message", (message) => {

    if(command === 'help') {
        message.channel.send('ok');
      } else
      if (command === 'hey') {
        message.channel.send('yes?');
      }

});

testBot.on('ready', () => {
  console.log('I am ready!');
})


testBot.login("Secret");

我是node.js的初学者。

错误就是这个。 'ReferenceError:消息未定义'我该如何解决?

谢谢。

2 个答案:

答案 0 :(得分:1)

我认为这是因为您的变量消息尚未声明

你可以尝试这样的事吗?

const Discord = require('discord.js');
const testBot = new Discord.Client();
const config = require("./config.json");

testBot.on("message", (message) => {
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    if(command === 'help') {
        message.channel.send('ok');
      } else
      if (command === 'hey') {
        message.channel.send('yes?');
      }
});

testBot.on('ready', () => {
  console.log('I am ready!');
})


testBot.login("Secret");

希望它有所帮助。

答案 1 :(得分:0)

嘿,我想你应该放

testBot.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.client) return;
    
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
相关问题