Skip to content

Commit fe6acae

Browse files
committed
refactor: use addRoleToUser utility for role assignment
1 parent efef3f9 commit fe6acae

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

src/commands/onboarding/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ApplicationCommandType, MessageFlags } from 'discord.js';
22
import { config } from '../../env.js';
3+
import { addRoleToUser } from '../../util/addRoleToUser.js';
34
import { createCommand } from '../../util/commands.js';
45
import { containerComponent } from './component.js';
56

@@ -46,19 +47,7 @@ export const onboardingCommand = createCommand({
4647
collector.on('collect', async (componentInteraction) => {
4748
if (componentInteraction.customId === 'onboarding_add_role') {
4849
const member = await guild.members.fetch(componentInteraction.user.id);
49-
const hasRole = member.roles.cache.has(onboardingRole.id);
50-
if (hasRole) {
51-
await componentInteraction.reply({
52-
content: `You already have the ${onboardingRole.name} role.`,
53-
flags: MessageFlags.Ephemeral,
54-
});
55-
} else {
56-
await member.roles.add(onboardingRole);
57-
await componentInteraction.reply({
58-
content: `You have been given the ${onboardingRole.name} role!`,
59-
flags: MessageFlags.Ephemeral,
60-
});
61-
}
50+
await addRoleToUser(member, onboardingRole, componentInteraction);
6251
}
6352
});
6453
},

src/util/addRoleToUser.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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';
13+
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+
) {
27+
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+
});
39+
}
40+
}

0 commit comments

Comments
 (0)