From fde47d8b7767d69c2825436defb0b7154d1ed3b5 Mon Sep 17 00:00:00 2001 From: Bxio Date: Tue, 29 Apr 2025 02:58:18 +0100 Subject: [PATCH] --- src/commands/Community/lista_membros.js | 95 +++++++++++++------------ 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/src/commands/Community/lista_membros.js b/src/commands/Community/lista_membros.js index ed3d72a..04fd3e9 100644 --- a/src/commands/Community/lista_membros.js +++ b/src/commands/Community/lista_membros.js @@ -26,51 +26,56 @@ module.exports = { -const AsciiTable = require('ascii-table'); - - -const table = new AsciiTable() -.setHeading('discord_id', 'discord_username', 'cargo'); - -try { - // Realizando a consulta ao banco de dados - const [rows] = await conn.execute('SELECT discord_id, discord_username, cargo FROM users'); // Altere para a sua consulta - - // Adicionando as linhas de dados à tabela - rows.forEach(row => { - table.addRow(row.discord_id, `<@${row.discord_username}>`, row.cargo); - }); - - let tabelaString = table.toString(); - - // Filtrando: remover linhas que começam com '+' ou '\' - let linhas = tabelaString.split('\n').filter(linha => { - return !linha.startsWith('+') && !linha.startsWith('\'') && !linha.startsWith('.'); - }); - - let tabelaSemBorda = linhas.join('\n'); - - // Criando o embed para mostrar a tabela - // const embed = new EmbedBuilder() - // .setTitle('📋 Informações dos Membros') - // .setColor('Green') - // .setDescription(` - // \`\`\` - // ${tabelaSemBorda} - // \`\`\` - // `) - // .setFooter({ text: 'Adicionado à base de dados ✅' }); - - // await interaction.editReply({ embeds: [embed] }); - - - await interaction.editReply(`\`\`\`${tabelaSemBorda}\`\`\``); - - -} catch (error) { - console.error('Erro ao acessar o banco de dados:', error); - await interaction.editReply({ content: 'Erro ao carregar os dados dos membros!' }); -} + try { + const [rows] = await conn.execute('SELECT discord_id, discord_username, cargo FROM users'); + + 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 tabelaFinal = [header, separator, ...linhas].join('\n'); + + await interaction.editReply(`\`\`\`fix\n${tabelaFinal}\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); + }