【发布时间】: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)
})
【问题讨论】:
-
嘿,你的机器人秘密令牌应该是秘密的!我强烈建议您在 discord 仪表板上重新生成它。
-
这能回答你的问题吗? How do I fix CLIENT_MISSING_INTENTS error?
标签: node.js discord.js bots price