Advertisement
Guest User

Scuffed Role Reaction Embed.

a guest
Feb 24th, 2020
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1.  
  2. const setupCMD = "/rolereact"
  3. const initialMessage = `**React to the messages below to receive the associated role. If you would like to remove the role, simply remove your reaction!**`;
  4. const embedMessage = `
  5. React to the emoji that matches the role you wish to receive.
  6. If you would like to remove the role, simply remove your reaction!
  7. `;
  8. const embedFooter = "Role Reactions";
  9. const roles = ["LoL", "Artist", "Public Relations", "Intern"];
  10. const reactions = ["๐Ÿ’ป", "๐Ÿ–Œ", "๐Ÿ˜ƒ", "๐Ÿ†•"];
  11. const embed = true;
  12. const embedColor = "#061D49";
  13. const botToken = "";
  14.  
  15. const { Client, RichEmbed } = require('discord.js');
  16. const client = new Client({ disableEveryone: true });
  17. client.login(botToken);
  18.  
  19. if (roles.length !== reactions.length) throw "Roles list and reactions list are not the same length!";
  20.  
  21. function generateMessages() {
  22. let messages = [];
  23. for (let role of roles) messages.push(`React below to get the **"${role}"** role!`);
  24. return messages;
  25. }
  26.  
  27. function generateEmbedFields() {
  28. return roles.map((r, e) => {
  29. return {
  30. emoji: reactions[e],
  31. role: r
  32. };
  33. });
  34. }
  35.  
  36. client.on("ready", async () => {
  37. console.log(`[LOGS] ${client.user.username} is watching over ${client.guilds.size} servers`);
  38. client.user.setPresence({
  39. game: {
  40. name: 'League of Legends',
  41. type: "STREAMING",
  42. url: "https://www.twitch.tv/mrpopper"
  43. }
  44. });
  45. });
  46.  
  47. // Handles the creation of the role reactions. Will either send the role messages separately or in an embed
  48. client.on("message", message => {
  49. if (message.content.toLowerCase() == setupCMD) {
  50.  
  51. if (!embed) {
  52. message.channel.send(initialMessage);
  53.  
  54. const toSend = generateMessages();
  55. toSend.forEach((role, react) => {
  56. message.channel.send(role).then(m => {
  57. m.react(reactions[react]);
  58. });
  59. });
  60. } else {
  61. const roleEmbed = new RichEmbed()
  62. .setDescription(embedMessage)
  63. .setFooter(embedFooter);
  64.  
  65. if (embedColor) roleEmbed.setColor(embedColor);
  66.  
  67. const fields = generateEmbedFields();
  68. for (const f of fields) roleEmbed.addField(f.emoji, f.role, true);
  69.  
  70. message.channel.send(roleEmbed).then(async m => {
  71. for (let r of reactions) await m.react(r);
  72. });
  73. }
  74. }
  75. });
  76.  
  77. // This makes the events used a bit more readable
  78. const events = {
  79. MESSAGE_REACTION_ADD: 'messageReactionAdd',
  80. MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
  81. };
  82.  
  83. // This event handles adding/removing users from the role(s) they chose
  84. client.on('raw', async event => {
  85.  
  86. if (!events.hasOwnProperty(event.t)) return;
  87.  
  88. const { d: data } = event;
  89. const user = client.users.get(data.user_id);
  90. const channel = client.channels.get(data.channel_id);
  91.  
  92. const message = await channel.fetchMessage(data.message_id);
  93. const member = message.guild.members.get(user.id);
  94.  
  95. const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
  96. const reaction = message.reactions.get(emojiKey);
  97.  
  98. let embedFooterText;
  99. if (message.embeds[0]) embedFooterText = message.embeds[0].footer.text;
  100.  
  101. if (message.author.id === client.user.id && (message.content !== initialMessage || (message.embeds[0] && (embedFooterText !== embedFooter)))) {
  102.  
  103. if (!embed) {
  104. const re = `\\*\\*"(.+)?(?="\\*\\*)`;
  105. const role = message.content.match(re)[1];
  106.  
  107. if (member.id !== client.user.id) {
  108. const roleObj = message.guild.roles.find(r => r.name === role);
  109.  
  110. if (event.t === "MESSAGE_REACTION_ADD") {
  111. member.addRole(roleObj.id);
  112. } else {
  113. member.removeRole(roleObj.id);
  114. }
  115. }
  116. } else {
  117. const fields = message.embeds[0].fields;
  118.  
  119. for (let i = 0; i < fields.length; i++) {
  120. if (member.id !== client.user.id) {
  121. const role = message.guild.roles.find(r => r.name === fields[i].value);
  122.  
  123. if (fields[i].name === reaction.emoji.name) {
  124. if (event.t === "MESSAGE_REACTION_ADD") {
  125. member.addRole(role.id);
  126. break;
  127. } else {
  128. member.removeRole(role.id);
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement