diff --git a/src/commands/Community/lista_membros.js b/src/commands/Community/lista_membros.js index 04fd3e9..ed885fb 100644 --- a/src/commands/Community/lista_membros.js +++ b/src/commands/Community/lista_membros.js @@ -28,55 +28,27 @@ module.exports = { try { const [rows] = await conn.execute('SELECT discord_id, discord_username, cargo FROM users'); + if (!rows.length) return await interaction.editReply('Nenhum membro encontrado.'); - if (rows.length === 0) { - await interaction.editReply('Nenhum membro encontrado.'); - return; - } - - // Primeiro, calcula o tamanho máximo de cada coluna - let maxDiscordIdLength = 'Discord ID'.length; - let maxUsernameLength = 'Username'.length; - let maxCargoLength = 'Cargo'.length; - - rows.forEach(row => { - maxDiscordIdLength = Math.max(maxDiscordIdLength, String(row.discord_id).length); - maxUsernameLength = Math.max(maxUsernameLength, row.discord_username.length); - maxCargoLength = Math.max(maxCargoLength, row.cargo.length); - }); - - // Cria cabeçalho - const header = - padRight('Discord ID', maxDiscordIdLength) + ' | ' + - padRight('Username', maxUsernameLength) + ' | ' + - padRight('Cargo', maxCargoLength); - - const separator = - '-'.repeat(maxDiscordIdLength) + '-|-' + - '-'.repeat(maxUsernameLength) + '-|-' + - '-'.repeat(maxCargoLength); - - // Monta as linhas - const linhas = rows.map(row => - padRight(String(row.discord_id), maxDiscordIdLength) + ' | ' + - padRight(row.discord_username, maxUsernameLength) + ' | ' + - padRight(row.cargo, maxCargoLength) + const campos = ['Discord ID', 'Username', 'Cargo']; + const colWidths = campos.map((campo, i) => + Math.max(campo.length, ...rows.map(row => String(Object.values(row)[i]).length)) ); - const tabelaFinal = [header, separator, ...linhas].join('\n'); + const montarLinha = arr => arr.map((txt, i) => String(txt).padEnd(colWidths[i])).join(' | '); - await interaction.editReply(`\`\`\`fix\n${tabelaFinal}\n\`\`\``); + const tabela = [ + montarLinha(campos), + colWidths.map(w => '-'.repeat(w)).join('-|-'), + ...rows.map(row => montarLinha([row.discord_id, row.discord_username, row.cargo])) + ].join('\n'); + + await interaction.editReply(`\`\`\`fix\n${tabela}\n\`\`\``); } catch (error) { console.error('Erro ao acessar o banco de dados:', error); await interaction.editReply({ content: 'Erro ao carregar os dados dos membros!' }); } - - // Função para preencher à direita - function padRight(text, length) { - return text + ' '.repeat(length - text.length); - } -