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

使用Discord.js 14获取论坛线程起始消息完整数据求助

获取Discord论坛线程起始消息并保存为数组

要获取论坛线程的第一条消息(即线程的起始帖),你需要调用Discord.js提供的fetchStarterMessage()方法——因为线程创建事件触发时,起始消息不一定已被客户端缓存。以下是修改后的可运行代码:

const { ChannelType } = require('discord.js');

client.on('threadCreate', async (thread) => {
    if (thread.type === ChannelType.GuildPublicThread) {
        try {
            // 获取线程的起始消息
            const starterMessage = await thread.fetchStarterMessage();
            
            // 提取所需消息数据,可按需调整字段
            const messageData = [
                {
                    messageId: starterMessage.id,
                    content: starterMessage.content,
                    author: {
                        id: starterMessage.author.id,
                        username: starterMessage.author.username
                    },
                    createTime: starterMessage.createdAt.toISOString(),
                    threadId: thread.id,
                    forumChannelId: thread.parentId
                }
            ];

            // 此处可添加将messageData通过API传递的逻辑
            console.log('已获取起始消息数据:', messageData);
        } catch (error) {
            console.error('获取起始消息失败:', error);
        }
    }
});

关键细节:

  • fetchStarterMessage()是异步方法,必须配合await调用,同时确保事件回调为async函数。
  • 你可以根据实际需求扩展messageData的字段,比如添加附件链接、嵌入内容等,直接从starterMessage对象中提取即可。
  • 用try/catch捕获异常,处理权限不足、起始消息已被删除等异常情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 13:27:03