【问题标题】:Discord bot command not working returning errorDiscord bot命令不起作用返回错误
【发布时间】:2021-05-26 21:02:33
【问题描述】:

所以我正在尝试开始学习如何编写 Discord 机器人。 但是当我运行代码并说命令“!hello” 它只是说这个错误信息:

Uncaught ReferenceError: hello is not defined 没有可用的调试器,无法发送“变量” 进程以代码 1 退出

我该如何解决?

const { Client, MessageAttachment } = require("discord.js");
const bot = new Client();

const token = new String("")

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

bot.on('message', (message) =>{

    console.log("HELLO");
    
    if(message.author.bot) return;
    let prefix = '!';
    let MessageArray = message.content.split(' ');

    let cmd = MessageArray[0].slice(prefix.length);
    let args = MessageArray.slice(1);

    if(!message.content.startsWith(prefix)) return;

    //command

    if(cmd === hello){
        console.log("command sent");
        message.channel.send("Hello there!");
    }
})

bot.login(token censored)

【问题讨论】:

  • 缺少引号,应该是cmd === 'hello'。没有hello 变量。
  • 我意识到我忘记了顶部。我已经放了“const { Client, MessageAttachment } = require("discord.js")”

标签: javascript discord discord.js


【解决方案1】:

在你的代码中,你有

if(cmd === hello){

但是,hello 不是您在代码中定义的变量,因此错误指出您没有定义它,而是应该将它放在引号中以表示它应该将 cmd 与“hello "(一个字符串)。

例如:

if(cmd === "hello"){

【讨论】:

    猜你喜欢
    • 2020-08-16
    • 2021-01-05
    • 2020-08-07
    • 2021-01-02
    • 2018-09-17
    • 2021-04-30
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多