示例代码:
const Discord = require('discord.js'); // const Discord = require('discord.js'); // using Intents class const client = new Discord.Client(); client.on('message', (msg) => { // 当用户编写特定的命令时,发回一个回复。 if (msg.content === '!cjavapy') { msg.reply('www.cjavapy.com!'); } });
报错信息:
[nodemon] 2.0.11 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): *.* [nodemon] watching extensions: js mjs, json [nodemon] starting " node app. js F: BotsVHello World BotInode_ modules discord . jsIsrcclientclient. js :544 throw new TypeError( CLIENT_MISSING_INTENTS'); TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client . at client._validateOptions (F: Bots)Hello World Bot)node modulesdiscord js)srcclientclient. js:544:13) at new client (F:BotsVHello World BotVnode modulesdiscord. jssrcclientclien js:73:10) at 0bject. <anonymous> (F: BotsHello World Bot)app. js: 54:16) at Module. compile ( internal/modules/cjs/loaer js :1085:14) at 0bject · Module.- extensions. . js (internal/ modules/cjs/loader . js :1114:10) 1 at Module. load (internal/modules/cjs/a . js:950:32) at Function . Module._load ( internal/modules/cjs/loader . js :790:14) at Function · executeUserEntryPoint [as runMain] (internal/ modules/run main. js:76:12) at internal/main/run main_ module. js:17:47 { [Symbol(code)]: CLIENT_MISSING_INTENTS' [nodemon] app crashed - waiting for file changes before starting.
解决方法:
将const client = new Discord.Client();
代码用const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
代码。
如果还报错可能需要将node.js
更新到v16版本,可以参考命令如下:
npm install node@16
参考文档:
https://discordjs.guide/popular-topics/intents.html#privileged-intents