@statcord-ts/seedcord-plugin 0.0.0 → 0.0.1-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2026 Vorasion Development
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @statcord-ts/seedcord-plugin
2
+
3
+ The Seedcord plugin for Statcord.
4
+
5
+ ---
6
+
7
+ Don't install this yet, we're still working out all the kinks.
@@ -0,0 +1,23 @@
1
+ import { CoreBase, Plugin } from "@seedcord/gateway";
2
+ import { Statcord, StatcordChart, StatcordCommand } from "@statcord-ts/core";
3
+ import { StatcordPluginOptions } from "@statcord-ts/plugin-options";
4
+ //#region src/index.d.ts
5
+ export interface SeedcordStatcordPluginOptions extends StatcordPluginOptions {
6
+ allowGuildCommands?: boolean;
7
+ }
8
+ export declare class StatcordPlugin extends Plugin {
9
+ #private;
10
+ private readonly options?;
11
+ private client;
12
+ readonly charts: Map<string, StatcordChart>;
13
+ readonly commands: Map<string, StatcordCommand>;
14
+ constructor(host: CoreBase, options?: SeedcordStatcordPluginOptions | undefined);
15
+ dispose(): Promise<void>;
16
+ init(): Promise<void>;
17
+ ready(): Promise<void>;
18
+ stopAutoposting(): void;
19
+ postStats(): Promise<void>;
20
+ addCustomChart(name: string, data: Record<string, number>): void;
21
+ get statcordClient(): Statcord;
22
+ }
23
+ //#endregion
package/dist/index.mjs ADDED
@@ -0,0 +1,134 @@
1
+ import { InteractionKind, Plugin } from "@seedcord/gateway";
2
+ import { Statcord } from "@statcord-ts/core";
3
+ import os from "node:os";
4
+ import { OSUtils } from "node-os-utils";
5
+ //#region src/eventHandler.ts
6
+ var PluginEventHandler = class {
7
+ commands;
8
+ options;
9
+ #acceptedInteractionKinds = [
10
+ InteractionKind.MessageContextMenu,
11
+ InteractionKind.Slash,
12
+ InteractionKind.UserContextMenu
13
+ ];
14
+ onCommandsDeployed = (event) => this.#addDeployedCommandsToCommands(event);
15
+ onInteractionDispatched = (event) => this.#handleCommandInteraction(event);
16
+ constructor(commands, options) {
17
+ this.commands = commands;
18
+ this.options = options;
19
+ }
20
+ #addDeployedCommandsToCommands(event) {
21
+ for (const command of event.global) {
22
+ if (this.commands.has(command.name)) continue;
23
+ this.commands.set(command.name, {
24
+ name: command.name,
25
+ count: 0
26
+ });
27
+ }
28
+ if (this.options?.allowGuildCommands) for (const commands of Object.values(event.guilds)) for (const command of commands) {
29
+ if (this.commands.has(command.name)) continue;
30
+ this.commands.set(command.name, {
31
+ name: command.name,
32
+ count: 0
33
+ });
34
+ }
35
+ }
36
+ #getCommandName(routeId) {
37
+ const name = routeId.slice(routeId.indexOf(":") + 1);
38
+ const end = name.indexOf("/");
39
+ return end === -1 ? name : name.slice(0, end);
40
+ }
41
+ #handleCommandInteraction({ kind, outcome, routeId }) {
42
+ if (!this.#acceptedInteractionKinds.includes(kind) || outcome === "failed") return;
43
+ const command = this.commands.get(this.#getCommandName(routeId));
44
+ if (command) command.count++;
45
+ }
46
+ };
47
+ //#endregion
48
+ //#region src/helpers.ts
49
+ const osUtils = new OSUtils();
50
+ const getCpuUsage = () => osUtils.cpu.usage();
51
+ const getRamUsage = () => (getTotalMemory() - os.freemem()) / getTotalMemory() * 100;
52
+ const getTotalMemory = () => os.totalmem();
53
+ //#endregion
54
+ //#region src/index.ts
55
+ var StatcordPlugin = class extends Plugin {
56
+ options;
57
+ #autopostingInterval = null;
58
+ #eventHandler;
59
+ charts = /* @__PURE__ */ new Map();
60
+ commands = /* @__PURE__ */ new Map();
61
+ constructor(host, options) {
62
+ super(host);
63
+ this.options = options;
64
+ this.#eventHandler = new PluginEventHandler(this.commands, this.options);
65
+ if (!this.options?.apiKey && !process.env["STATCORD_API_KEY"]) this.rejectOptions("apiKey is required.");
66
+ if (this.options?.httpTimeout !== void 0 && (!Number.isFinite(this.options.httpTimeout) || this.options.httpTimeout <= 0)) this.rejectOptions("httpTimeout must be greater than 0.");
67
+ }
68
+ dispose() {
69
+ this.stopAutoposting();
70
+ this.core.bus.off("commandsDeployed", this.#eventHandler.onCommandsDeployed);
71
+ this.core.bus.off("interactionDispatched", this.#eventHandler.onInteractionDispatched);
72
+ return Promise.resolve();
73
+ }
74
+ init() {
75
+ this.logger.info("Statcord plugin initialized.");
76
+ this.core.bus.on("commandsDeployed", this.#eventHandler.onCommandsDeployed);
77
+ this.core.bus.on("interactionDispatched", this.#eventHandler.onInteractionDispatched);
78
+ return Promise.resolve();
79
+ }
80
+ ready() {
81
+ this.client = new Statcord(this.core.applicationId, this.options?.apiKey, this.options?.httpTimeout);
82
+ const autopost = this.options?.autopost;
83
+ if ((autopost === true || typeof autopost === "object" && autopost !== null && autopost.enabled) && !this.#autopostingInterval) this.#autopostingInterval = setInterval(() => void this.postStats().catch((err) => this.logger.error("Failed to autopost stats:", err)), typeof this.options?.autopost === "object" ? this.options.autopost.interval ?? 6e4 : 6e4);
84
+ return Promise.resolve();
85
+ }
86
+ stopAutoposting() {
87
+ if (this.#autopostingInterval) {
88
+ clearInterval(this.#autopostingInterval);
89
+ this.#autopostingInterval = null;
90
+ }
91
+ }
92
+ async postStats() {
93
+ const guilds = this.core.bot.client.guilds.cache;
94
+ const guildCount = guilds.size;
95
+ const shardCount = this.core.bot.client.ws.shards.size;
96
+ const userCount = this.core.bot.client.users.cache.size;
97
+ const memberCount = guilds.reduce((acc, guild) => acc + guild.memberCount, 0);
98
+ const builder = this.statcordClient.createPayloadBuilder().setGuildCount(guildCount).setShardCount(shardCount).setUserCount(userCount).setMemberCount(memberCount).setCustomCharts(Array.from(this.charts.values())).setTopCommands(Array.from(this.commands.values()));
99
+ if (typeof this.options?.autopost !== "object") {
100
+ const cpuUsage = await getCpuUsage();
101
+ const ramUsage = getRamUsage();
102
+ builder.setTotalRam(getTotalMemory()).setRamUsage(ramUsage);
103
+ if (cpuUsage.success) builder.setCpuUsage(cpuUsage.data);
104
+ else this.logger.warn("Failed to retrieve CPU usage, skipping it.");
105
+ await this.statcordClient.postStats(builder.build());
106
+ return;
107
+ }
108
+ const autopostingOptions = typeof this.options?.autopost === "object" ? this.options.autopost : {};
109
+ if (autopostingOptions.includeRam) builder.setTotalRam(getTotalMemory()).setRamUsage(getRamUsage());
110
+ if (autopostingOptions.includeCpu) {
111
+ const cpuUsage = await getCpuUsage();
112
+ if (!cpuUsage.success) {
113
+ this.logger.warn("Failed to retrieve CPU usage, doing a push without it.");
114
+ await this.statcordClient.postStats(builder.build());
115
+ return;
116
+ }
117
+ builder.setCpuUsage(cpuUsage.data);
118
+ }
119
+ await this.statcordClient.postStats(builder.build());
120
+ }
121
+ addCustomChart(name, data) {
122
+ if (this.charts.has(name)) return;
123
+ this.charts.set(name, {
124
+ id: name,
125
+ data
126
+ });
127
+ }
128
+ get statcordClient() {
129
+ if (!this.client) throw new Error("Statcord client has not been initialized fully yet, wait until after login before accessing the client.");
130
+ return this.client;
131
+ }
132
+ };
133
+ //#endregion
134
+ export { StatcordPlugin };
package/package.json CHANGED
@@ -1,14 +1,48 @@
1
1
  {
2
2
  "name": "@statcord-ts/seedcord-plugin",
3
- "version": "0.0.0",
4
- "description": "Placeholder for @statcord-ts/seedcord-plugin",
5
- "main": "index.js",
6
- "scripts": {},
7
- "keywords": [],
8
- "author": "lunaradev (https://www.npmjs.com/~lunaradev)",
9
- "license": "UNLICENSED",
3
+ "version": "0.0.1-dev",
10
4
  "private": false,
5
+ "description": "The Seedcord plugin for Statcord.",
6
+ "homepage": "https://github.com/Vorasion-Development/statcord.ts#readme",
7
+ "bugs": {
8
+ "url": "https://github.com/Vorasion-Development/statcord.ts/issues"
9
+ },
10
+ "license": "MIT",
11
+ "author": "Vorasion Development",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/Vorasion-Development/statcord.ts.git"
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "LICENSE",
19
+ "README.md",
20
+ "package.json"
21
+ ],
22
+ "type": "module",
23
+ "exports": {
24
+ ".": "./dist/index.mjs",
25
+ "./package.json": "./package.json"
26
+ },
11
27
  "publishConfig": {
12
28
  "access": "public"
13
- }
14
- }
29
+ },
30
+ "dependencies": {
31
+ "@sapphire/timestamp": "^1.0.5",
32
+ "@statcord-ts/core": "^0.0.1-dev",
33
+ "@statcord-ts/plugin-options": "^0.0.1-dev",
34
+ "node-os-utils": "^3.1.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^26.1.1",
38
+ "bumpp": "^11.1.0",
39
+ "tsdown": "^0.23",
40
+ "typescript": "^7.0.2",
41
+ "vitest": "^5.0.4"
42
+ },
43
+ "peerDependencies": {
44
+ "@seedcord/gateway": "^0.7.5",
45
+ "discord.js": "14"
46
+ },
47
+ "readme": "README.md"
48
+ }
package/index.js DELETED
@@ -1 +0,0 @@
1
- // Placeholder