text: const { SlashCommandBuilder } = require('@discordjs/builders');
  const Discord = require('discord.js');
  const GuildModel = require('../mongoSchema/guild');
  let GuildInfo = require('../functions/guildInfo');
  
  module.exports = {
      data: new SlashCommandBuilder()
          .setName('config')
          .setDescription('exibe as configurações da Lyra nesse servidor'),
  
      userPermissions: ['ADMINISTRATOR'],
      /**
       * @param {Discord.Client} client
       * @param {Discord.CommandInteraction} interaction
       * @param {DisTube} distube
       */
      async execute(client, interaction) {
          await interaction.deferReply({ ephemeral: true });
          let guildInfo = await GuildInfo(interaction.guild);
  
          async function generateMessage() {
              guildInfo = await GuildInfo(interaction.guild);
              const autoPlayColor = guildInfo.autoPlay === true ? 'SUCCESS' : 'DANGER';
              const autoPlay = guildInfo.autoPlay === true ? '✅ Ativado' : '❌ Desativado';
              const autoPlayEmoji = guildInfo.autoPlay === true ? '✅' : '❌';
              const defaultVolume = guildInfo.volume;
  
              let djRoles = 'Para alterar esses cargos, utilize /cargos-dj\n\n';
              if (guildInfo.djRoles == []) djRoles = 'Nenhum cargo definido'
              else guildInfo.djRoles.forEach(roleId => djRoles += `<@&${roleId}>\n`);
  
              let embed = new Discord.MessageEmbed()
                  .setColor('AQUA')
                  .setTitle('⚙ Configurações')
                  .setFooter('Dica: Para permitir que qualquer um controle o player de música,\nAdicione @everyone como cargo de DJ')
                  .addFields(
                      { name: '🔊 Volume padrão', value: `${guildInfo.volume}`, inline: true },
                      { name: 'AutoPlay', value: `${autoPlay} por padrão`, inline: true },
                      { name: 'Cargos de DJ', value: djRoles, inline: true },
                  );
  
  
              let buttons = new Discord.MessageActionRow();
  
              buttons.addComponents(
                  new Discord.MessageButton()
                      .setCustomId('config-autoplay')
                      .setLabel(`AutoPlay`)
                      .setStyle(autoPlayColor)
                      .setEmoji(autoPlayEmoji),
                  new Discord.MessageButton()
                      .setCustomId('config-volume')
                      .setLabel(`Volume padrão | ${defaultVolume}`)
                      .setStyle('PRIMARY'),
              );
  
              return { embeds: [embed], components: [buttons] }
          }
  
          let message = await interaction.editReply(await generateMessage());
  
          let collector = message.createMessageComponentCollector({ time: 60_000 });
  
          collector.on('collect', async buttonInteraction => {
              if (!buttonInteraction.isButton()) return;
              await buttonInteraction.deferReply({ ephemeral: false });
              const option = buttonInteraction.customId;
  
              if (option == 'config-autoplay') {
                  const autoPlay = guildInfo.autoPlay !== true;
  
                  try {
                      let guildUpdate = await GuildModel.findOneAndUpdate({ guildId: interaction.guildId }, {
                          autoPlay: autoPlay,
                          lastEdit: Date.now()
                      });
                      guildUpdate.save();
                  }
                  catch (err) {
                      return buttonInteraction.editReply({ content: 'Ops, houve um erro de comunicação no banco de dados.\nNão foi possível salvar as configurações' });
                  }
  
                  await GuildInfo(interaction.guild, true);
                  interaction.editReply(await generateMessage());
                  buttonInteraction.deleteReply();
              }
              else if (option == 'config-volume') {
  
                  let embed = new Discord.MessageEmbed()
                      .setColor('AQUA')
                      .setTitle('🔊 Volume')
                      .setDescription(`Defina o volume padrão quando o player é iniciado`)
  
                  let buttons1 = new Discord.MessageActionRow()
                  let buttons2 = new Discord.MessageActionRow()
                  for (let vol = 10; vol <= 100; vol += 10) {
                      const buttons = vol > 50 ? buttons2 : buttons1;
                      buttons.addComponents(
                          new Discord.MessageButton()
                              .setCustomId(`${vol}`)
                              .setLabel(`${vol}`)
                              .setStyle('PRIMARY')
                      );
                  }
                  let volumeMessage = await interaction.followUp({ embeds: [embed], components: [buttons1, buttons2], fetchReply: true });
  
                  let volumeCollector = volumeMessage.createMessageComponentCollector({
                      filter: int => int.user.id == interaction.user.id
                  });
  
                  volumeCollector.on('collect', async volumeButton => {
                      await volumeButton.deferReply({ ephemeral: false });
                      const volumeChoice = parseInt(volumeButton.customId, 10);
                      try {
                          let guildUpdate = await GuildModel.findOneAndUpdate({ guildId: interaction.guildId }, {
                              volume: volumeChoice,
                              lastEdit: Date.now()
                          });
                          guildUpdate.save();
                      }
                      catch (err) {
                          return volumeButton.editReply({ content: 'Ops, houve um erro de comunicação no banco de dados.\nNão foi possível salvar as configurações' });
                      }
  
                      await GuildInfo(interaction.guild, true);
                      interaction.editReply(await generateMessage());
                      volumeButton.deleteReply();
                      volumeCollector.stop();
                  }).on('end', async () => {
                      try { volumeMessage.delete(); }
                      catch (err) { }
                  });
  
                  buttonInteraction.deleteReply();
  
              } // Volume button end
  
  
          }); // Collector event end
  
      }
  }