Files
pixelpoebel/src/events/interactionCreate.ts
coding ad988c9333
All checks were successful
Auto Build and Push Docker Image / build (push) Successful in 7s
Add role selection system with select menus
2026-04-02 10:55:43 +00:00

110 lines
4.8 KiB
TypeScript

import { Events, MessageFlags, ComponentType } from 'discord.js';
export default {
name: Events.InteractionCreate,
async execute(interaction: any, client: any) {
if (interaction.isStringSelectMenu()) {
const customId = interaction.customId;
if (customId.startsWith('role_select_')) {
const categoryName = customId.replace('role_select_', '');
const selectedRoleId = interaction.values[0];
const member = interaction.member;
const guild = interaction.guild;
const category: any = client.DB.get(
'SELECT * FROM role_categories WHERE guild_id = ? AND category_name = ?',
guild.id,
categoryName
);
if (!category) {
await interaction.reply({ content: 'Kategorie nicht gefunden.', flags: [MessageFlags.Ephemeral] });
return;
}
const option: any = client.DB.get(
'SELECT * FROM role_options WHERE category_id = ? AND role_id = ?',
category.id,
selectedRoleId
);
if (!option) {
await interaction.reply({ content: 'Rolle nicht gefunden.', flags: [MessageFlags.Ephemeral] });
return;
}
const discordRole = guild.roles.cache.get(selectedRoleId);
if (!discordRole) {
await interaction.reply({ content: 'Diese Rolle existiert nicht mehr.', flags: [MessageFlags.Ephemeral] });
return;
}
const hasRole = member.roles.cache.has(selectedRoleId);
if (category.exclusive) {
const options: any[] = client.DB.all('SELECT * FROM role_options WHERE category_id = ?', category.id);
for (const opt of options) {
if (opt.role_id !== selectedRoleId && member.roles.cache.has(opt.role_id)) {
await member.roles.remove(opt.role_id);
}
}
await member.roles.add(selectedRoleId);
await interaction.reply({
content: `Du hast jetzt die Rolle ${option.emoji ? option.emoji + ' ' : ''}${discordRole.name}!`,
flags: [MessageFlags.Ephemeral]
});
} else {
if (hasRole) {
await member.roles.remove(selectedRoleId);
await interaction.reply({
content: `Die Rolle ${option.emoji ? option.emoji + ' ' : ''}${discordRole.name} wurde entfernt.`,
flags: [MessageFlags.Ephemeral]
});
} else {
const userRoles = member.roles.cache.filter((r: any) => !r.managed && r.id !== guild.id).size;
if (category.max_roles > 0 && userRoles >= category.max_roles) {
await interaction.reply({
content: `Du kannst maximal ${category.max_roles} Rollen aus dieser Kategorie haben.`,
flags: [MessageFlags.Ephemeral]
});
return;
}
await member.roles.add(selectedRoleId);
await interaction.reply({
content: `Du hast jetzt die Rolle ${option.emoji ? option.emoji + ' ' : ''}${discordRole.name}!`,
flags: [MessageFlags.Ephemeral]
});
}
}
return;
}
}
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction, client);
client.DB.run(`
INSERT INTO command_stats (command_name, uses)
VALUES (?, 1)
ON CONFLICT(command_name) DO UPDATE SET uses = uses + 1
`, interaction.commandName);
} catch (error) {
console.error(error);
const replyOptions = { content: 'There was an error while executing this command!', flags: [MessageFlags.Ephemeral] };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(replyOptions);
} else {
await interaction.reply(replyOptions);
}
}
},
};