NodeJS Discord.js修复了类型错误[客户端缺少意图]的V14:必须为客户端提供有效的意图,但它仍然存在相同的问题

kuuvgm7e  于 2023-02-18  发布在  Node.js
关注(0)|答案(1)|浏览(102)

我是discord.js编程新手。我一直在尝试实现一个discord机器人,它可以同时拥有chatgpt聊天机器人和音乐机器人。我之所以拥有FLAGS Intent是为了我的音乐机器人功能。非FLAGS Intent是为了ChatGPT。似乎在我添加了这两个Intent之后,问题发生了。TypeError [ClientMissingIntents]:必须为客户端提供有效的意图。在客户端。_validateOptions(C:\Users\x\Desktop\bot\node_modules\discord. js\src\client\Client. js:489:13)在新客户端(C:\Users\x\Desktop\bot\node_modules\discord. js\src\client\Client. js:78:10)在对象。(C:\Users\x\Desktop\bot\main. js:3:16)在模块。_compile(节点:internal/modules/cjs/loader:1246:14)在模块。加载(节点:internal/modules/cjs/loader:1103:32)在模块。_加载(节点:internal/modules/cjs/loader:942:12)在函数。执行用户入口点[as runMain](节点:internal/modules/run_main:83:12)在节点:internal/main"客户端缺少意图"}
下面是我的代码。(index.js文件)

const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { Client, GatewayIntentBits, Collection, Events} = require("discord.js");
const { Player } = require("discord-player");
const { token } = require('./config.json');

const fs = require("node:fs");
const path = require("node:path");

import { ChatGPT } from "discord-chat-gpt";

const client = new Client({
    intents: [
        GatewayIntentBits.FLAGS.GUILDS,
        GatewayIntentBits.FLAGS.GUILD_MESSAGES,
        GatewayIntentBits.FLAGS.GUILD_VOICE_STATES,
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ],
    allowedMentions: {
        repliedUser: false, //to not let it ping me for 1million times 
    },
});

const gptClient = new ChatGPT({
    apiKey: `insertapikeyhere`, //
    orgKey: `insertorgkeyhere`, // 
  });

// Checking if bot is online (logger)
client.on("ready", () => {
    console.log('> ${client.user.username}is online!');
});

// Chat Bot System
client.on('messageCreate', async (message) => {
    if(!message.guild || message.author.bot) return;
    let ChannelID = "insertchannelidhere";
    let channel = message.guild.channels.cache.get(ChannelID);
    if (!channel) return;
    if (message.channel.id === channel.id) {
        let msg = await message.reply({
            content: `Loading... Please Wait.`,
        });
        let reply = await gptClient.chat(message.content,message.author.username);
        msg.edit({
            content: `${reply}`,
        });
    }
});

import {Client, GatewayIntentBits} from "discord.js"

// Load all the commands
const commands = [];
client.commands = new Collection();

const commandsPath = path.join(__dirname,"conmmands");
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);
}

client.player = new Player(client, {
    ytdlOptions: {
        quality: "highestaudio",
        highWaterMark: 1 << 25 
    }
});

client.on("ready", async () => {
    const guild_ids = client.guilds.cache.map(guild => guild.id);

    const rest = new REST({ version: "9" }).setToken(token);
    for (const guildId of guild_ids) {
        rest.put(Routes.applicationGuildCommands(client.application.id, guildId), {
            body: commands,
        })
            .then(() => console.log(`Added commands to ${guildId}`))
            .catch(console.error);
    }

    // Set the application commands
    await client.application.commands.set(commands);
    console.log("Commands registered!");
});

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("Sorry, an error occuured while executing that command. :C")
    }
});

client.application.commands.set();
client.login(token);

我已经试着找到这个解决方案2个小时了,遗憾的是什么都没有。希望你
我试过了
1.将意图更改为网关意图位
1.已将交互创建更改为交互创建
1.将node.js更新为最新版本(版本19)
1.将discord.js更新为最新版本(版本14)
1.问了chatgpt,说跟故意有关,还是死路一条哈哈哈
1.检查其他QnA没有帮助
我做了什么?
1.在终端中,我输入node .,然后显示,代码:'客户端缺少意图'
这是完整的文件包。

{
  "naexme": "bot",
  "version": "1.0.0",
  "description": "discordbot",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "helyz",
  "license": "ISC",
  "dependencies": {
    "@discordjs/opus": "^0.9.0",
    "@discordjs/voice": "^0.14.0",
    "discord-chat-gpt": "^1.0.2",
    "discord-player": "^5.4.0",
    "discord.js": "^14.7.1",
    "dotenv": "^16.0.3",
    "ffmpeg-static": "^5.1.0"
  }
}
mzillmmw

mzillmmw1#

在从v13到v14的更改中,Intent的选择方式发生了变化,您使用了两种方法。

网关意图位.标志.指南,是执行此操作的旧方法。

您的思路是正确的,因为您也有了新的方法,GatewayIntentBits.Guilds是正确的语法。
您需要做的就是删除前三个Intent,因为它们使用的是过时的方法。
如果要将项目从v13升级到v14,可以使用本指南帮助您将代码迁移到新方法。https://discordjs.guide/additional-info/changes-in-v14.html

相关问题