-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
184 lines (158 loc) · 5.93 KB
/
index.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import express from "express";
import WebSocket from "ws";
import cors from "cors";
import { D3_discord } from "./src/Core/client.js";
import { Demantle } from "./src/Demantle/Demantle.js";
import { socketMessageClient } from "./src/Typings/socket.js";
import { handleSocketReply, searchGarticAnswer } from "./src/Core/functions.js";
import { GuildTextBasedChannel } from "discord.js";
console.log("Rebooted");
let WebsiteClass: Record<string, Demantle[]> = {};
export const client = new D3_discord();
client.start();
const app = express();
app.use(cors());
app.use(express.text());
app.use(express.static("./Bhotianaa"));
app.use(express.static("./src/Gartic/public"));
const port = 3000;
const botServer = app.listen(port, () =>
console.log(`API server listening on port ${port}`),
);
app.get("/", (_req, res) => {
let data = Object.keys(WebsiteClass);
res.send(
`Connected Clients: ${data.length}\nList of Clients:\n${data.join("\n")}`,
);
});
app.get("/gartic", (req, res) => {
res.sendFile("index.html", { root: "./src/Gartic" });
})
app.get("/r/:id", (req, res) => {
res.sendFile("system.html", { root: "./src/Gartic/system" });
});
app.get("/ping", (req, res) => res.json({
return: true
}));
app.get("/d3mantle", (req, res) => {
res.sendFile("index.html", { root: "./src/Demantle/" });
});
app.get("/d3mantle/style.css", (req, res) => {
res.sendFile("style.css", { root: "./src/Demantle/" });
});
app.get("/d3mantle/script.js", (req, res) => {
res.sendFile("script.js", { root: "./src/Demantle/" });
});
app.get("/stopots", (req, res) => {
res.sendFile("StopotS.html", { root: "./src/StopotS/" });
});
app.get("/stopots/script.js", (req, res) => {
res.sendFile("script.js", { root: "./src/StopotS/" });
});
app.get("/bhotianaa/version", (req, res) => {
res.sendFile("Version.txt", { root: "./Bhotianaa" });
});
app.get("/bhotianaa/update", (req, res) => {
res.sendFile("Bhotianaa.zip", { root: "./Bhotianaa" });
});
app.get("/getmessagedata/:channelid/:messageid", async (req, res) => {
const channelId = req.params.channelid;
const messageId = req.params.messageid;
try {
const json = (
await (
(await client.channels.fetch(
channelId,
)) as GuildTextBasedChannel
).messages.fetch(messageId)
).toJSON();
res.json(json);
} catch (error) {
const err = error as Error;
res.json({
error: "An error ocurred",
errorName: err.name,
errorMessage: err.message,
});
}
});
app.get("/getusername/:userid", async (req, res) => {
const userId = req.params.userid;
const { username } = await client.users.fetch(userId);
res.send(username);
});
app.post("/garticfind", (req, res) => {
try {
let data = JSON.parse(req.body);
res.json(searchGarticAnswer(data.query));
} catch (err) {
console.log(req.body);
res.json({
error: `It's json bro JSON`,
resolution: `JSON.stringify({ query: "S _ _ _ _" })`,
explanation: `"query" takes gartic hint as input only`,
});
}
});
const d3socket = new WebSocket.Server({ server: botServer });
d3socket.on("connection", (ws) => {
ws.send(
`{ "Server": [ "D3_connect", { "message": "Initiate connection" } ] }`,
);
ws.on("message", async (res) => {
if (res.toString() === "2") return ws.send("3");
let message = JSON.parse(res.toString()) as socketMessageClient;
if (message.Client[0] === "D3_connect") {
if (
message.Client[1].message ===
"Requesting to initiate connection"
) {
const gameId = message.Client[1].gameId;
WebsiteClass[gameId] = [
new Demantle(2),
new Demantle(1),
new Demantle(2),
];
ws.send(
`{ "Server": ["D3_connect", { "message": "Connection Established at ${gameId}" }] }`,
);
}
} else if (message.Client[0] === "D3_guess") {
const gameType = message.Client[1].gameType;
const gameId = message.Client[1].gameId;
const guess = message.Client[1].guess;
const guesser = message.Client[1].guesser;
const index =
gameType === "Random" ? 0 : gameType === "Today" ? 1 : 2;
const res = await WebsiteClass[gameId][index].guess(
guess.toLowerCase(),
guesser,
);
if (res === undefined) return;
if (index === 2) {
d3socket.clients.forEach((client) =>
handleSocketReply(res, client, gameType),
);
} else handleSocketReply(res, ws, gameType);
} else if (message.Client[0] === "D3_giveup") {
const gameType = message.Client[1].gameType;
const index =
gameType === "Random" ? 0 : gameType === "Today" ? 1 : 2;
const gameId = message.Client[1].gameId;
const res = WebsiteClass[gameId][index].GiveUp();
ws.send(`{ "Server": [ "D3_giveup", { "word": "${res}" } ] }`);
} else if (message.Client[0] === "D3_requestData") {
const gameType = message.Client[1].gameType;
const gameId = message.Client[1].gameId;
const index =
gameType === "Random" ? 0 : gameType === "Today" ? 1 : 2;
const res = WebsiteClass[gameId][index].getMetadata();
ws.send(
`{ "Server": [ "D3_requestData", { "guessArr": ${JSON.stringify(res.guesses)} , "indexes": ${JSON.stringify(res.indexes)} , "gameType": "${gameType}" } ] }`,
);
} else if (message.Client[0] === "D3_disconnect") {
let gameId = message.Client[1].gameId;
delete WebsiteClass[gameId];
}
});
});