const {Command, Random} = require('../../lib.js')

const {EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, } = require('discord.js');


    class createchannel extends Command{
        constructor(client){
            super(client, {
                name: 'createchannel',
                description: 'Creates a text based channel on the category the command was used.',
                aliases: ['channel'],
                needsAdmin:true,

            })
        }
        
        async run(message, args)
        {
        //Check admin privilege 
            var name = args.join(' ');
            //log the creation of a new channel.
            console.log("Creating a new channel on server:",message.guild.name," with the name:",name);
            //Actually create the channel.
            message.guild.channels.create({
                name: name,
                type: ChannelType.GuildText,
                parent: message.guild.channels.cache.get(message.channelId).parentId
                }).then(()=>
                {
                sendMessage(name)
                })   
        
            //Send message confirming the creation of said new channel.
                
            async function sendMessage(name){
                    const randomID = Random()
                    const embed = new EmbedBuilder()
                        .setColor(0xd31f1f)
                        .setTitle('New channel created!')
                        .setAuthor( {name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png"})
                        .setDescription(`${name}`)
                        .setFooter({text:'Rem-chan on ', iconURL:"https://i.imgur.com/g6FSNhL.png"})
                        .setTimestamp()
                    const row = new ActionRowBuilder()
                    .addComponents(
                        new ButtonBuilder()
                            .setCustomId(randomID)
                            .setLabel('Remove this.')
                            .setStyle(ButtonStyle.Primary),
                    );
                    const Message = await message.channel.send({ephemeral: true, embeds: [embed], components: [row] }); 
                    const filter = i => i.customId === randomID;

                    const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 });

                    collector.on('collect', async m => 
                    {
                        message.delete();
                        m.message.delete();
                    });
                    
                    collector.on('end', async ()=>
                    {
                        Message.edit({ components: [] });
                    })
                }
        }
}module.exports = createchannel;