From c78139e8373092136fa7c7decd886b807cfae08c Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 14 Jan 2023 22:43:07 +0000 Subject: Add a complete mess of an RTD command --- src/Config.ts | 4 +++ src/RTD.ts | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Shulker.ts | 11 ++++++- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/RTD.ts (limited to 'src') 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 { + 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() } } -- cgit 1.4.1