summary refs log tree commit diff homepage
path: root/src/RTD.ts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/RTD.ts93
1 files changed, 93 insertions, 0 deletions
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