|
| 1 | +import type { |
| 2 | + CommandInteraction, |
| 3 | + MessageActionRowComponentBuilder, |
| 4 | + SelectMenuInteraction, |
| 5 | +} from "discord.js"; |
| 6 | +import { ActionRowBuilder, SelectMenuBuilder } from "discord.js"; |
| 7 | +import { Discord, SelectMenuComponent, Slash } from "discordx"; |
| 8 | + |
| 9 | +const roles = [ |
| 10 | + { label: "Principal", value: "principal" }, |
| 11 | + { label: "Teacher", value: "teacher" }, |
| 12 | + { label: "Student", value: "student" }, |
| 13 | +]; |
| 14 | + |
| 15 | +@Discord() |
| 16 | +export class Example { |
| 17 | + @SelectMenuComponent({ id: "role-menu" }) |
| 18 | + async handle(interaction: SelectMenuInteraction): Promise<unknown> { |
| 19 | + await interaction.deferReply(); |
| 20 | + |
| 21 | + // extract selected value by member |
| 22 | + const roleValue = interaction.values?.[0]; |
| 23 | + |
| 24 | + // if value not found |
| 25 | + if (!roleValue) { |
| 26 | + return interaction.followUp("invalid role id, select again"); |
| 27 | + } |
| 28 | + |
| 29 | + await interaction.followUp( |
| 30 | + `you have selected role: ${ |
| 31 | + roles.find((r) => r.value === roleValue)?.label ?? "unknown" |
| 32 | + }` |
| 33 | + ); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + @Slash({ description: "roles menu", name: "my_roles" }) |
| 38 | + async myRoles(interaction: CommandInteraction): Promise<unknown> { |
| 39 | + await interaction.deferReply(); |
| 40 | + |
| 41 | + // create menu for roles |
| 42 | + const menu = new SelectMenuBuilder() |
| 43 | + .addOptions(roles) |
| 44 | + .setCustomId("role-menu"); |
| 45 | + |
| 46 | + // create a row for message actions |
| 47 | + const buttonRow = |
| 48 | + new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents( |
| 49 | + menu |
| 50 | + ); |
| 51 | + |
| 52 | + // send it |
| 53 | + interaction.editReply({ |
| 54 | + components: [buttonRow], |
| 55 | + content: "select your role!", |
| 56 | + }); |
| 57 | + return; |
| 58 | + } |
| 59 | +} |
0 commit comments