创建 Discord 价格机器人时如何修复 CLIENT_MISSING_INTENTS 错误?

问题描述 投票:0回答:1

我是 JS 编程新手,正在尝试为我的 Discord 服务器制作一个加密价格机器人。我已经从另一个用户那里复制了代码并下载了所有需要的包。现在,每次我尝试实际节点项目时都会收到此错误:

TypeError [CLIENT_MISSING_INTENTS]:必须为客户端提供有效的意图。 在 Client._validateOptions (/Users/oliver/Desktop/Discord/node_modules/discord.js/src/client/Client.js:548:13) 在新客户端 (/Users/oliver/Desktop/Discord/node_modules/discord.js/src/client/Client.js:76:10) 在对象。 (/Users/oliver/Desktop/Discord/PriceBot.js:3:16) 在Module._compile(节点:内部/模块/cjs/loader:1101:14) 在 Object.Module._extensions..js (节点:内部/模块/cjs/loader:1153:10) 在Module.load(节点:内部/模块/cjs/loader:981:32) 在 Function.Module._load (节点:内部/模块/cjs/loader:822:12) 在 Function.executeUserEntryPoint [作为 runMain] (节点:内部/模块/run_main:81:12) 在节点:内部/主/run_main_module:17:47 { [符号(代码)]:'CLIENT_MISSING_INTENTS' }

这是我正在使用的代码。任何具体的改变将不胜感激,因为我是新手:

const axios = require('axios')
const Discord = require('discord.js')
const client = new Discord.Client()
// variables
const coinId = 'ethereum'; // https://api.coingecko.com/api/v3/coins/list
const guildId = 'Server_ID'; // Right click server icon on discord -> copy 
id
// const clientId = ''; // get it from your dev portal 
https://discord.com/developers/applications
const botSecret = 'Bot_ID'; // 
get it from your dev portal https://discord.com/developers/applications
// Invite bot to server:
// https://discord.com/api/oauth2/authorize? 
client_id=APP_ID&permissions=0&scope=bot%20applications.commands
function getPrices() {
// API for price data.
axios.get(`https://api.coingecko.com/api/v3/coins/markets? 
vs_currency=usd&ids=${coinId}`).then(res => {
// If we got a valid response
if(res.data && res.data[0].current_price && res.data[0].price_change_percentage_24h) 
{
let currentPrice = res.data[0].current_price || 0 // Default to zero
let priceChangePct = res.data[0].price_change_percentage_24h || 0 // Default to zero
let priceChange = res.data[0].price_change_24h || 0 // Default to zero
let symbol = res.data[0].symbol.toUpperCase() || '?'
client.user.setPresence({
game: {
// Example: "Watching -5,52% | BTC"
name: `${priceChange.toFixed(2)} (${priceChangePct.toFixed(2)}%)`,
type: 3 // Use activity type 3 which is "Watching"
}
})
console.log('Updated price to', currentPrice)
client.guilds.find(guild => guild.id === `${guildId}`).me.setNickname(`${symbol} 
$${(currentPrice).toLocaleString().replace(/,/g,',')}`)
}
else
console.log('Could not load player count data for', process.env.COIN_ID)
}).catch(err => console.log('Error at api.coingecko.com data:', err))
}
// Runs when client connects to Discord.
client.on('ready', () => {
console.log('Logged in as', client.user.tag)
getPrices() // Ping server once on startup
// Ping the server and set the new status message every x minutes. (Minimum of 1 
minute)
setInterval(getPrices, Math.max(1, 1 || 1) * 60 * 1000)
})
node.js discord.js bots
1个回答
-3
投票

这是一个简单的修复!

在不和谐的 v13 中,我们需要为我们的机器人指定意图,以便它们正常工作。您可以使用下面的代码,希望这可以解决您的问题!

const { Client, Discord, Intents } = require('discord.js')
client = new Client({ intents: 32767 });

还要确保重新生成你的机器人秘密令牌,因为你已经显示了它(这是为了确保其他人可以窃取它并使用它)。

我建议您创建某种配置文件夹,并将您的私人信息(例如机器人令牌)保存在其中。

© www.soinside.com 2019 - 2024. All rights reserved.