1
votes

I would like to know how to integrate reaction roles in my discord.js bot. I have tried the traditional methods of messageReactionAdd but it seem's very hard to make it extendable and editable, and it just became unusuable after having my bot in many guilds... I have been trying to search for node modules that make this possible, but the only thing i found was this, and when trying to integrate it in my bot, it just made my commands and stuff no longer work, i have tried to read how to make reaction roles using that package, but at no avail, nothing worked... I did try this:

const ReactionRole = require("reaction-role");
const system = new ReactionRole("my-token");

let option1 = system.createOption("x:697809640147105878", "697809380137107478"); 
let option2 = system.createOption("emoji-1:720843460158698152", "708355720436777033");
let option3 = system.createOption("pepe:720623437466435626", "703908514887761930");

system.createMessage("725572782157898450", "702115562158948432", 2, null, option1, option2, option3);

system.init();

But as i said, it made all my commands unusable...

Hope someone can help me!

1

1 Answers

3
votes

You can easily implement this using the discord.js-collector package, it supports MongoDB, which is a database, so you'll no longer have to edit your bot manually!

Here's how to do it:

const { ReactionRoleManager } = require('discord.js-collector'); //We import the discord.js-collector package that'll make reaction roles possible
const { Client } = require('discord.js'); // We import the client constructor to initialize a new client
const client = new Client(); //We create a new client

const reactionRoleManager = new ReactionRoleManager(client, {
 //We create a reaction role manager that'll handle everything related to reaction roles
 storage: true, // Enable reaction role store in a Json file
 path: __dirname + '/roles.json', // Where will save the roles if store is enabled
 mongoDbLink: 'url mongoose link', // See here to see how setup mongoose: https://github.com/IDjinn/Discord.js-Collector/tree/dev/examples/reaction-role-manager/Note.md & https://medium.com/@LondonAppBrewery/how-to-download-install-mongodb-on-windows-4ee4b3493514
});

client.on('ready', () => {
 console.log('ready');
});

client.on('message', async (message) => {
 const client = message.client;
 const args = message.content.split(' ').slice(1);
 // Example
 // >createReactionRole @role :emoji: MessageId
 if (message.content.startsWith('>createReactionRole')) {
  const role = message.mentions.roles.first();
  if (!role)
   return message
    .reply('You need mention a role')
    .then((m) => m.delete({ timeout: 1_000 }));

  const emoji = args[1];
  if (!emoji)
   return message
    .reply('You need use a valid emoji.')
    .then((m) => m.delete({ timeout: 1_000 }));

  const msg = await message.channel.messages.fetch(args[2] || message.id);
  if (!role)
   return message
    .reply('Message not found!')
    .then((m) => m.delete({ timeout: 1_000 }));

  reactionRoleManager.addRole({
   message: msg,
   role,
   emoji,
  });
  message.reply('Done').then((m) => m.delete({ timeout: 500 }));
 }
});

client.login('Token');

And here's a live preview:

Image

You can also use this package to do some other really cool stuff! Like embed paginator, questions, Yes/No Questions... You can find them all here!