Skip to content

Commit 96f190c

Browse files
Add status command
1 parent 379b355 commit 96f190c

File tree

5 files changed

+86
-55
lines changed

5 files changed

+86
-55
lines changed

package-lock.json

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"dependencies": {
1818
"@discordjs/builders": "^0.8.2",
1919
"@discordjs/rest": "^0.1.0-canary.0",
20-
"date-fns": "^2.27.0",
2120
"discord-api-types": "^0.25.2",
2221
"discord.js": "^13.3.1",
2322
"discord.js-docs": "github:BenjammingKirby/discord.js-docs#typescriptRewrite",

src/commands/docs/djs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Command } from "../../interfaces";
33
import { SlashCommandBuilder } from "@discordjs/builders";
44
import Doc, { sources } from "discord.js-docs";
5-
import { checkEmbedLimits } from "../../utils/embedUtils";
5+
import { checkEmbedLimits } from "../../utils/EmbedUtils";
66

77
const supportedBranches = Object.keys(sources).map(
88
(branch) => [capitalize(branch), branch] as [string, string]

src/handlers/MessageHandler.ts

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
1-
import { intervalToDuration } from "date-fns";
21
import { Formatters, MessageEmbed } from "discord.js";
32
import type { Message } from "discord.js";
3+
import { intervalToDuration, intervalObjToStr } from "../utils/DateUtils";
44

55
export async function messageHandler(message: Message<true>) {
6-
const clientUser = message.client.user;
7-
// The regex for the bot's mention
8-
const mentionRegex = new RegExp(`<@!?${clientUser.id}>`);
6+
try {
7+
const clientUser = message.client.user;
8+
// The regex for the bot's mention
9+
const mentionRegex = new RegExp(`<@!?${clientUser.id}>`);
910

10-
if (message.content.trim().match(mentionRegex)) {
11-
const pkgJSONPath = "../../package.json";
12-
const pkgJSON = await import(pkgJSONPath);
13-
const { version, description, dependencies } = pkgJSON;
11+
if (message.content.trim().match(mentionRegex)) {
12+
const pkgJSONPath = "../../package.json";
13+
const pkgJSON = await import(pkgJSONPath);
14+
const { version, description, dependencies } = pkgJSON;
1415

15-
const uptime = intervalToDuration({
16-
start: new Date().getTime() - message.client.uptime,
17-
end: new Date().getTime(),
18-
});
16+
const uptime = intervalToDuration(Date.now() - message.client.uptime, Date.now());
17+
const statusEmbed = new MessageEmbed()
18+
.setTitle(`${clientUser.username} (v${version})`)
19+
.setURL("https://github.com/the-programmers-hangout/tph-docs-bot/")
20+
.setColor(0xd250c7)
21+
.setDescription(description)
22+
.setThumbnail(clientUser.displayAvatarURL({ dynamic: true, format: "png", size: 4096 }))
23+
.addField(
24+
"Currently Supported Docs",
25+
["discord.js", "Javascript (mdn)"].map((str) => `\`${str}\``).join(", "),
26+
)
27+
.addField("Dependencies", Formatters.codeBlock("json", JSON.stringify(dependencies, undefined, 4)))
28+
.addField("Uptime", `${intervalObjToStr(uptime)}` || "Just turned on")
29+
.addField("Ping", message.client.ws.ping + "ms", true)
30+
.addField("Source", "[Github](https://github.com/the-programmers-hangout/tph-docs-bot/)", true)
31+
.addField(
32+
"Contributors",
33+
"[Link](https://github.com/the-programmers-hangout/tph-docs-bot/graphs/contributors)",
34+
true,
35+
);
1936

20-
const statusEmbed = new MessageEmbed()
21-
.setTitle(`${clientUser.tag} ${version}`)
22-
.setURL("https://github.com/the-programmers-hangout/tph-docs-bot/")
23-
.setColor(0x90ee90)
24-
.setDescription(description)
25-
.addField(
26-
"Currently Supported Docs",
27-
["discord.js", "Javascript (mdn)"].map((str) => `\`${str}\``).join(", "),
28-
)
29-
.addField("Dependencies", Formatters.codeBlock("json", JSON.stringify(dependencies, undefined, 4)))
30-
.addField(
31-
"Uptime",
32-
`${uptime.months} months, ${uptime.days} days, ${uptime.hours} hours, ${uptime.minutes} minutes, ${uptime.seconds} seconds`,
33-
)
34-
.addField("Ping", message.client.ws.ping + "ms", true)
35-
.addField("Source", "[github](https://github.com/the-programmers-hangout/tph-docs-bot/)", true)
36-
.addField(
37-
"Contributors",
38-
"[link](https://github.com/the-programmers-hangout/tph-docs-bot/graphs/contributors)",
39-
true,
40-
);
41-
42-
message.reply({ embeds: [statusEmbed] }).catch(console.error);
37+
await message.reply({ embeds: [statusEmbed] });
38+
}
39+
} catch (e) {
40+
console.error(e);
4341
}
42+
4443
return;
4544
}

src/utils/DateUtils.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const toSecondsConverterConstant = {
2+
month: 30 * 24 * 60 * 60,
3+
day: 24 * 60 * 60,
4+
hour: 60 * 60,
5+
minute: 60,
6+
second: 1,
7+
};
8+
type formats = keyof typeof toSecondsConverterConstant;
9+
export function intervalToDuration(start: number | Date, end: number | Date = Date.now()) {
10+
const intervalObj = {
11+
month: 0,
12+
day: 0,
13+
hour: 0,
14+
minute: 0,
15+
second: 0,
16+
};
17+
const startDate = new Date(start).getTime() / 1000;
18+
const endDate = new Date(end).getTime() / 1000;
19+
const interval = Math.round(endDate - startDate);
20+
// Months
21+
const remainingDays = intervalToFormat(interval, "month", intervalObj);
22+
// Days
23+
const remainingHours = intervalToFormat(remainingDays, "day", intervalObj);
24+
// Hours
25+
const remainingMinutes = intervalToFormat(remainingHours, "hour", intervalObj);
26+
// Minutes
27+
const remainingSeconds = intervalToFormat(remainingMinutes, "minute", intervalObj);
28+
// Seconds
29+
intervalToFormat(remainingSeconds, "second", intervalObj);
30+
31+
return intervalObj;
32+
}
33+
function intervalToFormat(int: number, format: formats, intervalObj: Record<formats, number>): number {
34+
const value = Math.floor(int / toSecondsConverterConstant[format]);
35+
const remaining = value === 0 ? int : int - value * toSecondsConverterConstant[format];
36+
intervalObj[format] = value;
37+
return remaining;
38+
}
39+
export function intervalObjToStr(int: Record<formats, number> | number | Date) {
40+
if (typeof int === "number" || int instanceof Date) int = intervalToDuration(int);
41+
42+
const formatStr = (format: formats) => {
43+
const amount = int[format];
44+
return amount ? `${amount} ${format}${amount === 1 ? "" : "s"}` : "";
45+
};
46+
return (["month", "day", "hour", "minute", "second"] as formats[]).reduce((acc, format) => {
47+
const formatted = formatStr(format);
48+
if (!acc) return formatted;
49+
return formatted ? acc + ", " + formatted : acc;
50+
}, "");
51+
}

0 commit comments

Comments
 (0)