Skip to content

Commit 1be1534

Browse files
committed
Refactor: Webserver setup
1 parent ee63f02 commit 1be1534

3 files changed

Lines changed: 48 additions & 51 deletions

File tree

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ TOKEN=
22
CLIENT_ID=
33
PUBLIC_KEY=
44
PORT=
5-
HOSTNAME=
65
EMBED_COLOR=
76
IP_INFO_URL=
87
IP_INFO_USER_AGENT=

src/app.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { API, MessageFlags } from "@discordjs/core/http-only";
22
import { REST } from "@discordjs/rest";
33
import { Collection } from "@discordjs/collection";
44
import { EventEmitter } from "node:events";
5-
import Polka from "polka";
65
import type { ChatInputCommand, ContextMenuCommand } from "./structures/index.js";
76

87
export default class App extends EventEmitter {
9-
public readonly server = Polka();
108
public readonly chatInputCommands = new Collection<string, ChatInputCommand>();
119
public readonly contextMenuCommands = new Collection<string, ContextMenuCommand<"message" | "user">>();
1210
public readonly api = new API(new REST().setToken(process.env.TOKEN!));

src/index.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { readdir } from "node:fs/promises";
1010
import { join } from "node:path";
1111
import { InteractionOptionResolver } from "@sapphire/discord-utilities";
12+
import Polka from "polka";
1213
import App from "./app.js";
1314
import { ChatInputCommand, ContextMenuCommand } from "#structures";
1415
import { log, parser, verifyKey } from "#util";
@@ -37,64 +38,63 @@ for (const folder of await readdir(new URL("commands", import.meta.url))) {
3738
}
3839
}
3940

40-
app.server.use(parser, verifyKey).post("/", async (req, res) => {
41-
const interaction: APIInteraction = req.body;
41+
Polka()
42+
.use(parser, verifyKey)
43+
.post("/", async (req, res) => {
44+
const interaction: APIInteraction = req.body;
4245

43-
app.emit("interaction", interaction);
46+
app.emit("interaction", interaction);
4447

45-
switch (interaction.type) {
46-
case InteractionType.Ping:
47-
res
48-
.setHeader("Content-Type", "application/json")
49-
.writeHead(200)
50-
.end(JSON.stringify({ type: InteractionResponseType.Pong }));
51-
break;
52-
case InteractionType.ApplicationCommand: {
53-
const options = new InteractionOptionResolver(interaction);
48+
switch (interaction.type) {
49+
case InteractionType.Ping:
50+
res
51+
.setHeader("Content-Type", "application/json")
52+
.writeHead(200)
53+
.end(JSON.stringify({ type: InteractionResponseType.Pong }));
54+
break;
55+
case InteractionType.ApplicationCommand: {
56+
const options = new InteractionOptionResolver(interaction);
5457

55-
switch (interaction.data.type) {
56-
case ApplicationCommandType.ChatInput: {
57-
const chatInputCmd = app.chatInputCommands.get(interaction.data.name);
58+
switch (interaction.data.type) {
59+
case ApplicationCommandType.ChatInput: {
60+
const chatInputCmd = app.chatInputCommands.get(interaction.data.name);
5861

59-
if (!chatInputCmd) return log("Red", `Command ${interaction.data.name} not found`);
62+
if (!chatInputCmd) return log("Red", `Command ${interaction.data.name} not found`);
6063

61-
log("White", [
62-
`\x1b[32m${(interaction.member ?? interaction).user!.username}\x1b[37m used `,
63-
`/${interaction.data.name} ${options.getSubcommand(false) ?? ""}\x1b[37m in `,
64-
`#${interaction.channel?.name ?? interaction.channel.id}`
65-
].join("\x1b[32m"));
64+
log("White", [
65+
`\x1b[32m${(interaction.member ?? interaction).user!.username}\x1b[37m used `,
66+
`/${interaction.data.name} ${options.getSubcommand(false) ?? ""}\x1b[37m in `,
67+
`#${interaction.channel?.name ?? interaction.channel.id}`
68+
].join("\x1b[32m"));
6669

67-
await chatInputCmd.run(app, interaction as APIChatInputApplicationCommandInteraction, options);
68-
break;
69-
}
70-
default: {
71-
const contextMenuCmd = app.contextMenuCommands.get(interaction.data.name);
70+
await chatInputCmd.run(app, interaction as APIChatInputApplicationCommandInteraction, options);
71+
break;
72+
}
73+
default: {
74+
const contextMenuCmd = app.contextMenuCommands.get(interaction.data.name);
7275

73-
if (!contextMenuCmd) return log("Red", `Command ${interaction.data.name} not found`);
76+
if (!contextMenuCmd) return log("Red", `Command ${interaction.data.name} not found`);
7477

75-
log("White", [
76-
`\x1b[32m${(interaction.member ?? interaction).user!.username}\x1b[37m used `,
77-
`${interaction.data.name}\x1b[37m in `,
78-
`#${interaction.channel.name ?? interaction.channel.id}`
79-
].join("\x1b[32m"));
78+
log("White", [
79+
`\x1b[32m${(interaction.member ?? interaction).user!.username}\x1b[37m used `,
80+
`${interaction.data.name}\x1b[37m in `,
81+
`#${interaction.channel.name ?? interaction.channel.id}`
82+
].join("\x1b[32m"));
8083

81-
await contextMenuCmd.run(app, interaction as APIContextMenuInteraction, options);
82-
break;
84+
await contextMenuCmd.run(app, interaction as APIContextMenuInteraction, options);
85+
break;
86+
}
8387
}
84-
}
8588

86-
break;
89+
break;
90+
}
91+
default:
92+
log("Yellow", `Interaction type ${interaction.type} not implemented`);
93+
break;
8794
}
88-
case InteractionType.MessageComponent:
89-
log("Yellow", "MessageComponent not implemented");
90-
break;
91-
case InteractionType.ApplicationCommandAutocomplete:
92-
log("Yellow", "ApplicationCommandAutocomplete not implemented");
93-
break;
94-
case InteractionType.ModalSubmit:
95-
log("Yellow", "ModalSubmit not implemented");
96-
break;
97-
}
98-
}).listen(+process.env.PORT!, process.env.HOSTNAME!, () => log("Blue", `Live on port \x1b[33m${process.env.PORT}\x1b[34m`));
95+
})
96+
.listen(parseInt(process.env.PORT!, 10));
97+
98+
log("Blue", `Live on port \x1b[33m${process.env.PORT}\x1b[34m`);
9999

100-
process.on("uncaughtException", console.log);
100+
process.on("uncaughtException", console.error);

0 commit comments

Comments
 (0)