有没有办法对使用 discord.js 发送的每条消息做出反应 [英] Is there a way to react to every message ever sent with discord.js

查看:17
本文介绍了有没有办法对使用 discord.js 发送的每条消息做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个机器人来使用 discord.js f.e. 对频道中的每条消息做出反应.我有一个表情符号竞赛频道,我想在其中的每个帖子上添加 ✅ 和 ✖ 反应ofc,所有不必要的消息都被清理,所以有大约 50 条消息

I wanna use an bot to react to every single message in an channel using discord.js f.e. i got an emoji contest channel and i wanna ad an ✅ and an ✖ reaction on every post in there ofc, all the unnecesary messages are cleaned up so that there are like 50 messages

推荐答案

  • 使用 获取已在频道中发送的消息TextChannel.fetchMessages().
  • 遍历Collection.
  • 使用 Message.react 添加反应().
  • 当频道中发送新消息时,您还应该添加反应.
  • const emojiChannelID = 'ChannelIDHere';
    

    client.on('ready', async () => {
      try {
        const channel = client.channels.get(emojiChannelID);
        if (!channel) return console.error('Invalid ID or missing channel.');
    
        const messages = await channel.fetchMessages({ limit: 100 });
    
        for (const [id, message] of messages) {
          await message.react('✅');
          await message.react('✖');
        }
      } catch(err) {
        console.error(err);
      }
    });
    

    client.on('message', async message => {
      if (message.channel.id === emojiChannelID) {
        try {
          await message.react('✅');
          await message.react('✖');
        } catch(err) {
          console.error(err);
        }
      }
    });
    

    在这段代码中,您会注意到我使用了 for...of 循环而不是 Map.forEach().这背后的原因是后者将简单地调用方法并继续前进.这将导致任何被拒绝的承诺 not 被捕获.我也使用过 async/await 样式而不是 then() 链,这很容易变得混乱.

    In this code, you'll notice I'm using a for...of loop rather than Map.forEach(). The reasoning behind this is that the latter will simply call the methods and move on. This would cause any rejected promises not to be caught. I've also used async/await style rather than then() chains which could easily get messy.

    这篇关于有没有办法对使用 discord.js 发送的每条消息做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆