Skip to content

Commit

Permalink
fix(bot): fix shardinfo command not working well
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Nov 10, 2024
1 parent 775e0b7 commit 19fe923
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/bot/src/sharding/modules/shardutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export default class ShardUtilModule extends ShardManagerModule {
)
.catch(() => null);
res.push({
id: shard.id,
...(shardRes ?? {}),
id: shard.id,
respawnWhenAvailable: shard.respawnWhenAvailable,
lastActivity: shard.lastActivity
});
Expand Down
29 changes: 18 additions & 11 deletions apps/bot/src/textCommands/shardinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ export default class ShardInfoCommand extends TextCommand {
return pad(hours) + ':' + pad(minutes) + ':' + pad(s);
}

padValue(value: number | undefined, maxLength: number) {
return (typeof value === 'number' ? value.toLocaleString() : '-').padEnd(maxLength, ' ');
}

async run(ctx: CommandContext) {
const sharding = this.client.modules.get('sharding') as ShardingModule;

if (!sharding.on) return void (await replyOrSend(ctx, 'Sharding is not enabled.'));
const {
d: { res }
} = await sharding.sendAndRecieve<{
res: { id: number; status: string; guilds: number; latency: number; uptime: number; recordings: number; respawnWhenAvailable: boolean }[];
res: { id: number; status?: string; guilds?: number; latency?: number; uptime?: number; recordings?: number; respawnWhenAvailable: boolean }[];
}>('getShardInfo');

const totalGuilds = res.reduce((acc, cur) => acc + cur.guilds, 0);
const averageLatency = Math.round(res.reduce((acc, cur) => acc + cur.latency, 0) / res.length);
const averageUptime = res.reduce((acc, cur) => acc + cur.uptime, 0) / res.length;
const totalRecordings = res.reduce((acc, cur) => acc + cur.recordings, 0);
const totalGuilds = res.reduce((acc, cur) => acc + (cur.guilds ?? 0), 0);
const averageLatency = Math.round(res.reduce((acc, cur) => acc + (cur.latency ?? 0), 0) / res.length);
const averageUptime = res.reduce((acc, cur) => acc + (cur.uptime ?? 0), 0) / res.length;
const totalRecordings = res.reduce((acc, cur) => acc + (cur.recordings ?? 0), 0);

const message =
`Your Shard ID: ${process.env.SHARD_ID}\n\n` +
Expand All @@ -52,12 +56,15 @@ export default class ShardInfoCommand extends TextCommand {
res
.map(
(shard) =>
`${shard.id === parseInt(process.env.SHARD_ID!) ? '>' : ' '} [${shard.id.toString().padStart(3, ' ')}]: ${shard.status.padStart(
12,
' '
)} | ${shard.guilds.toLocaleString().padEnd(10, ' ')} | ${`${Math.round(shard.latency)}ms`.padEnd(11, ' ')} | ${this.format(
shard.uptime
).padEnd(14, ' ')} | ${shard.recordings.toLocaleString().padEnd(12, ' ')} | ${shard.respawnWhenAvailable}`
`${shard.id === parseInt(process.env.SHARD_ID!) ? '>' : ' '} [${shard.id.toString().padStart(3, ' ')}]: ${(
shard.status ?? 'unknown'
).padStart(12, ' ')} | ${this.padValue(shard.guilds, 10)} | ${(typeof shard.latency === 'number'
? `${Math.round(shard.latency)}ms`
: '-'
).padEnd(11, ' ')} | ${(typeof shard.uptime === 'number' ? this.format(shard.uptime) : '-').padEnd(14, ' ')} | ${this.padValue(
shard.recordings,
12
)} | ${shard.respawnWhenAvailable ?? '-'}`
)
.join('\n');

Expand Down

0 comments on commit 19fe923

Please sign in to comment.