Discord Bot无法设置GWEI活动状态问题求助
Discord Bot无法显示以太坊Gas价格活动状态的解决方案
问题描述
开发Discord Bot时,期望Bot的活动状态显示当前以太坊Gas价格(GWEI单位),终端能正常输出正确的GWEI数值,但Bot始终无法设置活动状态,无任何状态显示。
原始代码
const Discord = require('discord.js'); const { createAlchemyWeb3 } = require('@alch/alchemy-web3'); const client = new Client({ intents: Discord.GatewayIntentBits.GUILD_MESSAGES | Discord.GatewayIntentBits.GUILDS }); const API_URL = 'https://eth-mainnet.alchemyapi.io/v2/API-key'; const web3 = createAlchemyWeb3(API_URL); const updateBotActivity = async () => { try { const gwei = await web3.eth.getGasPrice() / 10 ** 9; // Convert to GWEI if (gwei > 0) { client.user.setActivity(`${gwei.toFixed(2)} GWEI`, { type: 'WATCHING' }); console.log(`${gwei.toFixed(2)} GWEI`); } else { console.log('GWEI value is not valid'); } } catch (err) { console.error(err); } }; client.on('ready', () => { console.log(`Logged in as ${client.user.tag}`); updateBotActivity(); // Initial update setInterval(updateBotActivity, 300000); // 5-minute interval });
问题排查与修复
1. Client实例化错误
代码中直接使用new Client(),但你是通过const Discord = require('discord.js')导入的模块,Client是Discord对象下的属性,正确的实例化方式应该是:
const client = new Discord.Client({ intents: Discord.GatewayIntentBits.GUILD_MESSAGES | Discord.GatewayIntentBits.GUILDS });
或者改用解构导入简化代码:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [GatewayIntentBits.GUILD_MESSAGES, GatewayIntentBits.GUILDS] });
2. 确保client.user可用后再调用setActivity
虽然ready事件触发时客户端已登录,但异步操作可能存在时序问题,建议在更新方法里先做可用性检查:
const updateBotActivity = async () => { if (!client.user) return; // 客户端未就绪时直接返回 try { const gasPrice = await web3.eth.getGasPrice(); const gwei = Number(gasPrice) / 10 ** 9; // 显式转Number避免潜在类型问题 if (gwei > 0) { await client.user.setActivity(`${gwei.toFixed(2)} GWEI`, { type: 'WATCHING' }); console.log(`活动状态已更新: ${gwei.toFixed(2)} GWEI`); } else { console.log('无效的GWEI数值'); } } catch (err) { console.error('更新活动状态失败:', err); } };
3. 完整修复后的代码
const { Client, GatewayIntentBits } = require('discord.js'); const { createAlchemyWeb3 } = require('@alch/alchemy-web3'); const client = new Client({ intents: [GatewayIntentBits.GUILD_MESSAGES, GatewayIntentBits.GUILDS] }); const API_URL = 'https://eth-mainnet.alchemyapi.io/v2/你的API密钥'; const web3 = createAlchemyWeb3(API_URL); const updateBotActivity = async () => { if (!client.user) return; try { const gasPrice = await web3.eth.getGasPrice(); const gwei = Number(gasPrice) / 10 ** 9; if (gwei > 0) { await client.user.setActivity(`${gwei.toFixed(2)} GWEI`, { type: 'WATCHING' }); console.log(`活动状态已更新: ${gwei.toFixed(2)} GWEI`); } else { console.log('无效的GWEI数值'); } } catch (err) { console.error('更新活动状态出错:', err); } }; client.on('ready', () => { console.log(`已登录为 ${client.user.tag}`); updateBotActivity(); // 每5分钟更新一次状态 setInterval(updateBotActivity, 300000); }); // 务必替换为你的Bot令牌 client.login('你的Bot令牌');
额外提醒
- 替换代码中的
你的API密钥和你的Bot令牌为实际有效值 - 前往Discord开发者门户,确保Bot的
Presence Intent和Server Members Intent已开启,避免后续权限相关问题
内容的提问来源于stack exchange,提问作者deinedickemom
相关产品推荐
相关产品推荐

