You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord Bot未响应!ping命令问题求助

问题分析与修复方案

核心问题点

  • 未定义config变量却引用config.prefix:代码中声明了const prefix = '!',但判断消息前缀、截取内容时错误使用了未定义的config.prefix,会触发ReferenceError,导致命令逻辑无法执行。
  • 使用废弃事件:Discord.js v14+版本已移除message事件,需改用messageCreate事件监听消息。
  • 额外检查:需在Discord开发者平台的机器人设置中,开启Message Content Intent权限,否则机器人无法读取消息内容。

修正后的代码

const Discord = require('discord.js')

const client = new Discord.Client({intents: 32767});

const prefix = '!';
 
client.on('messageCreate', message => {
    if(!message.content.startsWith(prefix) || message.author.bot) return;
  
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
  
    if(command === 'ping'){
        message.channel.send('pong!');
    }
})

client.once('ready', () => {
    console.log('TixyBot is online!');
});

client.login('你的机器人Token')

内容的提问来源于stack exchange,提问作者Tixy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:30:46