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

Discord.js中message.reply函数无法正常工作的问题求助

解决Discord.js中message.reply无法工作的问题

问题根源

你的代码存在两个关键问题导致机器人无法回复消息:

  • 未实际调用message.reply()方法,仅在控制台输出内容
  • 缺少监听消息所需的网关意图,Discord不会推送消息事件给机器人

修正后的代码

// Require the necessary discord.js classes
const { Client, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance - 添加消息相关意图
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent
  ] 
});

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('The Bot is ready');
});

client.on('messageCreate', (message) => {
    if(message.content === 'hello') {
        // 调用reply方法发送回复
        message.reply('hello buddy');
        console.log('已发送回复: hello buddy');
    }
})

// Login to Discord with your client's token
client.login(token);

关键修改说明

  • 添加网关意图:新增GuildMessages(允许接收服务器内的消息事件)和MessageContent(允许读取消息内容),这两个是监听并回复消息必须的权限
  • 替换控制台输出为reply调用:把console.log('hello buddy')改成message.reply('hello buddy'),让机器人实际发送消息到频道

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 11:24:10