Team Ai
Apppublic

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

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
interactionCreate.js173 linesDownload Raw Back to events
1const { Events, ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder, UserSelectMenuBuilder, StringSelectMenuBuilder, ComponentType, MessageFlags } = require('discord.js');2const { getChannelName } = require('../utils/voiceUtils');3 4module.exports = {5    name: Events.InteractionCreate,6    async execute(interaction, client) {7        // Chat Input Commands8        if (interaction.isChatInputCommand()) {9            const command = client.commands.get(interaction.commandName);10            if (!command) return;11 12            try {13                await command.execute(interaction, client);14            } catch (error) {15                console.error(error);16                if (!interaction.replied && !interaction.deferred) {17                    await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });18                }19            }20            return;21        }22 23        // Button Interactions24        if (interaction.isButton()) {25            await handleButton(interaction, client);26            return;27        }28 29        // Modal Submits30        if (interaction.isModalSubmit()) {31            await handleModal(interaction, client);32            return;33        }34 35        // String/User Select Menus36        if (interaction.isAnySelectMenu()) {37            await handleSelectMenu(interaction, client);38            return;39        }40    },41};42 43async function handleButton(interaction, client) {44    await interaction.deferReply({ flags: MessageFlags.Ephemeral });45    const { customId, member, guild } = interaction;46    const voiceChannel = member.voice.channel;47 48    // Check if user is in any voice channel49    if (!voiceChannel) {50        return interaction.editReply({ content: 'โŒ You must be in a voice channel to use these controls.' });51    }52 53    // Verify Ownership54    const tempChannelData = await client.db.getTempChannel(voiceChannel.id);55    if (!tempChannelData || tempChannelData.owner_id !== member.id) {56        // If not the owner of their CURRENT channel, or if their current channel isn't a temp channel57        return interaction.editReply({ content: 'โŒ You do not own this channel.' });58    }59 60    try {61        switch (customId) {62            case 'vc_lock':63                await voiceChannel.permissionOverwrites.edit(guild.id, { Connect: false });64                await interaction.editReply({ content: '๐Ÿ”’ Channel Locked. (@everyone DENIED)' });65                break;66            case 'vc_unlock':67                await voiceChannel.permissionOverwrites.edit(guild.id, { Connect: true });68                await interaction.editReply({ content: '๐Ÿ”“ Channel Unlocked. (@everyone ALLOWED)' });69                break;70            case 'vc_rename_ask':71                if (tempChannelData) {72                    const lastRenamed = tempChannelData.last_renamed || 0;73                    const cooldown = 10 * 60 * 1000;74                    if (Date.now() - lastRenamed < cooldown) {75                        const mins = Math.ceil((cooldown - (Date.now() - lastRenamed)) / 60000);76                        await interaction.editReply({ content: `โŒ Rename cooldown! Wait ${mins} minutes.` });77                        break;78                    }79                }80                81                try {82                    const newName = getChannelName(member);83                    await voiceChannel.setName(newName);84                    if (tempChannelData) {85                        await client.db.updateTempChannelRenameTime(voiceChannel.id, Date.now());86                    }87                    await interaction.editReply({ content: `โœ… Renamed to **${newName}**` });88                } catch (err) {89                    console.error("Rename Error:", err);90                    await interaction.editReply({ content: "โŒ Rate limited or missing access." });91                }92                break;93            case 'vc_kick_menu':94                const userSelect = new UserSelectMenuBuilder()95                    .setCustomId('vc_kick_submit')96                    .setPlaceholder('Select user to kick')97                    .setMinValues(1)98                    .setMaxValues(1);99 100                const row = new ActionRowBuilder().addComponents(userSelect);101                await interaction.editReply({ content: 'Select a user to kick:', components: [row] });102                break;103        }104    } catch (error) {105        console.error("Button Error:", error);106        let msg = 'โŒ Error executing action.';107        if (error.code === 50013 || error.code === 50001) msg = "โŒ **Missing Access**: Check Bot Role Hierarchy!";108        await interaction.editReply({ content: msg });109    }110}111 112async function handleModal(interaction, client) {113    if (interaction.customId === 'vc_rename_submit') {114        const newName = interaction.fields.getTextInputValue('new_name');115        const voiceChannel = interaction.member.voice.channel;116 117        const tempChannelData = await client.db.getTempChannel(voiceChannel.id);118        if (tempChannelData) {119            const lastRenamed = tempChannelData.last_renamed || 0;120            const cooldown = 10 * 60 * 1000;121            if (Date.now() - lastRenamed < cooldown) {122                const mins = Math.ceil((cooldown - (Date.now() - lastRenamed)) / 60000);123                return interaction.reply({ content: `โŒ Rename cooldown! Wait ${mins} minutes.`, flags: MessageFlags.Ephemeral });124            }125        }126 127        await interaction.deferReply({ flags: MessageFlags.Ephemeral });128 129        try {130            await voiceChannel.setName(newName);131            if (tempChannelData) {132                await client.db.updateTempChannelRenameTime(voiceChannel.id, Date.now());133            }134            await interaction.editReply({ content: `โœ… Renamed to **${newName}**` });135        } catch (error) {136            console.error("Modal Error:", error);137            let msg = 'โŒ Rate limited or error. Try again later.';138            if (error.code === 50035) msg = 'โŒ Name invalid (too long/short).';139            if (error.code === 50013 || error.code === 50001) msg = "โŒ **Missing Access**: Check Bot Role Hierarchy!";140 141            await interaction.editReply({ content: msg });142        }143    }144}145 146async function handleSelectMenu(interaction, client) {147    if (interaction.customId === 'vc_kick_submit') {148        await interaction.deferUpdate();149        const targetUserId = interaction.values[0];150        const voiceChannel = interaction.member.voice.channel;151        const targetMember = await interaction.guild.members.fetch(targetUserId).catch(() => null);152 153        try {154            if (targetMember && targetMember.voice.channelId === voiceChannel.id) {155                await targetMember.voice.disconnect();156            }157            // Block user158            await voiceChannel.permissionOverwrites.edit(targetUserId, { Connect: false });159            await interaction.editReply({ content: `๐Ÿšซ <@${targetUserId}> blocked.`, components: [] });160        } catch (error) {161            console.error(error);162            let msg = 'โŒ Failed to kick user.';163            if (error.code === 50013 || error.code === 50001) msg = "โŒ **Missing Access**: Check Bot Role Hierarchy!";164 165            try {166                await interaction.editReply({ content: msg, components: [] });167            } catch (e) {168                console.error("Failed to send error message:", e);169            }170        }171    }172}173