const {Command} = require('../../lib.js')
const {EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle} = require('discord.js');
const channelM = require('../../models/channels');

class setrustcommitschannel extends Command{
    constructor(client){
        super(client, {
            name: 'setrustcommitschannel',
            aliases:['rustcommits','rustc'],
            description: 'Sets the channel that will receive the rust commits.',
            needsAdmin:true,

        })
    }
    async run(message, args)
    {           
        //Check admin privilege
        if(args.length>0) return sendMessage(2);
        if(typeof args[0]=='string' && !isNaN(parseInt(args[0])) && args[0].length>17) return sendMessage(2);
        var newName = message.guild.channels.cache.get(args[0]).name;
        addChannel(args[0], newName); 
       
        //Message sending function
        async function sendMessage(modifier, name)
        {
            const embed = new EmbedBuilder()
            embed.setFooter({text:'Rem-chan on ', iconURL:"https://i.imgur.com/g6FSNhL.png"})
            embed.setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'}); 
            embed.setColor(0x110809);
            embed.setTimestamp()
            embed.setTitle(`Channel ${name} defined.`)    

            const row = new ActionRowBuilder()
            .addComponents(
                new ButtonBuilder()
                    .setCustomId('RemoveRustCommitsNewChannelMessage')
                    .setLabel('Remove this.')
                    .setStyle(ButtonStyle.Primary),
            );
            const filter = i => i.customId === 'RemoveRustCommitsNewChannelMessage';

            await message.channel.send({ephemeral: true, embeds: [embed], components: [row] }); 
            const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 });
            collector.on('collect', async m => 
            {
                message.delete();
                m.message.delete()
            });
        }
        function addChannel(id, name)
        {
            var channel = new channelM();
            channel.name = name;
            channel.cID = id;
            channel.for = 'rust';
            channel.save(err=>
                {
                    if(err)console.log(err);
                    sendMessage(0, name);
                })
        }
    }
}module.exports = setrustcommitschannel;