This commit is contained in:
parent
fde47d8b77
commit
60fc21a8d9
@ -28,55 +28,27 @@ module.exports = {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const [rows] = await conn.execute('SELECT discord_id, discord_username, cargo FROM users');
|
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) {
|
const campos = ['Discord ID', 'Username', 'Cargo'];
|
||||||
await interaction.editReply('Nenhum membro encontrado.');
|
const colWidths = campos.map((campo, i) =>
|
||||||
return;
|
Math.max(campo.length, ...rows.map(row => String(Object.values(row)[i]).length))
|
||||||
}
|
|
||||||
|
|
||||||
// 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');
|
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) {
|
} catch (error) {
|
||||||
console.error('Erro ao acessar o banco de dados:', error);
|
console.error('Erro ao acessar o banco de dados:', error);
|
||||||
await interaction.editReply({ content: 'Erro ao carregar os dados dos membros!' });
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user