Discord Bot Intents相关错误排查与解决求助
Discord Bot开发中的两个错误及解决方法
问题概述
开发Discord Bot时先后遇到两个错误:
- 初始报错:
TypeError: Cannot read properties of undefined (reading 'FLAGS') - 添加错误代码后报错:
ReferenceError: intents is not defined
初始报错日志
TypeError: Cannot read properties of undefined (reading 'FLAGS') at Object.<anonymous> (C:\Users\aviai\OneDrive\Desktop\DiscordBot\index.js:13:23) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47
添加错误代码后的报错日志
ReferenceError: intents is not defined at Object.<anonymous> (C:\Users\aviai\OneDrive\Desktop\DiscordBot\index.js:10:1) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47
原始代码
require("dotenv").config(); const {REST} = require("@discordjs/rest"); const { Routes } = require("discord-api-types/v9"); const { Client, Intents, Collection } = require("discord.js"); const { Player } = require("discord-player"); const fs = require("node:fs"); const path = require("node:path"); intents.FLAGS.flagname; const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES] }); // Load all the commands const commands = []; client.commands = new Collection(); const commandsPath = path.joint(_dirname, "commands"); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js")); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); client.commands.set(command.data.name, command); command.push(command.data.ToJSON); } client.player = new Player(client, { ytdlOptions: { quality: "highestaudio", highWaterMark: 1 << 25 } }); client.on("ready", () => { const guild_ids = client.guilds.cache.map(guild => guild.id); const rest = new REST({version: "9"}).setToken(process.env.TOKEN); for(const guildId of guild_ids) { rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId), { body: commands }) .then(() => console.log(`Added commands to ${guildId}`)) .catch(console.error); } }); client.on("interactionCreate", async interaction => { if(!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if(!command) return; try { await command.execute({client, interaction}); } catch(err) { console.error(err); await interaction.reply("An error occured while executing that command."); } }); client.login(process.env.TOKEN);
错误原因及修复方案
1. 解决ReferenceError: intents is not defined
你代码中添加的intents.FLAGS.flagname;完全是多余且错误的:
- 从discord.js导入的是大写的
Intents,小写的intents从未定义过 - 直接删除这一行即可消除该报错
2. 解决初始的TypeError: Cannot read properties of undefined (reading 'FLAGS')
初始报错并非Intents.FLAGS本身的问题,而是代码中的拼写错误导致连锁反应:
- 错误1:
path.joint(_dirname, "commands")→ 改为path.join(__dirname, "commands")path模块的正确方法是join,不是joint- 当前目录变量是
__dirname(两个下划线),不是_dirname
- 错误2:
command.push(command.data.ToJSON)→ 改为commands.push(command.data.toJSON())- 要往定义好的
commands数组中添加数据,不是单个command对象 toJSON()是小写的方法,需要加括号调用
- 要往定义好的
修复后的完整代码
require("dotenv").config(); const { REST } = require("@discordjs/rest"); const { Routes } = require("discord-api-types/v9"); const { Client, Intents, Collection } = require("discord.js"); const { Player } = require("discord-player"); const fs = require("node:fs"); const path = require("node:path"); const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES ] }); // Load all the commands const commands = []; client.commands = new Collection(); const commandsPath = path.join(__dirname, "commands"); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js")); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); client.commands.set(command.data.name, command); commands.push(command.data.toJSON()); } client.player = new Player(client, { ytdlOptions: { quality: "highestaudio", highWaterMark: 1 << 25 } }); client.on("ready", () => { const guild_ids = client.guilds.cache.map(guild => guild.id); const rest = new REST({ version: "9" }).setToken(process.env.TOKEN); for (const guildId of guild_ids) { rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId), { body: commands }) .then(() => console.log(`Added commands to ${guildId}`)) .catch(console.error); } }); client.on("interactionCreate", async interaction => { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute({ client, interaction }); } catch (err) { console.error(err); await interaction.reply("An error occured while executing that command."); } }); client.login(process.env.TOKEN);
内容的提问来源于stack exchange,提问作者Kanashi
相关产品推荐
相关产品推荐

