【问题标题】:How do I fix CLIENT_MISSING_INTENTS error when creating a Discord Price Bot?创建 Discord Price Bot 时如何修复 CLIENT_MISSING_INTENTS 错误?
【发布时间】:2022-02-03 21:13:47
【问题描述】:

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

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 (node:internal/modules/cjs/loader:1101:14) 在 Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) 在 Module.load (node:internal/modules/cjs/loader:981:32) 在 Function.Module._load (node:internal/modules/cjs/loader:822:12) 在 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) 在节点:内部/主/运行主模块: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 price


【解决方案1】:

这是一个简单的修复!

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

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

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

我建议您创建某种配置文件夹,并将您的私人信息保存在其中,就像您的机器人令牌一样。

【讨论】:

  • 这会替换已经存在的代码吗? (const Discord = require('discord.js') ---> const { Discord, Intents } = require('discord.js') client = new Client({ intents: 32767 });。因为它现在吐出一个不同的错误说“未定义客户端”
  • 是的,将其替换为您所拥有的。我将针对您现在遇到的错误编辑我的消息
  • 当你不需要所有的意图时,你为什么要指定所有的意图。
  • 这是一个例子,他们可以通过指定他们需要的意图来改变它们,比如"GUILD_PRESENCES", "GUILD_MESSAGES", "GUILD_BANS"等等......
猜你喜欢
  • 2021-10-14
  • 2021-03-12
  • 2021-10-12
  • 2021-10-15
  • 1970-01-01
  • 2020-06-25
  • 2022-11-06
  • 2020-09-07
  • 1970-01-01
相关资源
最近更新 更多