javascript - Discord Api 必须为客户端提供有效意图

标签 javascript node.js discord.js

这个问题在这里已经有了答案:





How do I fix CLIENT_MISSING_INTENTS error?

(5 个回答)


12 个月前关闭。




我正在编写一个不和谐的机器人。当我尝试运行 Node 时。在 cmd 我得到这个:

C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\Client.js:544 throw new TypeError('CLIENT_MISSING_INTENTS'); ^

TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client. at Client._validateOptions (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:544:13) at new Client (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:73:10) at Object. (C:\Users\utente\Desktop\bouncerBot\main.js:2:16) ←[90m at Module._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 at Function.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 at internal/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m }

C:\Users\utente\Desktop\bouncerBot>node . C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\Client.js:544 throw new TypeError('CLIENT_MISSING_INTENTS'); ^

TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client. at Client._validateOptions (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:544:13) at new Client (C:\Users\utente\Desktop\bouncerBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:73:10) at Object. (C:\Users\utente\Desktop\bouncerBot\main.js:3:16) ←[90m at Module._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 at Function.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 at internal/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m


我试图在网上搜索解决方案,但一无所获。我还在机器人设置中激活了以下药水,但没有任何变化:
enter image description here
这是我的代码,即使它不是错误的原因:
主.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")
    
    });
配置.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 is not defined at RequestHandler.execute (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\rest\RequestHandler.js:172:15) at RequestHandler.execute (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\rest\RequestHandler.js:176:19) at RequestHandler.push (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\rest\RequestHandler.js:50:25) at async WebSocketManager.connect (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:128:9) at async Client.login (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\Client.js:245:7) (Use node --trace-warnings ... to show where the warning was created) (node:12216) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:12216) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

最佳答案

首先,永远不要显示你的机器人的 token ,或者稍后尝试更改它。
其次:
尝试使用这个。由于更新了 discord.js 之类的版本 ^13.0,您必须指定客户端意图:

const { Client, Intents } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
显然,把 client.login(TOKEN)在最底部。
有关新版本 discord.js 所需的更多更新,请单击 here

关于javascript - Discord Api 必须为客户端提供有效意图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68701446/

相关文章:

javascript - jQuery 使用 Prototype 对象扩展对象

javascript - 在 JavaAscript 中调用随机函数

javascript - 了解 Node.js 应用程序中的控制流

javascript - 在 TestCafe 中输出所有失败而不是一个失败

javascript - 从javascript中的字符串路径获取文件名?

javascript - Node.js : Make a response on the same page (update the page)

node.js - NodeJs POST 请求 ER_DATA_TOO_LONG

javascript - 如何让 Commando 机器人响应消息中任意位置包含特定子字符串的消息

javascript - 如何为嵌入制作随机颜色 Discord.js

javascript - 删除 channel 的困惑