Skip to content

Commit 379b355

Browse files
add status command
1 parent 9f9144d commit 379b355

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ A Discord bot to display documentation
55
## Currently supported docs
66

77
* discord.js (includes main/stable branch, includes djs-voice, djs-builders and djs-collection documentations)
8+
* Javascript ([mdn](https://developer.mozilla.org/))
89

910
## Docker
1011

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "tph_docs_bot",
33
"version": "1.0.0",
4-
"description": "A bot made for TPH (The Programmers Hangout - discord.gg/programming) ",
4+
"description": "A Discord bot to display documentation ",
55
"main": "dist/bot.js",
66
"scripts": {
77
"tsc": "tsc",
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"@discordjs/builders": "^0.8.2",
1919
"@discordjs/rest": "^0.1.0-canary.0",
20+
"date-fns": "^2.27.0",
2021
"discord-api-types": "^0.25.2",
2122
"discord.js": "^13.3.1",
2223
"discord.js-docs": "github:BenjammingKirby/discord.js-docs#typescriptRewrite",

src/bot.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
import "dotenv/config";
2-
import { Client, Collection } from "discord.js";
2+
import { Client, Collection, LimitedCollection } from "discord.js";
33
import { MyContext } from "./interfaces";
44
import { loadCommands, commandHandler } from "./handlers/CommandHandler";
5+
import { messageHandler } from "./handlers/MessageHandler";
56

67
(async function () {
78
const context: MyContext = {
89
client: new Client({
9-
intents: ["GUILDS"],
10+
intents: ["GUILDS", "GUILD_MESSAGES"],
1011
presence: {
11-
activities: [{ type: "WATCHING", name: "Discord.JS channel" }],
12+
activities: [{ type: "PLAYING", name: "Read the docs" }],
1213
status: "online",
1314
},
14-
// For DMs, a partial channel object is received, in order to receive dms, CHANNEL partials must be activated
15+
// For DMs, a partial channel object is received, in order to receive dms, CHANNEL partials must be activated
1516
partials: ["CHANNEL"],
17+
makeCache: (manager) => {
18+
//! Disabling these caches will break djs funcitonality
19+
const unsupportedCaches = [
20+
"GuildManager",
21+
"ChannelManager",
22+
"GuildChannelManager",
23+
"RoleManager",
24+
"PermissionOverwriteManager",
25+
];
26+
if (unsupportedCaches.includes(manager.name)) return new Collection();
27+
// Disable every supported cache
28+
return new LimitedCollection({ maxSize: 0 });
29+
},
1630
}),
1731
commands: new Collection(),
1832
cooldownCounter: new Collection(),
1933
};
2034
const docsBot = context.client;
2135
await loadCommands(context);
36+
2237
docsBot.on("error", console.error);
2338
docsBot.on("warn", console.warn);
24-
docsBot.on("ready", (client) => {
39+
40+
docsBot.once("ready", (client) => {
2541
console.info(`Logged in as ${client.user.tag} (${client.user.id})`);
2642
});
43+
44+
docsBot.on("messageCreate", messageHandler);
2745
docsBot.on("interactionCreate", commandHandler.bind(null, context));
2846

2947
docsBot.login(process.env.TOKEN);

src/handlers/MessageHandler.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { intervalToDuration } from "date-fns";
2+
import { Formatters, MessageEmbed } from "discord.js";
3+
import type { Message } from "discord.js";
4+
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}>`);
9+
10+
if (message.content.trim().match(mentionRegex)) {
11+
const pkgJSONPath = "../../package.json";
12+
const pkgJSON = await import(pkgJSONPath);
13+
const { version, description, dependencies } = pkgJSON;
14+
15+
const uptime = intervalToDuration({
16+
start: new Date().getTime() - message.client.uptime,
17+
end: new Date().getTime(),
18+
});
19+
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);
43+
}
44+
return;
45+
}

src/handlers/RegisterCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const rest = new REST({ version: "9" }).setToken(token);
3939
? "Resetting"
4040
: "Reloading"
4141
} application (/) commands for ${applicationId} ${
42-
guildId ? `on guild ${guildId}` : ""
42+
["GUILD", "RESET_GUILD"].includes(registerMode) ? `on guild ${guildId}` : ""
4343
}: \n\t${commands
4444
.map((cmd) => cmd.name)
4545
.join("\n\t")}\nOn mode: "${registerMode}"`

0 commit comments

Comments
 (0)