first steps

This commit is contained in:
mightypanders 2021-11-08 17:34:26 +01:00
parent df9cfda340
commit 39e4ca69f1
9 changed files with 247 additions and 0 deletions

15
index.ts Normal file
View File

@ -0,0 +1,15 @@
import express, { Application } from "express"
import { config } from "./server/configuration"
import { commandList } from "./server/constants"
import DiscordAdapter from "./server/discordAdapter"
import Routes from "./server/routes"
import Server from "./server/server"
const server = Server.init(config.port)
server.start(() => {
console.log(`Server running on port ${server.getPort()}`)
new Routes().setRoutes(server.getApp())
const discordAdapter = new DiscordAdapter()
discordAdapter.registerCommands(commandList)
})

14
server/MuteHandler.ts Normal file
View File

@ -0,0 +1,14 @@
export default class MuteHandler {
public mute(player: string): boolean {
console.log(`Performing mute wizardry on ${player}`)
return true
}
public unmute(player: string): boolean {
console.log(`Performing unmute wizardry on ${player}`)
return true
}
public unmuteAll(): boolean {
console.log(`Performing unmute wizardry on all players`)
return true
}
}

View File

@ -0,0 +1,14 @@
export default class RegistrationHandler {
private register(): void {
console.log('Registering')
}
private removeUser(): void { }
private getNameRegisteredForDiscordUser(): string { return "" }
private getNameRegisteredForSteamUser(): string { return "" }
private listRegisteredMembers(): void { }
private getMemberForPlayer(): any { return "" }
private isRegistered(): boolean { return false }
private printHelpText(): void { }
private buildHelpText(): string { return "" }
}

24
server/configuration.ts Normal file
View File

@ -0,0 +1,24 @@
export const config = {
server: {
bodyParser: {
urlEncodedOptions: {
inflate: true,
limit: '5mb',
type: 'application/x-www-form-urlencoded',
extended: true,
parameterLimit: 1000
},
jsonOptions: {
inflate: true,
limit: '5mb',
type: 'application/json',
strict: true
}
}
},
debug: true,
port: 1234,
token: "discord-bot-token",
guild_id: "id",
client_id: "id"
}

78
server/constants.ts Normal file
View File

@ -0,0 +1,78 @@
import { localized_string } from "./interfaces"
export const reactions = {
troll_grin: "U+1F92A",
angry_face: "U+1F621",
ok: "U+1F44C"
}
export const commands = {
LIST_COMMAND: "list",
REGISTER_COMMAND: "register",
REMOVE_COMMAND: "remove",
SHOW_FOR_STEAM_COMMAND: "forsteam",
SHOW_COMMAND: "show"
}
export interface discordCommand {
name: string,
description: string
}
export const commandList: discordCommand[] = [
{
name: commands.LIST_COMMAND,
description: "Lists all registerd users"
},
{
name: commands.REMOVE_COMMAND,
description: "Remove the mapping for this discord user"
},
{
name: commands.REGISTER_COMMAND,
description: "Register the senders discord name for a supplied steam name"
},
{
name: commands.SHOW_FOR_STEAM_COMMAND,
description: "Show the discord that's registered for a steam name"
},
{
name: commands.SHOW_COMMAND,
description: "Show the Steamname that's registered for this discord name"
}
]
export const msg_strings: localized_string = {
greeting: {
german: "Ich wurde neugestartet. Bitte registriert euch erneut, falls ihr automatisch gemutet werden wollt :)",
english: "I have been restarted. Please register again if you want to be muted automatically :)"
},
fmt_regsitered_user: {
german: "Habe den Steamname {steam_name} mit dem Discordnamen {discord_name} verknüpft.",
english: "Registered the steam name {steam_name} for the discord name {discord_name}."
},
fmt_registered_for_steam: {
german: "Aktuell registriert für User {steam_name}: {discord_name}",
english: "Currently registered for user {steam_name}: {discord_name}"
},
fmt_registered_for_discord: {
german: "Aktuell registriert für User {discord_name}: {steam_name}",
english: "Currently registered for user {discord_name}: {steam_name}"
},
user_was_not_registered: {
german: "Du warst gar nicht registriert.",
english: "You weren't even registered.",
},
user_has_been_removed: {
german: "Du wurdest aus der Liste entfernt.",
english: "You were removed from the list.",
},
currently_registered: {
german: "Aktuell registriert: \n {playerlist}",
english: "Currently registered: \n {playerlist}"
},
troll_rejection: {
german: "Nöööö, du nicht...",
english: "Naaaah, not you..."
},
troll_rejection_second_part: {
german: "Spaß, hab dich registriert: :P",
english: "Just kidding, you are registered: :P"
}
}

20
server/discordAdapter.ts Normal file
View File

@ -0,0 +1,20 @@
import { REST } from '@discordjs/rest'
import { commandList, discordCommand } from './constants'
import { Routes } from 'discord-api-types/v9'
import { config } from './configuration'
export default class DiscordAdapter {
private rest: REST
public constructor() {
this.rest = new REST({ version: '9' }).setToken(config.token)
}
public async registerCommands(pCommands: discordCommand[]) {
if (!config.debug)
try {
console.log('Refreshing slash commands')
await this.rest.put(Routes.applicationGuildCommands(config.client_id, config.guild_id), { body: pCommands })
console.log('Successfully refreshed slash commands')
} catch (error) {
console.log(`Error refreshing slash commands: ${error}`)
}
}
}

10
server/interfaces.ts Normal file
View File

@ -0,0 +1,10 @@
export interface Player {
name: string
}
export type supported_languages = "german" | "english"
export interface localized_string {
[k: string]: {
german: string,
english: string
}
}

36
server/routes.ts Normal file
View File

@ -0,0 +1,36 @@
import express from "express";
import MuteHandler from "./MuteHandler";
export default class Routes {
public constructor(
private muteHandler = new MuteHandler()
) { }
public setRoutes(app: express.Application): void {
app.route('').get(this.landingPage.bind(this))
app.route('/').get(this.landingPage.bind(this))
app.route('/mute').post(this.mutePlayer.bind(this))
app.route('/unmute/all').post(this.unmuteAll.bind(this))
app.route('/unmute/:id').post(this.unmutePlayer.bind(this))
}
private async mutePlayer(req: express.Request, res: express.Response): Promise<void> {
const playerName = req.body.name
console.log(`Muting player ${playerName}`)
this.muteHandler.mute(playerName)
res.status(200).json()
}
private async unmuteAll(_: express.Request, res: express.Response): Promise<void> {
console.log(`Unmuting all players`)
this.muteHandler.unmuteAll()
res.status(200).json()
}
private async unmutePlayer(req: express.Request, res: express.Response): Promise<void> {
const playerName = req.body.name
console.log(`Unmuting player ${playerName}`)
this.muteHandler.unmute(playerName)
res.status(200).json()
}
private async landingPage(_: express.Request, res: express.Response): Promise<void> {
res.send('Hello World')
}
}

36
server/server.ts Normal file
View File

@ -0,0 +1,36 @@
import bodyParser from "body-parser"
import cors from "cors"
import express from "express"
import { config } from "./configuration"
export default class Server {
private app: express.Application
private port: number
public constructor(port: number) {
this.port = port
this.app = express()
}
public static init(port: number): Server {
return new Server(port)
}
public start(callback: (...args: any[]) => void): void {
this.setBodyParser()
this.setCors()
this.getApp().listen(this.port, callback)
}
public getApp(): express.Application {
return this.app
}
public getPort(): number {
return this.port
}
private setBodyParser(): void {
this.getApp().use(bodyParser.urlencoded(config.server.bodyParser.urlEncodedOptions))
this.getApp().use(bodyParser.json(config.server.bodyParser.jsonOptions))
}
private setCors(): void {
this.getApp().use(cors())
}
}