消息未定义 Discord.js

时间:2021-03-23 17:29:45

标签: javascript node.js discord discord.js

请帮忙,我是 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

感谢您的观看。

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

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