Untitled

mail@pastecode.io avatar
unknown
typescript
a year ago
5.1 kB
4
Indexable
// create basic discord bot put a filler for the token

import { Client, IntentsBitField, TextChannel, REST, Routes, SlashCommandBuilder, ChannelType, VoiceChannel } from 'discord.js';
import { config } from 'dotenv';

const token = 'MTE1MDU0MTA1Mzc0OTg4NzA0Ng.GBk62U.J29K551NXo_K-4cMoBj5Ke2of2vRVl35o9h_Y4'

const client = new Client({intents: [IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages]})
client.login(token);

const channelOwners = new Map<string, VoiceChannel>();

client.on('ready', () => {  
    //get discord channel with id 1150539992590983288 as text channel
    const channel = client.channels.cache.get('1150539992590983288') as TextChannel;
    //send embed with title and description saying "Back Online" with a current timestamp
    channel.send({embeds: [{
        title: 'Bot is back online', 
        description: `Bot is back online at ${new Date().toLocaleString()}`,
    }]});


    const commands = [
        new SlashCommandBuilder()
            .setName('createvc')
            .setDescription('Create a VC')
            .addStringOption(option => option.setName('name').setDescription('Name of the VC').setRequired(true))
            .addIntegerOption(option => option.setName('limit').setDescription('Limit of the VC').setRequired(false))
            .addBooleanOption(option => option.setName('private').setDescription('Private VC').setRequired(false)),
        new SlashCommandBuilder()
            .setName('deletevc')
            .setDescription('Delete your current VC')
    ].map(command => command.toJSON());
    
    const rest = new REST({ version: '9' }).setToken(token);
    
    (async () => {
        try {
            await rest.put(
                Routes.applicationGuildCommands("1150541053749887046", "1149154996416430160"),
                { body: commands }
            );
            console.log('Successfully registered application commands.');
        } catch (error) {
            console.error(error);
        }
    })();


    //Handle the slash command
    client.on('interactionCreate', async interaction => {
        if (!interaction.isCommand()) return;

        if (interaction.commandName === 'createvc') {
                    //check if user id is a key in channelOwners
            if(channelOwners.has(interaction.user.id)) {
                interaction.reply({
                    content: `You already have a channel ${channelOwners.get(interaction.user.id)?.name.toString()}`,
                    ephemeral: true,
                });
                return;
            }
            const name = interaction.options.get('name')?.value as string;
            const limit = interaction.options.get('limit')?.value as number;
            const privateVC = interaction.options.get('private')?.value as boolean;

            if(limit > 25 || limit < 0){
                await interaction.reply({
                    content: 'Limit must be between 0 and 25',
                    ephemeral: true,
                });
                return;
            }

            //create the VC
            const channel = await interaction.guild?.channels.create(
                {
                    name: name,
                    type: ChannelType.GuildVoice,
                    userLimit: limit,
                    parent: '1150552103631204474',
                }
            );

            //set creator as the owner of the channel
            await channel?.permissionOverwrites.create(interaction.user as any, {
                ManageChannels: true
            });

            if(privateVC) {
                //set the channel to private
                await channel?.permissionOverwrites.create(interaction.guild?.roles.everyone as any, {
                    ViewChannel: false,
                });
            }

            channelOwners.set(interaction.user.id, channel as VoiceChannel);


            //send the message to the channel
            await interaction.reply({
                content: `Created channel ${channel?.name.toString()}`,
                ephemeral: true,
            });
        }

        if (interaction.commandName === 'deletevc') {
            //check if user id is a key in channelOwners
            if(channelOwners.has(interaction.user.id)) {
                let channel = channelOwners.get(interaction.user.id);
                //delete the channel
                await channelOwners.get(interaction.user.id)?.delete();
                //remove the key from the map
                channelOwners.delete(interaction.user.id);
                await interaction.reply({
                    content: `Deleted your channel ${channel?.name.toString()}`,
                    ephemeral: true
                });
            } else {
                await interaction.reply({
                    content: 'You do not have a channel',
                    ephemeral: true,
                });
            }
        }
    });
    
});