【问题标题】:How to make a bot that deletes a message and posts it in another channel based on reactions如何制作一个删除消息并根据反应将其发布到另一个频道的机器人
【发布时间】:2019-10-23 23:29:19
【问题描述】:

我正在努力做到这一点,所以当我的机器人在特定渠道中获得反应时,它会查看它是否在特定反应上达到 10 个反应。然后它会删除反应的消息并将其发布到另一个特定的频道,并附加一条消息。

这是代码

doopliss.on('message', message => {
  if (message.channel.id === "587066921422290953") {
    setTimeout(function(){
      message.react(message.guild.emojis.get('587070377696690177'))
    }, 10)
    setTimeout(function(){
      message.react(message.guild.emojis.get('587071853609353256'))
    }, 50)
    setTimeout(function(){
      message.react(message.guild.emojis.get('587070377704816640'))
    }, 100)
  }
});
const message = require("discord.js");
const emoji = require("discord.js");
const reaction = require("discord.js");
doopliss.on('message', message => {
  if (message.channel.id === "587066921422290953") {
  let limit = 2; // number of thumbsdown reactions you need
  if (message.reaction.emoji.name == message.guild.emojis.get('587070377696690177') 
  && reaction.count >= limit) message.reaction.message.delete();
  let tcontent = message.reaction.message.content
  let accept = message.guild.channels.get('587097086256873483')

  accept.send(`${tcontent} \`\`\`This server suggestion has been accepted by the community! Great job! Now a staff member just needs to forward it to username.\`\`\``)
}})

不知道怎么做。

预期结果:机器人查看帖子是否有 10 个反应,然后将其删除并将相同的消息带到不同的频道 实际结果:出现错误Cannot read 'channel' property

【问题讨论】:

    标签: discord.js commando


    【解决方案1】:

    首先我想说像this one 这样的问题有你搜索的内容。

    此外,discord 文档和指南提供了awaiting reactions section

    还有一些其他问题与指南中使用的主题或功能有关,通过搜索您甚至可以找到与您几乎相同的question like this one

    但是,这里有一个完整的示例来说明您想要做什么。你可以在 Promise 中添加一个计时器,而不是仅仅等待。我没有使用reaction collector,因为promise会快一点,但是你也可以创建一个多个collector的管理系统,或者使用client.on('messageReactionAdd')

    const Discord = require('discord.js');
    const config = require('./config.json');
    
    
    const channelSuggestion = '<channelID>';
    const channelSend = '<channelID>';
    const emojiReact = '<emojiID>';
    const prefixSuggestion = '!';
    const reactionMax = 11;
    
    const client = new Discord.Client();
    client.on('ready', () => {
      console.log('Starting!');
      client.user.setActivity(config.activity);
    });
    
    client.on('message', (msg) => {
      if ((msg.content[0] === prefixSuggestion) && (msg.channel.type === 'dm')){
        sendSuggestion(msg);
      }
    });
    
    
    function filter(reaction) {
      return reaction.emoji.id === emojiReact;
    }
    
    function moveSuggestion(msg) {
      client.channels.get(channelSend).send(msg.content)
        .then(() => msg.delete()).catch(err => console.log(error));
    }
    
    function sendSuggestion(msg){
      client.channels.get(channelSuggestion).send(msg.content.substr(1))
        .then((newMsg) => newMsg.react(emojiReact))
        .then((newMsgReaction) => 
          newMsgReaction.message
          .awaitReactions(filter, {max: reactionMax})//, time: 15000, errors: ['time']})
          .then(collected => {
            moveSuggestion(newMsgReaction.message);
          })
          // .catch(collected => {
          //   console.log(`After a minute, only ${collected.size} out of 10 reacted.`);
          // })
         );
    }
    
    client.login(config.token)
      .then(() => console.log("We're in!"))
      .catch((err) => console.log(err));
    
    

    机器人会收听以!开头的dm消息(我不知道你希望你的机器人如何发送建议消息,所以我自己做了)。
    然后bot会向特定频道发送消息,等待N人添加反应,然后将消息发送到另一个频道。

    【讨论】:

    • 它将在文本频道中,不需要前缀。它只会做出反应,如果达到 11 个反应(包括机器人),它会删除消息,将内容复制到另一个频道。当我用正确的变量 const channelSuggestion = 'id' 填充某些部分并更改一些内容以满足我的机器人前缀时,它不起作用。
    • @SomePerson 你是什么意思它没有工作?你能给我更多的细节吗?你到底改变了什么?有错误吗?
    • 这就是我所做的const channelSuggestion = '587097052324954132'; const channelSend = '587097086256873483'; const emojiReact = '587070377696690177'; const reactionMax = 2; 我将所有client 更改为doopliss 并将msg.channel.type 更改为text 并尝试将client.on 设置为doopliss.on('messageReactionAdd' 然后当我在channelSuggestion 发了一条消息,表情弹出,我对所有表情都做出了反应,但什么也没发生。
    • 您是否尝试了我的示例而不进行任何更改?您是否尝试记录正在发生的事情以了解它为什么不起作用?我的示例在频道中发送消息,使用 emot 做出反应并等待消息上的 N emot 它不适合用于反应如果您想选择任何消息而不是机器人发送的消息,您确实必须使用“messageReactionAdd”,但您必须修改sendSuggestion
    • 我必须将所有client 更改为doopliss,因为这是我正在使用的突击队客户端。另外,我必须删除创建新用户和登录名的部分代码。在代码中,它将一些变量(例如 collected)呈现为已声明但从未使用过的变量。我将message 更改为messageReactionAdd
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2017-09-02
    相关资源
    最近更新 更多