summary refs log tree commit diff
path: root/src-slowcord/status/src/gateway.ts
blob: 14944d0950ccc564de18b4153445014300bae5c0 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import "dotenv/config";
import Fosscord from "fosscord-gopnik";
import Discord from "discord.js";
import mysql from "mysql2";
import fetch from "node-fetch";

const dbConn = mysql.createConnection(process.env.DATABASE as string);
const executePromise = (sql: string, args: any[]) => new Promise((resolve, reject) => dbConn.execute(sql, args, (err, res) => { if (err) reject(err); else resolve(res); }));
const savePerf = async (time: number, name: string, error?: string | Error) => {
	if (error && typeof error != "string") error = error.message;
	try {
		await executePromise("INSERT INTO performance (value, endpoint, timestamp, error) VALUES (?, ?, ?, ?)", [time ?? 0, name, new Date(), error ?? null]);
		// await executePromise("DELETE FROM performance WHERE DATE(timestamp) < now() - interval ? DAY", [process.env.RETENTION_DAYS]);
	}
	catch (e) {
		console.error(e);
	}
};

var timestamp: number | undefined;

const doMeasurements = async (channel: Discord.TextChannel) => {
	timestamp = Date.now();
	await channel.send("hello this is a special message kthxbye");

	setTimeout(doMeasurements, parseInt(process.env.MEASURE_INTERVAL as string), channel);
};

const instance = {
	app: process.env.INSTANCE_WEB_APP as string,
	api: process.env.INSTANCE_API as string,
	cdn: process.env.INSTANCE_CDN as string,
	token: process.env.INSTANCE_TOKEN as string,
};

const client = new Fosscord.Client({
	intents: [],
	http: {
		api: instance.api,
		cdn: instance.cdn
	}
});

client.on("ready", async () => {
	console.log(`Ready on gateway as ${client.user!.tag}`);

	const channel = await client.channels.fetch("1019955729054267764");
	if (!channel) return;

	doMeasurements(channel as Discord.TextChannel);
});

client.on("messageCreate", async (msg: Discord.Message) => {
	if (!timestamp) return;
	if (msg.author.id != "992745947417141682"
		|| msg.channel.id != "1019955729054267764"
		|| msg.content != "hello this is a special message kthxbye")
		return;
	await savePerf(Date.now() - timestamp, "messageCreate", undefined);
	timestamp = undefined;

	await fetch(`${instance.api}/channels/1019955729054267764/messages/${msg.id}`, {
		method: "DELETE",
		headers: {
			authorization: instance.token
		}
	})
});

client.on("error", (error: any) => {
	console.log(`Gateway error`, error);
});

client.on("warn", (msg: any) => {
	console.log(`Gateway warning:`, msg);
});

(async () => {
	await new Promise((resolve) => dbConn.connect(resolve));
	console.log("Connected to db");
	await client.login(instance.token);
})();