diff options
author | June McEnroe <june@causal.agency> | 2023-01-14 22:43:07 +0000 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2023-01-14 22:43:07 +0000 |
commit | c78139e8373092136fa7c7decd886b807cfae08c (patch) | |
tree | 02ab3d747890daf72f1c737f8c9c39824020ed3a | |
parent | Clean up rcon package callbacks (diff) | |
download | shulker-c78139e8373092136fa7c7decd886b807cfae08c.tar.gz shulker-c78139e8373092136fa7c7decd886b807cfae08c.zip |
-rw-r--r-- | config.example.json | 6 | ||||
-rw-r--r-- | src/Config.ts | 4 | ||||
-rw-r--r-- | src/RTD.ts | 93 | ||||
-rw-r--r-- | src/Shulker.ts | 11 |
4 files changed, 112 insertions, 2 deletions
diff --git a/config.example.json b/config.example.json index 1041466..07f4dbc 100644 --- a/config.example.json +++ b/config.example.json @@ -43,5 +43,9 @@ "SHOW_PLAYER_CONN_STAT": true, "SHOW_PLAYER_ADVANCEMENT": true, "SHOW_PLAYER_DEATH": true, - "SHOW_PLAYER_ME": false + "SHOW_PLAYER_ME": false, + + "RTD_ENABLED": false, + "RTD_REGEX": "^!?[Rr][Tt][Dd]$", + "RTD_COOLDOWN": 60, } diff --git a/src/Config.ts b/src/Config.ts index 8b18aa7..a378fe4 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -47,4 +47,8 @@ export interface Config { SHOW_PLAYER_ADVANCEMENT: boolean SHOW_PLAYER_DEATH: boolean SHOW_PLAYER_ME: boolean + + RTD_ENABLED: boolean + RTD_REGEX: string + RTD_COOLDOWN: number } diff --git a/src/RTD.ts b/src/RTD.ts new file mode 100644 index 0000000..2bb6f53 --- /dev/null +++ b/src/RTD.ts @@ -0,0 +1,93 @@ +import Rcon from './Rcon' +import Discord from './Discord' +import type { Config } from './Config' +import type { LogLine } from './MinecraftHandler' + +const Effects: { [key: string]: string } = { + speed: 'Speed', + slowness: 'Slowness', + haste: 'Haste', + mining_fatigue: 'Mining Fatigue', + strength: 'Strength', + instant_health: 'Instant Health', + instant_damage: 'Instant Damage', + jump_boost: 'Jump Boost', + nausea: 'Nausea', + regeneration: 'Regeneration', + resistance: 'Resistance', + fire_resistance: 'Fire Resistance', + water_breathing: 'Water Breathing', + invisibility: 'Invisibility', + blindness: 'Blindness', + night_vision: 'Night Vision', + hunger: 'Hunger', + weakness: 'Weakness', + poison: 'Poison', + wither: 'Wither', + health_boost: 'Health Boost', + absorption: 'Absorption', + saturation: 'Saturation', + glowing: 'Glowing', + levitation: 'Levitation', + luck: 'Luck', + unluck: 'Bad Luck', + slow_falling: 'Slow Falling', + conduit_power: 'Conduit Power', + dolphins_grace: "Dolphin's Grace", + bad_omen: 'Bad Omen', + hero_of_the_village: 'Hero of the Village', + darkness: 'Darkness', +} + +class RTD { + config: Config + rcon: Rcon + discord: Discord + cooldown: { [key: string]: NodeJS.Timeout } + + constructor(config: Config, discord: Discord) { + this.config = config + this.discord = discord + this.rcon = new Rcon(this.config.MINECRAFT_SERVER_RCON_IP, this.config.MINECRAFT_SERVER_RCON_PORT, this.config.DEBUG) + this.cooldown = {} + } + + public async init() { + try { + await this.rcon.auth(this.config.MINECRAFT_SERVER_RCON_PASSWORD) + } catch (e) { + console.log('[ERROR] Could not auth with the server!') + if (this.config.DEBUG) console.error(e) + process.exit(1) + } + } + + public async handle(username: string, message: string): Promise<LogLine> { + if (!this.config.RTD_ENABLED) return null + + const regex = new RegExp(this.config.RTD_REGEX) + if (!regex.test(message)) return null + + if (username in this.cooldown) { + await this.rcon.command(`tellraw ${username} {"text":"[RTD] You're on cooldown!"}`) + // FIXME: don't send cooldown rtd to discord + return null + } + + this.cooldown[username] = setTimeout(() => { + delete this.cooldown[username] + }, this.config.RTD_COOLDOWN * 1000) + + const keys = Object.keys(Effects) + const effect = keys[Math.floor(Math.random() * keys.length)] + await this.rcon.command(`effect give ${username} ${effect}`) + await this.rcon.command(`tellraw @a {"text":"[RTD] ${username} rolled the dice and got ${Effects[effect]}!"}`) + + return { + username: `${this.config.SERVER_NAME} - Server`, + message: `${username} rolled the dice and got ${Effects[effect]}!`, + } + } +} + +export default RTD diff --git a/src/Shulker.ts b/src/Shulker.ts index 8f6a5bb..7d3d4a1 100644 --- a/src/Shulker.ts +++ b/src/Shulker.ts @@ -2,6 +2,7 @@ import fs from 'fs' import DiscordClient from './Discord' import Handler, { LogLine } from './MinecraftHandler' +import RTD from './RTD' import type { Config } from './Config' @@ -13,6 +14,7 @@ class Shulker { config: Config discordClient: DiscordClient handler: Handler + rtd: RTD readonly deprecatedConfigs: OutdatedConfigMessages = { 'DEATH_KEY_WORDS': '`DEATH_KEY_WORDS` has been replaced with `REGEX_DEATH_MESSAGE`. Please update this from the latest example config.' @@ -69,7 +71,12 @@ class Shulker { this.handler.init(async (data: LogLine) => { if (data) { const { username, message } = data - await this.discordClient.sendMessage(username, message) + const rtd = await this.rtd.handle(username, message) + if (rtd) { + await this.discordClient.sendMessage(rtd.username, rtd.message) + } else { + await this.discordClient.sendMessage(username, message) + } } }) } @@ -80,8 +87,10 @@ class Shulker { this.discordClient = new DiscordClient(this.config, () => this.onDiscordReady()) this.handler = new Handler(this.config) + this.rtd = new RTD(this.config, this.discordClient) await this.discordClient.init() + await this.rtd.init() } } |