user10294268
user10294268

Reputation:

Is there a way to detect msg reactions on a specific msg?

I'm making a new system on my server, i need a way to detect reactions on some msgs, so the user will react to run a function like send msg or something else...

I tried to use reaction collector, but i can't deal with it, this method will not gonna work with old msgs. I need something simple, to build my system on, so i can use different functions like message.channel.send or message.guild.createChannel, with the ability to detect the msg content, this one is very important.

Upvotes: 0

Views: 83

Answers (2)

slothiful
slothiful

Reputation: 5623

Reaction collectors can indeed work on messages that have already been sent prior to the client's login. However, these messages must be kept track of and fetched.

Example System (did not test, just an example)

messages.json:

[
  {
    "guild": "guildIDhere",
    "channel": "channelIDhere",
    "message": "messageIDhere",
    "filter": "(reaction, user) => reaction.emoji.name === '😄' && user.id !== client.user.id;",
    "options": { "max": 1 },
    "callback": "collected => console.log(collected);"
  }
]

index.js:

const messages = require('./messages.json'); // Path may vary
const fs = require('fs');

client.on('ready', () => {
  const deleteQueue = [];

  for (let i = 0; i < messages.length; i++) {
    const guild = client.guilds.get(messages[i].guild);
    if (!guild) {
      deleteQueue.push(i);
      continue;
    }

    const channel = guild.channels.get(messages[i].channel);
    if (!channel) {
      deleteQueue.push(i);
      continue;
    }

    const message = channel.fetchMessage(messages[i].message);
    if (!message) {
      deleteQueue.push(i);
      continue;
    }

    message.createReactionCollector(eval(messages[i].filter), messages[i].options)
      .then(eval(messages[i].callback))
      .catch(console.error);

  }

  if (deleteQueue.length > 0) {
    for (let i = 0; i < deleteQueue.length; i++) messages.splice(deleteQueue[i], 1);
    fs.writeFileSync('./message.json', JSON.stringify(messages));
    console.log(`${deleteQueue.length} message(s) are no longer available.`);
  }
});

Creating a collector later on:

message.channel.send('React with :smile:')
  .then(m => {
    const filter = ...;
    const options = ...;
    const callback = ...;
    m.createReactionCollector(filter, options)
      .then(callback)
      .catch(...);

    messages.push({
      guild: message.guild.id,
      channel: message.channel.id,
      message: message.id,
      filter: `${filter}`,
      options: options,
      callback: `${callback}`
    });

    fs.writeFileSync('./messages.json', JSON.stringify(messages));
  })
  .catch(console.error);

Upvotes: 1

Paulo Marcelo
Paulo Marcelo

Reputation: 31

I think u can use RethinkDB to save messages. RethinkDB is a Realtime Database, and a simple way to detect realtime messages.

https://www.rethinkdb.com/

Upvotes: 0

Related Questions