1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-17 00:10:58 -05:00

combine both embeds into one command

This commit is contained in:
toast-ts 2022-11-30 19:34:53 +11:00
parent f2c8ba088c
commit dd8ec39947

View File

@ -6,81 +6,70 @@ import { versionMajorMinor } from 'typescript';
import { VERSION } from 'ts-node'; import { VERSION } from 'ts-node';
export default { export default {
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
switch (interaction.options.getSubcommand()) { await interaction.deferReply();
case 'commands': const columns = ['Command name', 'Count'];
const columns = ['Command name', 'Count']; const includedCommands = client.commands.filter(x=>x.uses).sort((a,b)=>b.uses - a.uses);
const includedCommands = client.commands.filter(x=>x.uses).sort((a,b)=>b.uses - a.uses); if (includedCommands.size == 0) return interaction.reply(`No commands have been used yet.\nUptime: ${client.formatTime(client.uptime as number, 2, {longNames: true, commas: true})}`);
if (includedCommands.size == 0) return interaction.reply(`No commands have been used yet.\nUptime: ${client.formatTime(client.uptime as number, 2, {longNames: true, commas: true})}`); const nameLength = Math.max(...includedCommands.map(x=>x.default.data.name.length), columns[0].length) + 2;
const nameLength = Math.max(...includedCommands.map(x=>x.default.data.name.length), columns[0].length) + 2; const amountLength = Math.max(...includedCommands.map(x=>x.uses.toLocaleString().length), columns[1].length) + 1;
const amountLength = Math.max(...includedCommands.map(x=>x.uses.toLocaleString().length), columns[1].length) + 1; const rows = [`${columns[0] + ' '.repeat(nameLength - columns[0].length)}|${' '.repeat(amountLength - columns[1].length) + columns[1]}\n`, '-'.repeat(nameLength) + '-'.repeat(amountLength) + '\n'];
const rows = [`${columns[0] + ' '.repeat(nameLength - columns[0].length)}|${' '.repeat(amountLength - columns[1].length) + columns[1]}\n`, '-'.repeat(nameLength) + '-'.repeat(amountLength) + '\n']; includedCommands.forEach(command=>{
includedCommands.forEach(command=>{ const name = command.default.data.name;
const name = command.default.data.name; const count = command.uses.toString();
const count = command.uses.toString(); rows.push(`${name + ' '.repeat(nameLength - name.length)}${' '.repeat(amountLength - count.length) + count}\n`);
rows.push(`${name + ' '.repeat(nameLength - name.length)}${' '.repeat(amountLength - count.length) + count}\n`); });
const embed = new client.embed().setColor(client.config.embedColor).setTitle('Statistics: Command Usage')
.setDescription([
'List of commands that have been used in this session, ordered by amount of use. Table contains command name and amount of uses.',
`Total amount of commands used in this session: ${client.commands.filter(x=>x.uses).map(x=>x.uses).reduce((a,b)=>a+b, 0)}`
].join('\n'))
if (rows.join('').length > 1024){
let fieldValue = '';
rows.forEach(row=>{
if (fieldValue.length + row.length > 1024){
embed.addFields({name: '\u200b', value: `\`\`\`\n${fieldValue}\`\`\``});
fieldValue = row;
} else {
fieldValue += row;
}
}); });
const embed = new client.embed().setColor(client.config.embedColor).setTitle('Statistics: Command Usage') embed.addFields({name: '\u200b', value: `\`\`\`\n${fieldValue}\`\`\``});
.setDescription([ } else {
'List of commands that have been used in this session, ordered by amount of use. Table contains command name and amount of uses.', embed.addFields({name: '\u200b', value: `\`\`\`\n${rows.join('')}\`\`\``})
`Total amount of commands used in this session: ${client.commands.filter(x=>x.uses).map(x=>x.uses).reduce((a,b)=>a+b, 0)}` };
].join('\n'))
if (rows.join('').length > 1024){ // Bytes conversion
let fieldValue = ''; function formatBytes(bytes, decimals = 2) {
rows.forEach(row=>{ if (bytes === 0) return '0 Bytes';
if (fieldValue.length + row.length > 1024){ const k = 1024;
embed.addFields({name: '\u200b', value: `\`\`\`\n${fieldValue}\`\`\``}); const dm = decimals < 0 ? 0 : decimals;
fieldValue = row; const sizes = ['Bytes', 'KB', 'MB', 'GB'];
} else { const i = Math.floor(Math.log(bytes) / Math.log(k));
fieldValue += row; return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
} }
}); const cpu = await si.cpu();
embed.addFields({name: '\u200b', value: `\`\`\`\n${fieldValue}\`\`\``}); const ram = await si.mem();
} else { const osInfo = await si.osInfo();
embed.addFields({name: '\u200b', value: `\`\`\`\n${rows.join('')}\`\`\``}) const currentLoad = await si.currentLoad();
}; const embed1 = new client.embed().setColor(client.config.embedColor).setTitle('Statistics: Host Info').setDescription([
interaction.reply({embeds: [embed]}) `> __Dependencies__`,
break; `**TypeScript:** ${versionMajorMinor}`,
case 'host': `**TS-Node:** ${VERSION}`,
// Bytes conversion `**NodeJS:** ${process.version}`,
function formatBytes(bytes, decimals = 2) { `**DiscordJS:** ${version}`,
if (bytes === 0) return '0 Bytes'; `> __Host__`,
const k = 1024; `**Operating System:** ${osInfo.distro + ' ' + osInfo.release}`,
const dm = decimals < 0 ? 0 : decimals; `**CPU:** ${cpu.manufacturer, cpu.brand}`,
const sizes = ['Bytes', 'KB', 'MB', 'GB']; `**Memory:** ${formatBytes(ram.used)}/${formatBytes(ram.total)}`,
const i = Math.floor(Math.log(bytes) / Math.log(k)); `**NodeJS:** ${formatBytes(process.memoryUsage().heapUsed)}/${formatBytes(process.memoryUsage().heapTotal)}`,
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; `**Load Usage:**\nUser: ${currentLoad.currentLoadUser.toFixed(1)}%\nSystem: ${currentLoad.currentLoadSystem.toFixed(1)}%`,
} `**Uptime:**\nHost: ${client.formatTime((os.uptime()*1000), 2, {longNames: true, commas: true})}\nBot: ${client.formatTime(client.uptime as number, 2, {commas: true, longNames: true})}`
await interaction.deferReply(); ].join('\n'))
const cpu = await si.cpu(); .setFooter({text: `Load time: ${Date.now() - interaction.createdTimestamp}ms`});
const ram = await si.mem(); interaction.editReply({embeds: [embed, embed1]});
const osInfo = await si.osInfo();
const currentLoad = await si.currentLoad();
const embed1 = new client.embed().setColor(client.config.embedColor).setTitle('Statistics: Host Info').setDescription([
`> __Dependencies__`,
`**TypeScript:** ${versionMajorMinor}`,
`**TS-Node:** ${VERSION}`,
`**NodeJS:** ${process.version}`,
`**DiscordJS:** ${version}`,
`> __Host__`,
`**Operating System:** ${osInfo.distro + ' ' + osInfo.release}`,
`**CPU:** ${cpu.manufacturer, cpu.brand}`,
`**Memory:** ${formatBytes(ram.used)}/${formatBytes(ram.total)}`,
`**NodeJS:** ${formatBytes(process.memoryUsage().heapUsed)}/${formatBytes(process.memoryUsage().heapTotal)}`,
`**Load Usage:**\nUser: ${currentLoad.currentLoadUser.toFixed(1)}%\nSystem: ${currentLoad.currentLoadSystem.toFixed(1)}%`,
`**Uptime:**\nHost: ${client.formatTime((os.uptime()*1000), 2, {longNames: true, commas: true})}\nBot: ${client.formatTime(client.uptime as number, 2, {commas: true, longNames: true})}`
].join('\n'))
.setFooter({text: `Load time: ${Date.now() - interaction.createdTimestamp}ms`});
interaction.editReply({embeds: [embed1]});
}
}, },
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('statistics') .setName('statistics')
.setDescription('See a list of commands ordered by their usage or bot stats') .setDescription('See a list of commands ordered by their usage or bot stats')
.setDMPermission(false) .setDMPermission(false)
.addSubcommand((opt)=>opt
.setName('commands')
.setDescription('View command usage stats'))
.addSubcommand((opt)=>opt
.setName('host')
.setDescription('View host stats'))
} }