Discord.js 14 F用于媒体频道 - 发布新线程时,在另一个频道中发布消息

问题描述 投票:0回答:1

我的 Discord 中有多个媒体频道。每个都基于不同的主题。 我试图让它到达以下位置:如果在媒体频道中发布了新帖子,则文本频道会收到一条消息,其中包含新媒体的链接。

我为此使用了地图,因为每个媒体频道也将有自己的通知频道。

这是尝试的脚本:

const gameMediaMap = new Map();
gameMediaMap.set('1236005955464986745', '1234209500437680260');

client.on('messageCreate', async (message) => {
    if (gameMediaMap.has(message.channel.id)) {
        const gameNotifierChannelId = gameMediaMap.get(message.channel.id);
        const gameNotifierChannel = await client.channels.fetch(gameNotifierChannelId);

        if (gameNotifierChannel) {
            const postTags = message.content.match(/#[\w-]+/g) || []; // Extract hashtags
            const postTagsString = postTags.join(', ');

            const messageUrl = `https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.id}`;
            const notificationMessage = `A new clip was posted in <#${message.channel.id}>. [Go to clip](${messageUrl}).\nVideo tagged with: '${postTagsString}'.`;

            gameNotifierChannel.send(notificationMessage);
        }
    }
});

因为发布新帖子时没有任何反应,但附加的机器人的其余部分可以完美地工作。

有什么建议或想法吗?

discord discord.js
1个回答
0
投票

找到了解决方案。需要使用 threadCreate 而不是 messageCreate

// Mapping of game media channels to their corresponding notifier channels
const gameMediaMap = new Map([ // Media, Notification
    ['1236006027955146792', '1134296339790970922'], // 7 Days to Die
    ['1157136842047623169', '1135347403046801480'], // Battlebit Remastered
    ['1158427246319648879', '1158292907153969152'], // Battlefield 4
    ['1236006184293630044', '1178481743309836328'], // Call of Duty
    ['1236006284562665573', '1232691826352324618'], // Escape from Tarkov
    ['1236007603839172658', '1197342057711276032'], // Farming Simulator 22
    ['1236005955464986745', '1234209500437680260'], // Gray Zone Warfare
    ['1236006450996711537', '12335729887425373690'], // Ground Branch
    ['1236006385272225823', '1234212407551983727'], // Helldivers 2
    ['1236006511092695080', '1135347218296098867'] // Minecraft
]);

client.on('threadCreate', async thread => {
    if (thread.parentId && gameMediaMap.has(thread.parentId)) {
        const notifierChannelId = gameMediaMap.get(thread.parentId);
        const gameNotifierChannel = await client.channels.fetch(notifierChannelId);

        if (gameNotifierChannel.isTextBased()) {
            const messageUrl = `<https://discordapp.com/channels/${thread.guildId}/${thread.parentId}/${thread.id}>`;
            const postTitle = thread.name || "Unnamed Post"; // Assuming thread name is the post title.
            const author = thread.guild.members.cache.get(thread.ownerId) || await thread.guild.members.fetch(thread.ownerId).catch(() => null);
            const authorName = author ? (author.nickname || author.user.username) : "Unknown User";
            
            // Handling tags with backticks
            const tags = thread.appliedTags
                .map(s => thread.parent.availableTags.find(t => t.id === s))
                .map(x => `\`${x.name}\``) // Surrounding each tag with backticks
                .join(", ");
            const tagsMessage = tags ? `🏷️ Tags: ${tags}` : "Tags: None";

            const message = `🎥 A new clip was posted in <#${thread.parentId}> by **${authorName}**. 🎥\n[Go to clip - ${postTitle}](${messageUrl})\n${tagsMessage}`;
            gameNotifierChannel.send(message);
        }
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.