Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const setupCMD = "/rolereact"
- const initialMessage = `**React to the messages below to receive the associated role. If you would like to remove the role, simply remove your reaction!**`;
- const embedMessage = `
- React to the emoji that matches the role you wish to receive.
- If you would like to remove the role, simply remove your reaction!
- `;
- const embedFooter = "Role Reactions";
- const roles = ["LoL", "Artist", "Public Relations", "Intern"];
- const reactions = ["๐ป", "๐", "๐", "๐"];
- const embed = true;
- const embedColor = "#061D49";
- const botToken = "";
- const { Client, RichEmbed } = require('discord.js');
- const client = new Client({ disableEveryone: true });
- client.login(botToken);
- if (roles.length !== reactions.length) throw "Roles list and reactions list are not the same length!";
- function generateMessages() {
- let messages = [];
- for (let role of roles) messages.push(`React below to get the **"${role}"** role!`);
- return messages;
- }
- function generateEmbedFields() {
- return roles.map((r, e) => {
- return {
- emoji: reactions[e],
- role: r
- };
- });
- }
- client.on("ready", async () => {
- console.log(`[LOGS] ${client.user.username} is watching over ${client.guilds.size} servers`);
- client.user.setPresence({
- game: {
- name: 'League of Legends',
- type: "STREAMING",
- url: "https://www.twitch.tv/mrpopper"
- }
- });
- });
- // Handles the creation of the role reactions. Will either send the role messages separately or in an embed
- client.on("message", message => {
- if (message.content.toLowerCase() == setupCMD) {
- if (!embed) {
- message.channel.send(initialMessage);
- const toSend = generateMessages();
- toSend.forEach((role, react) => {
- message.channel.send(role).then(m => {
- m.react(reactions[react]);
- });
- });
- } else {
- const roleEmbed = new RichEmbed()
- .setDescription(embedMessage)
- .setFooter(embedFooter);
- if (embedColor) roleEmbed.setColor(embedColor);
- const fields = generateEmbedFields();
- for (const f of fields) roleEmbed.addField(f.emoji, f.role, true);
- message.channel.send(roleEmbed).then(async m => {
- for (let r of reactions) await m.react(r);
- });
- }
- }
- });
- // This makes the events used a bit more readable
- const events = {
- MESSAGE_REACTION_ADD: 'messageReactionAdd',
- MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
- };
- // This event handles adding/removing users from the role(s) they chose
- client.on('raw', async event => {
- if (!events.hasOwnProperty(event.t)) return;
- const { d: data } = event;
- const user = client.users.get(data.user_id);
- const channel = client.channels.get(data.channel_id);
- const message = await channel.fetchMessage(data.message_id);
- const member = message.guild.members.get(user.id);
- const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
- const reaction = message.reactions.get(emojiKey);
- let embedFooterText;
- if (message.embeds[0]) embedFooterText = message.embeds[0].footer.text;
- if (message.author.id === client.user.id && (message.content !== initialMessage || (message.embeds[0] && (embedFooterText !== embedFooter)))) {
- if (!embed) {
- const re = `\\*\\*"(.+)?(?="\\*\\*)`;
- const role = message.content.match(re)[1];
- if (member.id !== client.user.id) {
- const roleObj = message.guild.roles.find(r => r.name === role);
- if (event.t === "MESSAGE_REACTION_ADD") {
- member.addRole(roleObj.id);
- } else {
- member.removeRole(roleObj.id);
- }
- }
- } else {
- const fields = message.embeds[0].fields;
- for (let i = 0; i < fields.length; i++) {
- if (member.id !== client.user.id) {
- const role = message.guild.roles.find(r => r.name === fields[i].value);
- if (fields[i].name === reaction.emoji.name) {
- if (event.t === "MESSAGE_REACTION_ADD") {
- member.addRole(role.id);
- break;
- } else {
- member.removeRole(role.id);
- break;
- }
- }
- }
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement