问题描述
我正在编写一个不和谐的机器人.当我尝试运行节点时.在 cmd 我得到这个:
I am programming a discord bot. When I try to run node. in the cmd I get this:
C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcclientClient.js:544抛出新的 TypeError('CLIENT_MISSING_INTENTS');^
TypeError [CLIENT_MISSING_INTENTS]:必须为客户端.在 Client._validateOptions (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:544:13)在新客户端 (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:73:10)在对象.(C:UsersutenteDesktopouncerBotmain.js:2:16) ←[90m atModule._compile (internal/modules/cjs/loader.js:1072:14)←[39m ←[90m
在 Object.Module._extensions..js(internal/modules/cjs/loader.js:1101:10)←[39m ←[90m at Module.load(内部/模块/cjs/loader.js:937:32)←[39m ←[90m atFunction.Module._load (internal/modules/cjs/loader.js:778:12)←[39m←[90m at Function.executeUserEntryPoint [as runMain](内部/模块/run_main.js:76:12)←[39m ←[90m at内部/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(代码)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m }
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided forthe Client.at Client._validateOptions (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:544:13)at new Client (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:73:10)at Object. (C:UsersutenteDesktopouncerBotmain.js:2:16) ←[90m atModule._compile (internal/modules/cjs/loader.js:1072:14)←[39m ←[90m
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1101:10)←[39m ←[90m at Module.load(internal/modules/cjs/loader.js:937:32)←[39m ←[90m atFunction.Module._load (internal/modules/cjs/loader.js:778:12)←[39m←[90m at Function.executeUserEntryPoint [as runMain](internal/modules/run_main.js:76:12)←[39m ←[90m atinternal/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m }
C:UsersutenteDesktopouncerBot>节点.C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcclientClient.js:544抛出新的 TypeError('CLIENT_MISSING_INTENTS');^
C:UsersutenteDesktopouncerBot>node .C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcclientClient.js:544throw new TypeError('CLIENT_MISSING_INTENTS');^
TypeError [CLIENT_MISSING_INTENTS]:必须为客户端.在 Client._validateOptions (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:544:13)在新客户端 (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:73:10)在对象.(C:UsersutenteDesktopouncerBotmain.js:3:16) ←[90m atModule._compile (internal/modules/cjs/loader.js:1072:14)←[39m ←[90m
在 Object.Module._extensions..js(internal/modules/cjs/loader.js:1101:10)←[39m ←[90m at Module.load(内部/模块/cjs/loader.js:937:32)←[39m ←[90m atFunction.Module._load (internal/modules/cjs/loader.js:778:12)←[39m←[90m at Function.executeUserEntryPoint [as runMain](内部/模块/run_main.js:76:12)←[39m ←[90m at内部/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(代码)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided forthe Client.at Client._validateOptions (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:544:13)at new Client (C:UsersutenteDesktopouncerBotode_modules←[4mdiscord.js←[24msrcclientClient.js:73:10)at Object. (C:UsersutenteDesktopouncerBotmain.js:3:16) ←[90m atModule._compile (internal/modules/cjs/loader.js:1072:14)←[39m ←[90m
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1101:10)←[39m ←[90m at Module.load(internal/modules/cjs/loader.js:937:32)←[39m ←[90m atFunction.Module._load (internal/modules/cjs/loader.js:778:12)←[39m←[90m at Function.executeUserEntryPoint [as runMain](internal/modules/run_main.js:76:12)←[39m ←[90m atinternal/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m
我试图在网上搜索解决方案,但一无所获.我还在机器人设置中激活了以下药水,但没有任何变化:
I tried to search online for solutions but found nothing. I also activated the following potions in the bot settings but nothing changes:
这是我的代码,即使它不是错误的原因:
This is my code, even if it is not the cause of the error:
Main.js:
const Discord = require('discord.js');
const fs = require('fs');
const client = new Discord.Client();
let rawdata = fs.readFileSync('config.json');
let config = JSON.parse(rawdata);
const TOKEN = config.botToken
const prefix = config.prefix
client.login(TOKEN)
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/ +/)
const command = args[1].toLowerCase()
console.log(args)
//Command test!
})
client.once('ready', () => {
console.log("Discord bot online")
});
config.json:
config.json:
{
"botToken":"",
"prefix":"!pgc"
}
我尝试更新代码如下:
const Discord = require('discord.js');
const fs = require('fs');
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
let rawdata = fs.readFileSync('config.json');
let config = JSON.parse(rawdata);
const TOKEN = config.botToken
const prefix = config.prefix
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/ +/)
const command = args[1].toLowerCase()
console.log(args)
//Command test!
})
client.once('ready', () => {
console.log("Discord bot online")
});
client.login(TOKEN)
但我仍然收到错误:
(node:12216) UnhandledPromiseRejectionWarning: ReferenceError:未定义 AbortController在 RequestHandler.execute (C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcestRequestHandler.js:172:15)在 RequestHandler.execute (C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcestRequestHandler.js:176:19)在 RequestHandler.push (C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcestRequestHandler.js:50:25)在异步 WebSocketManager.connect (C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:128:9)在异步 Client.login (C:UsersutenteDesktopouncerBotode_modulesdiscord.jssrcclientClient.js:245:7)(使用 node --trace-warnings ...
显示警告的位置创建)(节点:12216)UnhandledPromiseRejectionWarning:未处理承诺拒绝.此错误源于抛出内部一个没有 catch 块的异步函数,或者通过拒绝一个承诺.catch() 没有处理.终止节点进程未处理的承诺拒绝,使用 CLI 标志--unhandled-rejections=strict
(见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).(拒绝 ID:2)(节点:12216)[DEP0018] DeprecationWarning:未处理不推荐使用承诺拒绝.在未来,承诺拒绝未处理的将终止 Node.js 进程非零退出代码.
推荐答案
首先,永远不要显示你的机器人的令牌,或者稍后尝试更改它.
First of all, never show a token of your bot, or try to change it later.
其次:
尝试使用它.由于新更新了 discord.js,如 ^13.0 版,您必须指定客户端意图:
Try using this. Since new updated of discord.js like version ^13.0 you have to specify client intents:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
显然,将 client.login(TOKEN)
放在最底部.
And obviously, put client.login(TOKEN)
at the very bottom.
有关新版本 discord.js 所需的更多更新,请单击此处
For more updates required from new version of discord.js click here
这篇关于Discord Api 必须为客户端提供有效意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!