Repository navigation
Expand file tree
/
Copy pathversionCheck.ts
More file actions
65 lines (57 loc) · 1.77 KB
/
Copy pathversionCheck.ts
File metadata and controls
65 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { SlashCommandBuilder } from "discord.js";
import type {
CommandName,
NamedChatInputCommandInteraction,
} from "./commandTypes.ts";
import { execSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
export const VERSION_COMMAND_NAME = "version" satisfies CommandName;
export const versionCommandData = new SlashCommandBuilder()
.setName(VERSION_COMMAND_NAME)
.setDescription("Get the current bot version based on git commit history")
.toJSON();
type VersionCommandInteraction = NamedChatInputCommandInteraction<
typeof VERSION_COMMAND_NAME
>;
const VERSION_FILE_PATH = fileURLToPath(
new URL("../version.json", import.meta.url),
);
export async function handleVersionCommand(
interaction: VersionCommandInteraction,
) {
try {
await interaction.deferReply();
let commitCount: string;
let commitHash: string;
if (existsSync(VERSION_FILE_PATH)) {
const versionData = JSON.parse(
readFileSync(VERSION_FILE_PATH, "utf8"),
);
commitCount = versionData.commitCount;
commitHash = versionData.commitHash;
} else {
commitCount = execSync("git rev-list --count HEAD")
.toString()
.trim();
commitHash = execSync("git rev-parse --short HEAD")
.toString()
.trim();
}
await interaction.editReply({
content: `commit #${commitCount} - [${commitHash}](https://github.com/InvalidSE/ithinkihave/commit/${commitHash})`,
});
} catch (error) {
console.error("[bot] failed to get version info", error);
if (interaction.deferred || interaction.replied) {
await interaction.editReply({
content: "Could not retrieve version information.",
});
return;
}
await interaction.reply({
content: "Could not retrieve version information.",
ephemeral: true,
});
}
}