如何使机器人根据反应删除一条消息并将其发布到另一个渠道

时间:2019-06-09 02:34:14

标签: discord.js commando

我正在尝试做到这一点,因此当我的机器人在特定频道中拾取某个反应时,它将查看它是否在特定反应中击中了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.\`\`\``)
}})

不知道该怎么做。

预期结果:Bot会查看帖子是否有10条反应,然后将其删除并将同一条消息带到另一个频道 实际结果:发生错误Cannot read 'channel' property

1 个答案:

答案 0 :(得分:0)

首先,我想说的是this one之类的问题与您搜索的内容有关。

此外,不和谐的文档和指南提供了awaiting reactions section

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

但是,这是您要执行的操作的完整示例。您可以向诺言添加计时器,而不仅仅是等待。我没有使用反应收集器,因为Promise更快,但是您也可以创建多个收集器的管理系统,或者使用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消息(我不知道您希望机器人如何发送建议消息,所以我以自己的方式)。
然后,该漫游器会将消息发送到特定频道,等待N个人添加反应,然后将消息发送到另一个频道。