pnp-scheduler/source/Discord/Commands/Bot.ts

68 lines
No EOL
2 KiB
TypeScript

import {
CacheType,
ChatInputCommandInteraction,
hyperlink,
MessageFlags,
PermissionFlagsBits,
SlashCommandBuilder
} from "discord.js";
import {ChatInteractionCommand, Command} from "./Command";
import * as fs from "node:fs";
import {UserError} from "../UserError";
import {ifError} from "node:assert";
import {BuildContextGetter} from "../../Utilities/BuildContext";
import {EmbedLibrary} from "../EmbedLibrary";
export class BotCommand implements Command, ChatInteractionCommand {
definition(): SlashCommandBuilder {
return new SlashCommandBuilder()
.setName("bot")
.setDescription("Offers some information about the bot")
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addSubcommand(command => command
.setName("build")
.setDescription("Displays some information about the build the bot is running on.")
)
}
execute(interaction: ChatInputCommandInteraction<CacheType>): Promise<void> {
switch (interaction.options.getSubcommand()) {
case "build":
this.displayBuildInfos(interaction);
break;
}
}
private displayBuildInfos(interaction: ChatInputCommandInteraction<CacheType>) {
const buildContext = new BuildContextGetter().getContext();
if (!buildContext) {
throw new UserError("Can't find required deploy information", "Using a valid docker image or (when running on a dev build) running `npm run build` once.");
}
const embed = EmbedLibrary.base("Current Build")
.setFields(
[
{
name: "Build Target",
value: buildContext.target,
inline: true
},
{
name: "Build Label",
value: buildContext.label,
inline: true
},
{
name: "Latest Commit",
value: hyperlink(buildContext.commitHash, buildContext.commitLink)
}
]
)
interaction.reply({
embeds: [embed],
flags: MessageFlags.Ephemeral
})
}
}