gpt4 book ai didi

javascript - Discord.js 修复了 V14 的 TypeError [ClientMissingIntents] : Valid intents must be provided for the Client but it is still having the same problem

转载 作者:行者123 更新时间:2023-12-02 05:48:02 24 4
gpt4 key购买 nike

我是 discord.js 编程的新手。我一直在尝试实现一个能够拥有 chatgpt 聊天机器人和音乐机器人的 discord 机器人。我有 FLAGS 意图的原因是我的音乐机器人功能。非标记意图适用于 ChatGPT。好像是我把他们都加进去后,问题就出现了。TypeError [ClientMissingIntents]:必须为客户端提供有效的意图。在 Client._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)在 Module._compile (node:internal/modules/cjs/loader:1246:14)在 Module.load (node:internal/modules/cjs/loader:1103:32)在 Module._load (node:internal/modules/cjs/loader:942:12)在 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)在 Node :内部/主/run_main_module:23:47 {代码:'ClientMissingIntents'

这是我的代码。 (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. intent 更改为 gatewayintentbits
  2. INTERACTION_CREATE 更改为 interactionCreate
  3. 将 node.js 更新到最新版本(版本 19)
  4. 将 discord.js 更新到最新版本(版本 14)
  5. 询问了 chatgpt,说这与意图有关。还是死胡同哈哈哈
  6. 检查其他 QnA 没有帮助

我做了什么?

  1. 在终端中,我输入了 node .然后它显示,代码:'ClientMissingIntents'

这是完整的包文件。

{
"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"
}
}

最佳答案

在从 v13 到 v14 的变化中,选择意图的方式发生了变化,您已经使用了这两种方法。

GatewayIntentBits.FLAGS.GUILDS,是老办法。

你走在正确的轨道上,因为你也有新的方法,GatewayIntentBits.Guilds 是正确的语法。

您需要做的就是删除前三个意图,因为它们使用的是过时的方法。

如果您要将项目从 v13 升级到 v14,则可以使用本指南帮助您将代码迁移到新方法。 https://discordjs.guide/additional-info/changes-in-v14.html

关于javascript - Discord.js 修复了 V14 的 TypeError [ClientMissingIntents] : Valid intents must be provided for the Client but it is still having the same problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75462534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com