-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from this-is-yaash/24-role-implementation-stag…
…e-ii 24 role implementation stage ii
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const { EmbedBuilder, SlashCommandBuilder, ActionRowBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder } = require('discord.js'); | ||
|
||
module.exports = { | ||
data: new SlashCommandBuilder() | ||
.setName('promote') | ||
.setDescription('Request for role promotion.'), | ||
|
||
async execute(interaction) { | ||
try { | ||
const guild = interaction.guild; | ||
|
||
const roles = guild.roles.cache | ||
.filter((role) => !role.managed && role.name !== '@everyone') | ||
.map((role) => role.name); | ||
|
||
const roleOptions = roles.map((role, index) => ({ | ||
label: role, | ||
value: `role_${index}`, | ||
})); | ||
|
||
const selectOptions = roleOptions.map((option) => | ||
new StringSelectMenuOptionBuilder() | ||
.setLabel(option.label) | ||
.setValue(option.value) | ||
); | ||
|
||
const selectMenu = new StringSelectMenuBuilder() | ||
.setCustomId('roleMenu') | ||
.setPlaceholder('Select a role') | ||
.addOptions(selectOptions); | ||
|
||
const row = new ActionRowBuilder().addComponents(selectMenu); | ||
|
||
// Defer the initial response | ||
await interaction.deferReply({ ephemeral: true }); | ||
|
||
// Send the role selection menu | ||
await interaction.followUp({ content: 'Please select a role:', components: [row] }) | ||
.catch(console.error); | ||
|
||
const filter = (interaction) => interaction.customId === 'roleMenu'; | ||
const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 }); | ||
|
||
collector.on('collect', async (interaction) => { | ||
try { | ||
const selectedRole = interaction.values[0].split('_')[1]; | ||
const roleName = roles[parseInt(selectedRole)]; | ||
|
||
const adminRole = guild.roles.cache.find((role) => role.name === 'Admin'); | ||
|
||
if (adminRole) { | ||
const adminMembers = adminRole.members; | ||
for (const admin of adminMembers) { | ||
await admin.send(`User ${interaction.user.tag} requested role: ${roleName}`); | ||
} | ||
|
||
const requestSentEmbed = new EmbedBuilder() | ||
.setColor('#0099ff') | ||
.setTitle('Role Request Sent') | ||
.setDescription(`Your request for the role "${roleName}" has been sent to the admins.`); | ||
|
||
await interaction.editReply({ content: '', embeds: [requestSentEmbed] }).catch(console.error); | ||
} | ||
} catch (error) { | ||
console.error('Failed to process the role request:', error); | ||
const errorMessage = 'Failed to send the request. Please try again later.'; | ||
interaction.followUp(errorMessage).catch(console.error); | ||
} | ||
}); | ||
} catch (error) { | ||
console.error('Error in processing the role promotion:', error); | ||
} | ||
}, | ||
}; |