1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-09-29 20:40:59 -04:00
Daggerbot-TS/src/commands/statistics.ts

92 lines
5.2 KiB
TypeScript
Raw Normal View History

2023-05-23 01:14:17 -04:00
import Discord from 'discord.js';
2023-04-14 06:47:58 -04:00
import pkg from 'typescript';
2023-08-29 20:21:53 -04:00
import MessageTool from '../helpers/MessageTool.js';
2023-08-30 04:34:59 -04:00
import FormatBytes from '../helpers/FormatBytes.js';
import FormatTime from '../helpers/FormatTime.js';
import si from 'systeminformation';
2023-04-14 06:47:58 -04:00
import TClient from '../client.js';
import os from 'node:os';
2023-09-29 20:56:35 -04:00
import {Octokit} from '@octokit/rest';
import {createTokenAuth} from '@octokit/auth-token';
2023-08-24 12:06:39 -04:00
import {readFileSync} from 'node:fs';
2023-09-26 06:48:06 -04:00
import {execSync} from 'node:child_process';
2023-08-24 12:06:39 -04:00
const packageJson = JSON.parse(readFileSync('package.json', 'utf8'));
2023-09-26 06:48:06 -04:00
function commitHashes() {
const localHash = execSync('git rev-parse HEAD').toString().trim().slice(0, 7);
const remoteHash = execSync('git ls-remote origin HEAD').toString().split('\t')[0].slice(0, 7);
return { localHash, remoteHash };
}
export default {
2023-03-05 05:04:10 -05:00
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
2023-06-11 21:36:20 -04:00
const waitForData = await interaction.reply({content: '<a:sakjdfsajkfhsdjhjfsa:1065342869428252743>', fetchReply:true})
2023-03-05 05:04:10 -05:00
const cpu = await si.cpu();
const ram = await si.mem();
const osInfo = await si.osInfo();
const currentLoad = await si.currentLoad();
const columns = ['Command name', 'Count'];
const includedCommands = client.commands.filter(x=>x.uses).sort((a,b)=>b.uses - a.uses);
2023-08-30 04:34:59 -04:00
if (includedCommands.size === 0) return interaction.reply(`No commands have been used yet.\nUptime: **${FormatTime(client.uptime, 3, {longNames: true, commas: true})}**`);
2023-04-14 06:47:58 -04:00
const nameLength = Math.max(...includedCommands.map(x=>x.command.default.data.name.length), columns[0].length) + 2;
2023-03-05 05:04:10 -05:00
const amountLength = Math.max(...includedCommands.map(x=>x.uses.toString().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'];
includedCommands.forEach(command=>{
2023-04-14 06:47:58 -04:00
const name = command.command.default.data.name;
2023-03-05 05:04:10 -05:00
const count = command.uses.toString();
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')
2023-08-29 20:21:53 -04:00
.setDescription(MessageTool.concatMessage(
2023-03-05 05:04:10 -05:00
'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)}`
2023-08-29 20:21:53 -04:00
));
2023-03-05 05:04:10 -05:00
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
});
embed.addFields({name: '\u200b', value: `\`\`\`\n${fieldValue}\`\`\``});
} else embed.addFields({name: '\u200b', value: `\`\`\`\n${rows.join('')}\`\`\``});
2023-09-29 20:56:35 -04:00
const SummonAuthentication = createTokenAuth(client.tokens.octokit);
const {token} = await SummonAuthentication();
let githubRepo = {owner: 'AnxietyisReal', repo: 'Daggerbot-TS', ref: 'HEAD'};
const octokit = new Octokit({auth: token, timeZone: 'Australia/NSW', userAgent: 'Daggerbot-TS'});
const github = {
remoteCommit: await octokit.repos.getCommit({...githubRepo, ref: commitHashes().remoteHash}),
localCommit: await octokit.repos.getCommit({...githubRepo, ref: commitHashes().localHash}),
}
2023-03-05 05:04:10 -05:00
embed.addFields(
2023-09-26 06:48:06 -04:00
{
name: '> __Repository__', value: MessageTool.concatMessage(
2023-09-29 20:56:35 -04:00
`**Local:** [${commitHashes().localHash}](${github.localCommit.data.html_url})`,
`**Remote:** [${commitHashes().remoteHash}](${github.remoteCommit.data.html_url})`,
2023-09-26 06:48:06 -04:00
)
},
2023-08-29 20:21:53 -04:00
{name: '> __Dependencies__', value: MessageTool.concatMessage(
2023-04-14 06:47:58 -04:00
`**TypeScript:** ${pkg.version}`,
2023-03-05 05:04:10 -05:00
`**NodeJS:** ${process.version}`,
2023-05-23 01:14:17 -04:00
`**DiscordJS:** ${Discord.version}`,
2023-08-24 12:06:39 -04:00
`**Yarn:** ${packageJson.packageManager.slice(5)}`
2023-08-29 20:21:53 -04:00
)},
{name: '> __Host__', value: MessageTool.concatMessage(
2023-03-05 05:04:10 -05:00
`**Operating System:** ${osInfo.distro + ' ' + osInfo.release}`,
`**CPU:** ${cpu.manufacturer} ${cpu.brand}`,
2023-08-30 04:34:59 -04:00
`**Memory:** ${FormatBytes(ram.used)}/${FormatBytes(ram.total)}`,
`**Process:** ${FormatBytes(process.memoryUsage().heapUsed)}/${FormatBytes(process.memoryUsage().heapTotal)}`,
2023-03-05 05:04:10 -05:00
`**Load Usage:**\nUser: ${currentLoad.currentLoadUser.toFixed(1)}%\nSystem: ${currentLoad.currentLoadSystem.toFixed(1)}%`,
2023-08-30 04:34:59 -04:00
`**Uptime:**\nHost: ${FormatTime((os.uptime()*1000), 2, {longNames: true, commas: true})}\nBot: ${FormatTime(client.uptime, 2, {commas: true, longNames: true})}`
2023-08-29 20:21:53 -04:00
)}
2023-03-05 05:04:10 -05:00
);
2023-08-30 04:34:59 -04:00
waitForData.edit({content:null,embeds:[embed]}).then(x=>x.edit({embeds:[new client.embed(x.embeds[0].data).setFooter({text: `Load time: ${FormatTime(x.createdTimestamp - interaction.createdTimestamp, 2, {longNames: true, commas: true})}`})]}))
2023-03-05 05:04:10 -05:00
},
2023-05-23 01:14:17 -04:00
data: new Discord.SlashCommandBuilder()
2023-03-05 05:04:10 -05:00
.setName('statistics')
.setDescription('See a list of commands ordered by their usage or host stats')
}