You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.6 KiB
87 lines
2.6 KiB
import fastify, {FastifyInstance} from "fastify";
|
|
import {SlashCreator, SlashCreatorOptions} from "slash-create";
|
|
import fastifyCookie from "fastify-cookie";
|
|
import {FastifyServerButItWorksUnlikeTheRealOne} from "./FastifyHelpers.js";
|
|
import {Pool} from "pg";
|
|
|
|
export interface BaseServerDeps {
|
|
appId: string
|
|
botToken: string
|
|
clientSecret: string
|
|
publicKey: string
|
|
listenPort: number
|
|
listenAddress: string
|
|
cookieSecret: string
|
|
pool: Pool
|
|
}
|
|
|
|
export class BaseServer {
|
|
readonly server: FastifyInstance
|
|
readonly appId: string
|
|
readonly clientSecret: string
|
|
readonly listenPort: number
|
|
readonly listenAddress: string
|
|
readonly slashcmd: SlashCreator
|
|
readonly pool: Pool
|
|
|
|
constructor({
|
|
cookieSecret,
|
|
appId,
|
|
clientSecret,
|
|
listenPort,
|
|
listenAddress,
|
|
pool,
|
|
publicKey,
|
|
botToken,
|
|
slashCreatorOptions = {}
|
|
}: BaseServerDeps & { slashCreatorOptions?: Partial<SlashCreatorOptions> }) {
|
|
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.pool = pool
|
|
}
|
|
|
|
async _initInternal(): Promise<void> {
|
|
return
|
|
}
|
|
|
|
async initialize(): Promise<void> {
|
|
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<void> {
|
|
return
|
|
}
|
|
|
|
async shutdown(): Promise<void> {
|
|
await this._shutdownInternal()
|
|
return this.server.close()
|
|
}
|
|
} |