Discord.js建议通道代码不起作用

时间:2020-10-04 18:22:59

标签: discord.js

const emojiChannelID = '762366483622789170';
client.on('ready', async () => {
  try {
    const channel = client.channels.cache.get(emojiChannelID);
    if (!channel) return console.error('Invalid ID or missing channel.');

    const messages = await channel.messages.fetch({ limit: 100 });

    for (const [id, message] of messages) {
      await message.react('<yes:762371315239485480>');
      await message.react('<no:762371418867761172>');
    }
  } catch(err) {
    console.error(err);
  }
});

使用最新的或至少接近最新版本的Discord.js

没有显示错误,但是在频道中它对消息没有反应

1 个答案:

答案 0 :(得分:0)

您的当前代码仅会在机器人就绪后获得最后100条消息。如果您希望对收到的消息做出反应,则需要使用client.on("message")事件,该事件将在每次发送消息时触发。这是一个例子。

client.on("message", async(msg)=>{
   try {
      if(msg.channel.id!=="762366483622789170") return; // do not run outside suggestions channel
      await msg.react("762371315239485480"); // react with yes
      await msg.react("762371418867761172"); // react with no
   } catch (e) {
      console.error(e);
   }
}