Untitled

 avatar
Gray
plain_text
19 days ago
3.8 kB
2
Indexable
const {
    Client,
    GatewayIntentBits,
    REST,
    Routes,
    SlashCommandBuilder,
    ChannelType,
    PermissionsBitField,
} = require("discord.js");
require("dotenv").config(); // Load token from .env file

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMembers, // Needed to fetch members
    ],
});

// Manually specify user IDs who should get VC access
const ALLOWED_USER_IDS = ["860829278174576650", "828283498078666764"]; // Replace with actual IDs

// Register commands
const commands = [
    new SlashCommandBuilder()
        .setName("ping")
        .setDescription("Replies with Pong!"),
    new SlashCommandBuilder()
        .setName("createvc")
        .setDescription("Creates a private voice channel"),
].map((command) => command.toJSON());

const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);

client.once("ready", async () => {
    console.log(`Logged in as ${client.user.tag}`);

    try {
        console.log("Refreshing slash commands...");
        await rest.put(Routes.applicationCommands(client.user.id), {
            body: commands,
        });
        console.log("Slash commands registered!");
    } catch (error) {
        console.error(error);
    }
});

client.on("interactionCreate", async (interaction) => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === "ping") {
        return interaction.reply({ content: "Pong! 🏓", ephemeral: true });
    }

    if (interaction.commandName === "createvc") {
        if (
            !interaction.member.permissions.has(
                PermissionsBitField.Flags.ManageChannels,
            )
        ) {
            return interaction.reply({
                content:
                    "❌ You don't have permission to create voice channels!",
                ephemeral: true,
            });
        }

        try {
            // Ensure members are fetched
            await interaction.guild.members.fetch();

            const channel = await interaction.guild.channels.create({
                name: "Private VC",
                type: ChannelType.GuildVoice,
                parent: interaction.channel.parentId || null,
                permissionOverwrites: [
                    {
                        id: interaction.guild.roles.everyone,
                        deny: [
                            PermissionsBitField.Flags.ViewChannel,
                            PermissionsBitField.Flags.Connect,
                        ],
                    },
                    ...ALLOWED_USER_IDS.map((userId) => ({
                        id: userId,
                        allow: [
                            PermissionsBitField.Flags.ViewChannel,
                            PermissionsBitField.Flags.Connect,
                        ],
                    })),
                    {
                        id: interaction.user.id,
                        allow: [
                            PermissionsBitField.Flags.ViewChannel,
                            PermissionsBitField.Flags.Connect,
                        ],
                    },
                ],
            });

            await interaction.reply({
                content: `✅ Private voice channel **${channel.name}** created!`,
                ephemeral: true,
            });
        } catch (error) {
            console.error("Error details:", error);
            await interaction.reply({
                content: `❌ Could not create the voice channel. ${error.message}`,
                ephemeral: true,
            });
        }
    }
});

client.login(process.env.TOKEN);
Editor is loading...
Leave a Comment