【发布时间】: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 => ...部分。console.log(message.content);之后不小心关闭了吗? -
});之后的结束console.log(message.content);应该移动到client.login(token)之前。 -
谢谢,成功了!
-
不客气!
标签: javascript node.js discord discord.js