|
| 1 | +/*! |
| 2 | + * @author TRACTION (iamtraction) |
| 3 | + * @copyright 2024 |
| 4 | + */ |
| 5 | +import { ApplicationCommandOptionType, ButtonStyle, ChatInputCommandInteraction, ComponentType, GuildTextBasedChannel, PermissionFlagsBits } from "discord.js"; |
| 6 | +import { Command, Logger } from "@bastion/tesseract"; |
| 7 | + |
| 8 | +import RoleModel from "../../../models/Role.js"; |
| 9 | +import SelectRoleGroupModel from "../../../models/SelectRoleGroup.js"; |
| 10 | +import * as arrays from "../../../utils/arrays.js"; |
| 11 | +import MessageComponents from "../../../utils/components.js"; |
| 12 | +import { SelectRolesUI } from "../../../utils/constants.js"; |
| 13 | +import { generate as generateEmbed } from "../../../utils/embeds.js"; |
| 14 | + |
| 15 | +class SelectRolesUpdateCommand extends Command { |
| 16 | + constructor() { |
| 17 | + super({ |
| 18 | + name: "update", |
| 19 | + description: "Update the roles and message for the Select Roles Group.", |
| 20 | + options: [ |
| 21 | + { |
| 22 | + type: ApplicationCommandOptionType.String, |
| 23 | + name: "id", |
| 24 | + description: "The Select Roles Group ID.", |
| 25 | + required: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + type: ApplicationCommandOptionType.String, |
| 29 | + name: "message", |
| 30 | + description: "The new message content.", |
| 31 | + }, |
| 32 | + ], |
| 33 | + userPermissions: [ PermissionFlagsBits.ManageGuild ], |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + public async exec(interaction: ChatInputCommandInteraction<"cached">): Promise<unknown> { |
| 38 | + await interaction.deferReply(); |
| 39 | + const id = interaction.options.getString("id"); |
| 40 | + const message = generateEmbed(interaction.options.getString("message") || ""); |
| 41 | + |
| 42 | + // get the select roles group document |
| 43 | + const selectRoleGroup = await SelectRoleGroupModel.findById(id); |
| 44 | + |
| 45 | + // check whether the select roles group exists |
| 46 | + if (!selectRoleGroup?.id) { |
| 47 | + return interaction.editReply(`The Select Roles Group **${ id }** doesn't exist.`); |
| 48 | + } |
| 49 | + |
| 50 | + // fetch the select roles group message |
| 51 | + const selectRolesMessage = await (interaction.guild.channels.cache.get(selectRoleGroup.channel) as GuildTextBasedChannel)?.messages.fetch(selectRoleGroup.id).catch(Logger.ignore); |
| 52 | + |
| 53 | + // check whether the select roles group message exists |
| 54 | + if (selectRolesMessage) { |
| 55 | + // update roles in select roles group |
| 56 | + const roleDocuments = await RoleModel.find({ |
| 57 | + $or: selectRoleGroup.roles.map(id => ({ id })), |
| 58 | + }); |
| 59 | + |
| 60 | + const selectRoles = selectRoleGroup.roles.map(id => { |
| 61 | + const role = interaction.guild.roles.cache.get(id); |
| 62 | + const roleDocument = roleDocuments.find(r => r.id === id); |
| 63 | + |
| 64 | + return { |
| 65 | + value: id, |
| 66 | + label: role?.name, |
| 67 | + description: roleDocument?.description, |
| 68 | + emoji: roleDocument?.emoji || role?.unicodeEmoji, |
| 69 | + }; |
| 70 | + }); |
| 71 | + |
| 72 | + // update select roles group components with select menu |
| 73 | + if (selectRoleGroup.ui === SelectRolesUI.SelectMenu) { |
| 74 | + await selectRolesMessage.edit({ |
| 75 | + content: message ? typeof message === "string" ? message : "" : undefined, |
| 76 | + embeds: message ? typeof message === "string" ? [] : [ message ] : undefined, |
| 77 | + components: [ |
| 78 | + { |
| 79 | + type: ComponentType.ActionRow, |
| 80 | + components: [ |
| 81 | + { |
| 82 | + type: ComponentType.StringSelect, |
| 83 | + customId: MessageComponents.SelectRolesSelect, |
| 84 | + placeholder: "Select Roles", |
| 85 | + minValues: selectRoleGroup.min || 0, |
| 86 | + maxValues: selectRoleGroup.max || selectRoles.length, |
| 87 | + options: selectRoles, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + ], |
| 92 | + }); |
| 93 | + } else { |
| 94 | + // update select roles group components with buttons |
| 95 | + await selectRolesMessage.edit({ |
| 96 | + content: message ? typeof message === "string" ? message : "" : undefined, |
| 97 | + embeds: message ? typeof message === "string" ? [] : [ message ] : undefined, |
| 98 | + components: arrays.chunks(selectRoles, 5).map(roles => ({ |
| 99 | + type: ComponentType.ActionRow, |
| 100 | + components: roles.map(role => ({ |
| 101 | + customId: MessageComponents.SelectRolesButton + ":" + role.value, |
| 102 | + type: ComponentType.Button, |
| 103 | + label: role.label, |
| 104 | + style: ButtonStyle.Secondary, |
| 105 | + emoji: role.emoji, |
| 106 | + })), |
| 107 | + })), |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + return interaction.editReply(`I've updated the Select Roles Group **${ id }**.`); |
| 112 | + } |
| 113 | + |
| 114 | + await interaction.editReply(`The Select Roles Group **${ id }** doesn't exist anymore.`); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export { SelectRolesUpdateCommand as Command }; |
0 commit comments