【问题标题】:Discord.js message is not definedDiscord.js 消息未定义
【发布时间】:2020-11-06 23:25:23
【问题描述】:
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: message is not defined' 我该如何解决?

谢谢。

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

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

    你可以试试这样的东西吗?

    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");
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      嘿,我觉得你应该放

      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();
      

      【讨论】:

      • 很好的答案,但也要尝试包括它为什么起作用;在这种情况下,由于message的范围。
      猜你喜欢
      • 2020-06-27
      • 2021-06-20
      • 1970-01-01
      • 2021-06-03
      • 2018-10-23
      • 1970-01-01
      • 2020-04-22
      • 2020-09-10
      • 2019-07-30
      相关资源
      最近更新 更多