|
1 | | -import { |
2 | | - type ButtonInteraction, |
3 | | - type CacheType, |
4 | | - type ChannelSelectMenuInteraction, |
5 | | - type GuildMember, |
6 | | - type MentionableSelectMenuInteraction, |
7 | | - MessageFlags, |
8 | | - type Role, |
9 | | - type RoleSelectMenuInteraction, |
10 | | - type StringSelectMenuInteraction, |
11 | | - type UserSelectMenuInteraction, |
12 | | -} from 'discord.js'; |
| 1 | +import type { GuildMember, Role } from 'discord.js'; |
13 | 2 |
|
14 | | -type ComponentInteraction = |
15 | | - | ButtonInteraction<CacheType> |
16 | | - | StringSelectMenuInteraction<CacheType> |
17 | | - | UserSelectMenuInteraction<CacheType> |
18 | | - | RoleSelectMenuInteraction<CacheType> |
19 | | - | MentionableSelectMenuInteraction<CacheType> |
20 | | - | ChannelSelectMenuInteraction<CacheType>; |
21 | | - |
22 | | -export async function addRoleToUser( |
23 | | - member: GuildMember, |
24 | | - role: Role, |
25 | | - interaction: ComponentInteraction |
26 | | -) { |
| 3 | +export async function addRoleToUser(member: GuildMember, role: Role): Promise<void> { |
27 | 4 | const hasRole = member.roles.cache.has(role.id); |
28 | | - if (hasRole) { |
29 | | - await interaction.reply({ |
30 | | - content: `You already have the ${role.name} role.`, |
31 | | - flags: MessageFlags.Ephemeral, |
32 | | - }); |
33 | | - } else { |
34 | | - await member.roles.add(role); |
35 | | - await interaction.reply({ |
36 | | - content: `You have been given the ${role.name} role!`, |
37 | | - flags: MessageFlags.Ephemeral, |
38 | | - }); |
| 5 | + if (!hasRole) { |
| 6 | + try { |
| 7 | + await member.roles.add(role); |
| 8 | + } catch (error) { |
| 9 | + throw new Error( |
| 10 | + `Failed to add role "${role.name}" to user "${member.user.username}": ${error instanceof Error ? error.message : String(error)}` |
| 11 | + ); |
| 12 | + } |
39 | 13 | } |
40 | 14 | } |
0 commit comments