【问题标题】:message is not defined Discord.js消息未定义 Discord.js
【发布时间】:2021-06-20 09:50:56
【问题描述】:

请帮忙,我是 discord.js 的初学者。这是我的 index.js:

const Discord = require("discord.js");
const config = require("./config.json");
const { prefix, token } = require("./config.json");
const fs = require("fs");

const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFolders = fs.readdirSync("./commands");

client.once("ready", () => {
    console.log("Ready!");
});

client.on("message", (message) => {
    console.log(message.content);
});

const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();

if (!client.commands.has(commandName)) return;

try {
    command.execute(message, args);
} catch (error) {
    console.error(error);
    message.reply("there was an error trying to execute that command!");
}

const commandFiles = fs.readdirSync("./commands").filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
    const command = require(`./commands/${folder}/${file}`);
    client.commands.set(command.name, command);
}

client.login(token);

这里有错误信息:

const args = message.content.slice(prefix.length).trim().split(/ +/);
             ^

    ReferenceError: message is not defined
        at Object.<anonymous> (C:\Users\Rodrigo\Desktop\HxH\index.js:18:14)
        at Module._compile (node:internal/modules/cjs/loader:1092:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
        at Module.load (node:internal/modules/cjs/loader:972:32)
        at Function.Module._load (node:internal/modules/cjs/loader:813:14)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
        at node:internal/main/run_main_module:17:47

感谢观看。

【问题讨论】:

  • 它没有被定义,因为你不在client.on('message', message =&gt; ... 部分。 console.log(message.content);之后不小心关闭了吗?
  • }); 之后的结束 console.log(message.content); 应该移动到 client.login(token) 之前。
  • 谢谢,成功了!
  • 不客气!

标签: javascript node.js discord discord.js


【解决方案1】:

我相信这是因为所有的消息代码都在你之外

client.on("message", (message) => {
    
});

声明。 message 是一个在 client.on 消息语句中生成的变量,所以它都必须在那里。

【讨论】:

    【解决方案2】:

    出现此错误是因为您在其他代码之前关闭了“})”

    您应该删除 }) 或在 }) 关闭之前放入代码

    【讨论】:

    • Obrigado, mas tô sem o código já kkkk
    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2018-10-23
    • 1970-01-01
    • 2020-04-22
    • 2020-09-10
    • 2019-07-30
    相关资源
    最近更新 更多