Team Ai
Apppublic

Stryker-SC/Discord-Bot-VCMaster-JavaScript-Revamped

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
autorole.js101 linesDownload Raw Back to roles
1const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');2 3module.exports = {4    data: new SlashCommandBuilder()5        .setName('autorole')6        .setDescription('Configure auto-assigned roles for new members.')7        .setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)8        .addSubcommand(subcommand =>9            subcommand10                .setName('human')11                .setDescription('Set role for new human members')12                .addRoleOption(option =>13                    option.setName('role')14                        .setDescription('The role to assign')15                        .setRequired(true)))16        .addSubcommand(subcommand =>17            subcommand18                .setName('bot')19                .setDescription('Set role for new bot members')20                .addRoleOption(option =>21                    option.setName('role')22                        .setDescription('The role to assign')23                        .setRequired(true)))24        .addSubcommand(subcommand =>25            subcommand26                .setName('disable')27                .setDescription('Disable auto-role for a specific type')28                .addStringOption(option =>29                    option.setName('type')30                        .setDescription('The type to disable')31                        .setRequired(true)32                        .addChoices(33                            { name: 'Human', value: 'human' },34                            { name: 'Bot', value: 'bot' }35                        ))),36 37    async execute(interaction, client) {38        await interaction.deferReply({ ephemeral: true });39 40        const subcommand = interaction.options.getSubcommand();41        const guildId = interaction.guild.id;42 43        try {44            if (subcommand === 'human') {45                const role = interaction.options.getRole('role');46                // Permission check: Bot must be able to manage this role47                if (role.position >= interaction.guild.members.me.roles.highest.position) {48                    return interaction.editReply({ content: `❌ I cannot assign **${role.name}** because it is higher than or equal to my highest role.` });49                }50 51                await client.db.setAutoJoinRole(guildId, role.id);52                53                // Synchronize local cache54                let cachedConfig = client.roleConfigCache.get(guildId);55                if (!cachedConfig) cachedConfig = await client.db.getRoleConfig(guildId) || {};56                cachedConfig.auto_join_role_id = role.id;57                client.roleConfigCache.set(guildId, cachedConfig);58 59                await interaction.editReply({ content: `✅ Auto-role for **Humans** set to ${role}.` });60 61            } else if (subcommand === 'bot') {62                const role = interaction.options.getRole('role');63                if (role.position >= interaction.guild.members.me.roles.highest.position) {64                    return interaction.editReply({ content: `❌ I cannot assign **${role.name}** because it is higher than or equal to my highest role.` });65                }66 67                await client.db.setAutoBotRole(guildId, role.id);68                69                // Synchronize local cache70                let cachedConfig = client.roleConfigCache.get(guildId);71                if (!cachedConfig) cachedConfig = await client.db.getRoleConfig(guildId) || {};72                cachedConfig.auto_bot_role_id = role.id;73                client.roleConfigCache.set(guildId, cachedConfig);74 75                await interaction.editReply({ content: `✅ Auto-role for **Bots** set to ${role}.` });76 77            } else if (subcommand === 'disable') {78                const type = interaction.options.getString('type');79                80                let cachedConfig = client.roleConfigCache.get(guildId);81                if (!cachedConfig) cachedConfig = await client.db.getRoleConfig(guildId) || {};82 83                if (type === 'human') {84                    await client.db.setAutoJoinRole(guildId, null);85                    cachedConfig.auto_join_role_id = null;86                    await interaction.editReply({ content: `✅ Auto-role for **Humans** disabled.` });87                } else if (type === 'bot') {88                    await client.db.setAutoBotRole(guildId, null);89                    cachedConfig.auto_bot_role_id = null;90                    await interaction.editReply({ content: `✅ Auto-role for **Bots** disabled.` });91                }92                93                client.roleConfigCache.set(guildId, cachedConfig);94            }95        } catch (error) {96            console.error(error);97            await interaction.editReply({ content: '❌ Failed to save configuration.' });98        }99    },100};101