toowake

reactionrole command

Aug 20th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ChannelType, ButtonStyle, Embed, ActionRowBuilder } = require("discord.js");
  2. const schema = require("../../Schemas.js/reactionrole");
  3. const { ButtonBuilder } = require("@discordjs/builders");
  4.  
  5. module.exports = {
  6.     data: new SlashCommandBuilder()
  7.     .setName("reactionrole")
  8.     .setDescription("Reactionrol")
  9.     .addSubcommand(command => command
  10.         .setName("add")
  11.         .setDescription("add a reactionrole to your server")
  12.         .addRoleOption(option => option.setName("role").setDescription("The role you want to give").setRequired(true))
  13.         .addChannelOption(option => option.setName("channel").setDescription("The channel you want to send the message").addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement).setRequired(true))
  14.         .addStringOption(option => option.setName("label").setDescription("The label for the button").setRequired(true))
  15.         .addStringOption(option => option.setName("description").setDescription("The description of the embed").setRequired(false))
  16.         .addStringOption(option => option.setName("title").setDescription("The title of the embed").setRequired(false))
  17.     )
  18.     .addSubcommand(command => command
  19.         .setName("edit")
  20.         .setDescription("edit a reactionrole")
  21.         .addStringOption(option => option.setName("message-id").setDescription("The message id from the reactionrole you want to edit").setRequired(true))
  22.         .addRoleOption(option => option.setName("role").setDescription("The new role you want to add").setRequired(true))
  23.     )
  24.     .addSubcommand(command => command
  25.         .setName("delete")
  26.         .setDescription("delete a reactionrole in your server")
  27.         .addStringOption(option => option.setName("message-id").setDescription("The id of the reactionrole message").setRequired(true))
  28.     ),
  29.  
  30.     async execute (interaction, client) {
  31.         const { options, guild } = interaction;
  32.         const sub = options.getSubcommand()
  33.  
  34.         if (!interaction.member.permissions.has(PermissionFlagsBits.Administrator)) {
  35.             return await interaction.reply({
  36.                 content: "You dont have the permission to execute this command!",
  37.                 ephemeral: true
  38.             })
  39.         }
  40.  
  41.         switch (sub) {
  42.             case "add":
  43.                 const role = options.getRole("role");
  44.                 const channel = options.getChannel("channel");
  45.                 const label = options.getString("label");
  46.                 const desc = options.getString("description") || `React with the button below to get the role ${role}!`;
  47.                 const title = options.getString("title") || "Reactionrole";
  48.                 const finalEmbed = new EmbedBuilder()
  49.                 .setTitle(`${title}`)
  50.                 .setDescription(`${desc}`)
  51.  
  52.                 const button = new ButtonBuilder()
  53.                 .setLabel(`${label}`)
  54.                 .setCustomId("reactionrole")
  55.                 .setStyle(ButtonStyle.Primary)
  56.  
  57.                 const embed = new EmbedBuilder()
  58.                 .setTitle("Embed Created")
  59.                 .addFields(
  60.                     {name: "Title:", value: title},
  61.                     {name: "Description:", value: desc},
  62.                     {name: "Role:", value: `${role}`},
  63.                     {name: "Channel:", value: `${channel}`},
  64.                     {name: "Label:", value: label}
  65.                 )
  66.  
  67.                 const row = new ActionRowBuilder().addComponents(button)
  68.  
  69.                 const message = await channel.send({
  70.                     embeds: [finalEmbed],
  71.                     components: [row]
  72.                 }).catch(err => {
  73.                     interaction.reply({
  74.                         content: `there was an error sending this message to ${channel}!`,
  75.                         ephemeral: true
  76.                     })
  77.  
  78.                     console.log(err)
  79.                 })
  80.  
  81.                 await schema.create({
  82.                     Guild: guild.id,
  83.                     Message: message.id,
  84.                     Role: role.id,
  85.                     Channel: channel.id
  86.                 }).catch(err => {
  87.                     interaction.reply({
  88.                         content: `There was an error creating a schema in our database!`,
  89.                         ephemeral: true
  90.                     })
  91.                 })
  92.  
  93.                 await interaction.reply({
  94.                     embeds: [embed]
  95.                 })
  96.             break;
  97.             case "edit":
  98.                 const editRole = options.getRole("role");
  99.                 const editID = options.getString("message-id");
  100.  
  101.                 const data = await schema.findOne({
  102.                     Message: editID,
  103.                     Guild: guild.id
  104.                 });
  105.  
  106.                 if (!data) {
  107.                     return await interaction.reply({
  108.                         content: `I cant find a reactionrole with \`${editID}\`! Please make sure to execute this command in the correct server!`,
  109.                         ephemeral: true
  110.                     })
  111.                 } else if (data) {
  112.                     await data.update({
  113.                         Guild: guild.id,
  114.                         Message: editID,
  115.                         Role: editRole.id
  116.                     }).catch(err => {
  117.                         interaction.reply({
  118.                             content: "There was an error while trying to edit the reactionrole!",
  119.                             ephemeral: true
  120.                         });
  121.                         console.log(err)
  122.                     })
  123.  
  124.                     return await interaction.reply({
  125.                         content: "The reactionrole has been successfully been edited!",
  126.                         ephemeral: true
  127.                     })
  128.                 }
  129.             break;
  130.             case "delete":
  131.                 const delID = options.getString("message-id");
  132.  
  133.                 await schema.findOneAndDelete({ Guild: guild.id, Message: delID}).catch(async (err, data) => {
  134.                     if (err) {
  135.                         await console.log(err)
  136.                         return interaction.reply({
  137.                             content: "There was an error deleting the reactionrole!",
  138.                             ephemeral: true
  139.                         })
  140.                     } else if (!data) {
  141.                         return await interaction.reply({
  142.                             content: "This reactionrole doesnt exist!",
  143.                             ephemeral: true
  144.                         })
  145.                     }
  146.                 })
  147.  
  148.                 await interaction.reply({
  149.                     content: "The reactionrole has been deleted successfull!",
  150.                     ephemeral: true
  151.                 })
  152.             break;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment