import {SavedWebhook} from "./SavedWebhook.js"; import fastify, {FastifyInstance} from "fastify"; import {SlashCreator, SlashCreatorOptions} from "slash-create"; import fastifyCookie from "fastify-cookie"; import {FastifyServerButItWorksUnlikeTheRealOne} from "./FastifyHelpers.js"; export interface BaseServerDeps { appId: string botToken: string clientSecret: string publicKey: string listenPort: number listenAddress: string cookieSecret: string gameWebhook: SavedWebhook adminWebhook: SavedWebhook } export class BaseServer { readonly server: FastifyInstance readonly gameWebhook: SavedWebhook readonly adminWebhook: SavedWebhook readonly appId: string readonly clientSecret: string readonly listenPort: number readonly listenAddress: string readonly slashcmd: SlashCreator constructor({ cookieSecret, appId, clientSecret, listenPort, listenAddress, gameWebhook, adminWebhook, publicKey, botToken, slashCreatorOptions = {} }: BaseServerDeps & { slashCreatorOptions?: Partial }) { this.slashcmd = new SlashCreator({ allowedMentions: {everyone: false, roles: false, users: false}, applicationID: appId, defaultImageFormat: "webp", handleCommandsManually: false, maxSignatureTimestamp: 3000, publicKey, requestTimeout: 10000, token: botToken, unknownCommandResponse: true, endpointPath: "/interactions", ...slashCreatorOptions, }) this.server = fastify({logger: true}) this.slashcmd.withServer(new FastifyServerButItWorksUnlikeTheRealOne(this.server, {alreadyListening: true})) this.server.register(fastifyCookie, { secret: cookieSecret, }) this.appId = appId this.clientSecret = clientSecret this.listenPort = listenPort this.listenAddress = listenAddress this.gameWebhook = gameWebhook this.adminWebhook = adminWebhook } async _initInternal(): Promise { return } async initialize(): Promise { await this._initInternal() await this.slashcmd.syncCommandsAsync({ syncGuilds: true, syncPermissions: true, skipGuildErrors: false, deleteCommands: true, }) await this.slashcmd.startServer() await this.server.listen(this.listenPort, this.listenAddress) } async _shutdownInternal(): Promise { return } async shutdown(): Promise { await this._shutdownInternal() return this.server.close() } }