-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add functionality to give role to users #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
e76ae3d
fd1e4ec
288f0c1
e3f7cec
896d89d
efef3f9
fe6acae
c33fba6
7859759
d302932
45ae96c
00b10df
1f94b7a
7288b02
facfd9f
526e3d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { | ||
| ActionRowBuilder, | ||
| ButtonBuilder, | ||
| ButtonStyle, | ||
| ContainerBuilder, | ||
| type MessageActionRowComponentBuilder, | ||
| } from 'discord.js'; | ||
|
|
||
| // Exported at the bottom after all child components are added | ||
| const containerComponent = new ContainerBuilder(); | ||
|
|
||
| const actionRowComponent = new ActionRowBuilder<MessageActionRowComponentBuilder>(); | ||
|
|
||
| const buttonComponent = new ButtonBuilder() | ||
| .setCustomId('onboarding_add_role') | ||
| .setLabel('Add role') | ||
| .setStyle(ButtonStyle.Primary); | ||
|
|
||
| actionRowComponent.addComponents(buttonComponent); | ||
| containerComponent.addActionRowComponents(actionRowComponent); | ||
|
|
||
| export { containerComponent }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { ApplicationCommandType, MessageFlags } from 'discord.js'; | ||
| import { config } from '../../env.js'; | ||
| import { addRoleToUser } from '../../util/addRoleToUser.js'; | ||
| import { createCommand } from '../../util/commands.js'; | ||
| import { containerComponent } from './component.js'; | ||
|
|
||
| export const onboardingCommand = createCommand({ | ||
| data: { | ||
| name: 'onboarding', | ||
| description: 'Manage onboarding settings', | ||
| type: ApplicationCommandType.ChatInput, | ||
| }, | ||
| execute: async (interaction) => { | ||
| const guild = interaction.guild; | ||
| if (!guild) { | ||
| await interaction.reply({ | ||
| content: 'This command can only be used in a server.', | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| return; | ||
| } | ||
|
Comment on lines
+15
to
+21
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idea for future: should add a custom function that does exactly this check. |
||
| const onboardingRole = guild.roles.cache.get(config.onboarding.roleId); | ||
| if (!onboardingRole) { | ||
| await interaction.reply({ | ||
| content: 'Onboarding role not found. Please check the configuration.', | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| return; | ||
| } | ||
| const onboardingChannel = guild.channels.cache.get(config.onboarding.channelId); | ||
| if (!onboardingChannel || !onboardingChannel.isSendable()) { | ||
| await interaction.reply({ | ||
| content: | ||
| 'Onboarding channel not found or is not a text channel. Please check the configuration.', | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const onboardingMessage = await interaction.reply({ | ||
| components: [containerComponent], | ||
| flags: MessageFlags.IsComponentsV2, | ||
| }); | ||
|
|
||
| const collector = onboardingMessage.createMessageComponentCollector({}); | ||
|
|
||
| collector.on('collect', async (componentInteraction) => { | ||
| if (componentInteraction.customId === 'onboarding_add_role') { | ||
| const member = await guild.members.fetch(componentInteraction.user.id); | ||
| await addRoleToUser(member, onboardingRole, componentInteraction); | ||
| } | ||
| }); | ||
| }, | ||
| }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo file names should be kebab-case |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { | ||
| type ButtonInteraction, | ||
| type CacheType, | ||
| type ChannelSelectMenuInteraction, | ||
| type GuildMember, | ||
| type MentionableSelectMenuInteraction, | ||
| MessageFlags, | ||
| type Role, | ||
| type RoleSelectMenuInteraction, | ||
| type StringSelectMenuInteraction, | ||
| type UserSelectMenuInteraction, | ||
| } from 'discord.js'; | ||
|
|
||
| type ComponentInteraction = | ||
| | ButtonInteraction<CacheType> | ||
| | StringSelectMenuInteraction<CacheType> | ||
| | UserSelectMenuInteraction<CacheType> | ||
| | RoleSelectMenuInteraction<CacheType> | ||
| | MentionableSelectMenuInteraction<CacheType> | ||
| | ChannelSelectMenuInteraction<CacheType>; | ||
|
|
||
| export async function addRoleToUser( | ||
| member: GuildMember, | ||
| role: Role, | ||
| interaction: ComponentInteraction | ||
| ) { | ||
| const hasRole = member.roles.cache.has(role.id); | ||
| if (hasRole) { | ||
| await interaction.reply({ | ||
| content: `You already have the ${role.name} role.`, | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| } else { | ||
| await member.roles.add(role); | ||
| await interaction.reply({ | ||
| content: `You have been given the ${role.name} role!`, | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| } | ||
| } | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.