|
| 1 | +import { ApplicationCommandType, MessageFlags } from 'discord.js'; |
| 2 | +import { config } from '../../env.js'; |
| 3 | +import { addRoleToUser } from '../../util/addRoleToUser.js'; |
| 4 | +import { createCommand } from '../../util/commands.js'; |
| 5 | +import { containerComponent } from './component.js'; |
| 6 | + |
| 7 | +export const onboardingCommand = createCommand({ |
| 8 | + data: { |
| 9 | + name: 'onboarding', |
| 10 | + description: 'Manage onboarding settings', |
| 11 | + type: ApplicationCommandType.ChatInput, |
| 12 | + }, |
| 13 | + execute: async (interaction) => { |
| 14 | + const guild = interaction.guild; |
| 15 | + if (!guild) { |
| 16 | + await interaction.reply({ |
| 17 | + content: 'This command can only be used in a server.', |
| 18 | + flags: MessageFlags.Ephemeral, |
| 19 | + }); |
| 20 | + return; |
| 21 | + } |
| 22 | + const onboardingRole = guild.roles.cache.get(config.onboarding.roleId); |
| 23 | + if (!onboardingRole) { |
| 24 | + await interaction.reply({ |
| 25 | + content: 'Onboarding role not found. Please check the configuration.', |
| 26 | + flags: MessageFlags.Ephemeral, |
| 27 | + }); |
| 28 | + return; |
| 29 | + } |
| 30 | + const onboardingChannel = guild.channels.cache.get(config.onboarding.channelId); |
| 31 | + if (!onboardingChannel || !onboardingChannel.isSendable()) { |
| 32 | + await interaction.reply({ |
| 33 | + content: |
| 34 | + 'Onboarding channel not found or is not a text channel. Please check the configuration.', |
| 35 | + flags: MessageFlags.Ephemeral, |
| 36 | + }); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const onboardingMessage = await interaction.reply({ |
| 41 | + components: [containerComponent], |
| 42 | + flags: MessageFlags.IsComponentsV2, |
| 43 | + }); |
| 44 | + |
| 45 | + const collector = onboardingMessage.createMessageComponentCollector({}); |
| 46 | + |
| 47 | + collector.on('collect', async (componentInteraction) => { |
| 48 | + if (componentInteraction.customId === 'onboarding_add_role') { |
| 49 | + try { |
| 50 | + const member = await guild.members.fetch(componentInteraction.user.id); |
| 51 | + await addRoleToUser(member, onboardingRole); |
| 52 | + |
| 53 | + await componentInteraction.reply({ |
| 54 | + content: `You have been given the ${onboardingRole.name} role!`, |
| 55 | + flags: MessageFlags.Ephemeral, |
| 56 | + }); |
| 57 | + } catch (error) { |
| 58 | + await componentInteraction.reply({ |
| 59 | + content: `Failed to add role. Please contact an administrator.`, |
| 60 | + flags: MessageFlags.Ephemeral, |
| 61 | + }); |
| 62 | + console.error('Error adding role:\n', error); |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
| 66 | + }, |
| 67 | +}); |
0 commit comments