discord-jellyfin-bot/src/commands/status.command.ts

95 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-12-16 14:15:32 +01:00
import { TransformPipe } from '@discord-nestjs/common';
import {
Command,
DiscordCommand,
2022-12-16 14:15:32 +01:00
InjectDiscordClient,
UsePipes
2022-12-16 14:15:32 +01:00
} from '@discord-nestjs/core';
import {
Client,
CommandInteraction,
InteractionReplyOptions,
Status
} from 'discord.js';
2022-12-16 14:15:32 +01:00
2022-12-16 16:10:16 +01:00
import { formatDuration, intervalToDuration } from 'date-fns';
import { DiscordMessageService } from '../clients/discord/discord.message.service';
import { JellyfinService } from '../clients/jellyfin/jellyfin.service';
2022-12-16 21:21:26 +01:00
import { Constants } from '../utils/constants';
2022-12-16 16:10:16 +01:00
import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api';
2022-12-16 14:15:32 +01:00
@Command({
name: 'status',
description: 'Display the current status for troubleshooting',
})
@UsePipes(TransformPipe)
export class StatusCommand implements DiscordCommand {
2022-12-16 14:15:32 +01:00
constructor(
@InjectDiscordClient()
private readonly client: Client,
private readonly discordMessageService: DiscordMessageService,
private readonly jellyfinService: JellyfinService,
2022-12-16 14:15:32 +01:00
) {}
async handler(
commandInteraction: CommandInteraction,
): Promise<string | InteractionReplyOptions> {
2022-12-16 14:15:32 +01:00
const ping = this.client.ws.ping;
2022-12-16 16:10:16 +01:00
const status = Status[this.client.ws.status];
const interval = intervalToDuration({
start: this.client.uptime,
end: 0,
});
const formattedDuration = formatDuration(interval);
2022-12-16 14:15:32 +01:00
const jellyfinSystemApi = getSystemApi(this.jellyfinService.getApi());
const jellyfinSystemInformation = await jellyfinSystemApi.getSystemInfo();
2022-12-16 14:15:32 +01:00
return {
embeds: [
this.discordMessageService.buildMessage({
title: 'Discord Bot Status',
mixin(embedBuilder) {
return embedBuilder.addFields([
{
name: 'Bot Version',
2022-12-19 11:57:04 +01:00
value: Constants.Metadata.Version.All(),
inline: true,
},
{
name: 'Discord Bot Ping',
value: `${ping}ms`,
inline: true,
},
{
name: 'Discord Bot Status',
value: `${status}`,
inline: true,
},
{
name: 'Discord Bot Uptime',
value: `${formattedDuration}`,
inline: false,
},
{
name: 'Jellyfin Server Version',
value: jellyfinSystemInformation.data.Version ?? 'unknown',
inline: true,
},
{
name: 'Jellyfin Server Operating System',
value:
jellyfinSystemInformation.data.OperatingSystem ?? 'unknown',
inline: true,
},
]);
},
}),
2022-12-16 14:15:32 +01:00
],
};
}
}