-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e76ae3d
🔨 refactor: run check script (organize imports)
hmd-ali fd1e4ec
🌟 feat: add onboarding channelId and roleId to env
hmd-ali 288f0c1
🌟 feat: add onboarding component with button for role addition
hmd-ali e3f7cec
🌟 feat: add onboarding command
hmd-ali 896d89d
🤖 ci: add onboarding channelId and roleId to deployment script
hmd-ali efef3f9
🌟 feat: update web-features dependency to version 3.7.0 and add 'open…
hmd-ali fe6acae
refactor: use addRoleToUser utility for role assignment
wiktoriavh c33fba6
chore: add comment to clarify that export is later
wiktoriavh 7859759
Merge branch 'main' into feat/onboarding
wiktoriavh d302932
Merge branch 'main' into feat/onboarding
wiktoriavh 45ae96c
chore: should not be part of the curren active commands
wiktoriavh 00b10df
fix: improve error handling in role assignment
wiktoriavh 1f94b7a
Merge branch 'main' into feat/onboarding
wiktoriavh 7288b02
Change onboarding channel and role IDs to optional
hmd-ali facfd9f
Merge branch 'main' into feat/onboarding
hmd-ali 526e3d9
remove duplicate vars in example env
hmd-ali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 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') { | ||
| try { | ||
| const member = await guild.members.fetch(componentInteraction.user.id); | ||
| await addRoleToUser(member, onboardingRole); | ||
|
|
||
| await componentInteraction.reply({ | ||
| content: `You have been given the ${onboardingRole.name} role!`, | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| } catch (error) { | ||
| await componentInteraction.reply({ | ||
| content: `Failed to add role. Please contact an administrator.`, | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| console.error('Error adding role:\n', error); | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { GuildMember, Role } from 'discord.js'; | ||
|
|
||
| export async function addRoleToUser(member: GuildMember, role: Role): Promise<void> { | ||
| const hasRole = member.roles.cache.has(role.id); | ||
| if (!hasRole) { | ||
| try { | ||
| await member.roles.add(role); | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Failed to add role "${role.name}" to user "${member.user.username}": ${error instanceof Error ? error.message : String(error)}` | ||
| ); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.