summary refs log tree commit diff homepage
path: root/src/RTD.ts
blob: 2bb6f533052e4f5133935f7dce7c1dc4dca025b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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