-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' into feat-clear-cmd
- Loading branch information
Showing
8 changed files
with
97 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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 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 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,33 @@ | ||
import { event, Events } from '../../lib'; | ||
import { handleRollAssignment } from './functions/handleRollAssignment'; | ||
|
||
export default event(Events.InteractionCreate, async ({ log }, interaction) => { | ||
if (!interaction.isButton()) return; | ||
|
||
log('Button Handler: Button pressed'); | ||
|
||
const { customId, component, user } = interaction; | ||
|
||
const buttonLabel = component?.label; | ||
|
||
try { | ||
const [prefix, ...params] = interaction.customId.split('_'); | ||
|
||
switch (prefix) { | ||
case 'roleAssignment': | ||
const [roleID] = params; | ||
await handleRollAssignment(interaction, roleID); | ||
log(`Button Handler: Role assignment button pressed by ${user.tag} (${user.id}). roleID: ${roleID}`); | ||
break; | ||
default: | ||
if (buttonLabel) { | ||
log(`Button Handler: Custom ID not matched. Skipping...\nCustom ID: ${customId}, Label: ${buttonLabel}, User: ${user.tag}, User ID: ${user.id}`); | ||
} else { | ||
log(`Button Handler: Custom ID not matched. Skipping...\nCustom ID: ${customId}, Label: null, User: ${user.tag}, User ID: ${user.id}`); | ||
} | ||
return; | ||
} | ||
} catch (error) { | ||
log('Button Handler: Error handling button press', error); | ||
} | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/events/buttonHandlers/functions/handleRollAssignment.ts
This file contains 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,41 @@ | ||
import { ButtonInteraction, GuildMember } from 'discord.js'; | ||
import { constantsConfig, Logger } from '../../../lib'; | ||
|
||
export async function handleRollAssignment(interaction: ButtonInteraction, roleID: string) { | ||
await interaction.deferReply({ ephemeral: true }); | ||
|
||
try { | ||
// Find the role object based on the customId | ||
let role = null; | ||
for (const group of constantsConfig.roleAssignmentIds) { | ||
role = group.roles.find((r) => r.id === roleID); | ||
if (role) break; // Stop searching if the role is found in any group | ||
} | ||
|
||
if (!role) { | ||
Logger.error('Role Assignment: Role not found'); | ||
interaction.editReply({ content: 'I couldn\'t find that role' }); | ||
return; | ||
} | ||
|
||
if (!interaction.member) { | ||
Logger.error('Role Assignment: Interaction member is null'); | ||
return; | ||
} | ||
|
||
const member = interaction.member as GuildMember; | ||
|
||
const hasRole = member.roles.cache.has(role.id); | ||
|
||
if (hasRole) { | ||
await member.roles.remove(role.id); | ||
await interaction.editReply(`The role <@&${role.id}> has been removed.`); | ||
} else { | ||
await member.roles.add(role.id); | ||
await interaction.editReply(`The role <@&${role.id}> has been added.`); | ||
} | ||
} catch (error) { | ||
Logger.error(error); | ||
await interaction.editReply({ content: 'Something went wrong, this role may no longer exist. Please try again. The error message has been logged.' }); | ||
} | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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