|
1 | | -import { intervalToDuration } from "date-fns"; |
2 | 1 | import { Formatters, MessageEmbed } from "discord.js"; |
3 | 2 | import type { Message } from "discord.js"; |
| 3 | +import { intervalToDuration, intervalObjToStr } from "../utils/DateUtils"; |
4 | 4 |
|
5 | 5 | 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}>`); |
9 | 10 |
|
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; |
14 | 15 |
|
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 | + ); |
19 | 36 |
|
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); |
43 | 41 | } |
| 42 | + |
44 | 43 | return; |
45 | 44 | } |
0 commit comments