-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
80 lines (66 loc) · 2.18 KB
/
app.ts
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
import { Presence } from './presence.js'
import { LogReader } from './service/logReader.js'
import { DiscordClient } from './service/discordClient.js'
import { Rcon } from './service/rcon.js'
import {
logFile, timestampPattern,
rconHost, rconPort, rconPassword, senderColor,
discordToken, channelId, webhookName, loginTimeout,
} from './config.js'
const reader = new LogReader(logFile, timestampPattern)
const rcon = new Rcon(rconHost, rconPort, rconPassword, senderColor)
const discord = new DiscordClient(discordToken, channelId, webhookName, loginTimeout)
const services = [reader, rcon, discord]
const main = async () => {
for (const service of services) {
console.debug(`Initializing ${service.constructor.name}`)
await service.initialize()
}
const ready = Date.now()
setInterval(async () => {
const players = await rcon.getPlayers()
const presence = new Presence(players, ready)
discord.setPresence(presence)
}, 10000)
reader.register('\\[Server thread\\/INFO]: <', logOutput => {
const matches = logOutput.match(/<\w+>/)
if (!matches) {
return
}
const sender = matches[0].replace(/[<>]/g, '')
const content = logOutput.substring(logOutput.indexOf('>') + 1)
discord.send(sender, content)
})
discord.onMessage(message => {
const sender = message.author.displayName
const content = message.content
.replace(/\\/g, '')
.replace(/"/g, '\\"')
.replace(/(\r\n|\n|\r)/gm, ' ')
rcon.send(sender, content)
})
}
const terminateServices = async () => {
for (const service of services) {
console.debug(`Terminating ${service.constructor.name}`)
await service.terminate().catch(e => {
console.error(`Failed to terminate ${service.constructor.name}`, e)
})
}
}
process.on('SIGTERM', async () => {
console.log('Received SIGTERM signal. Gracefully shutting down...')
await terminateServices()
process.exit(0)
})
process.on('SIGINT', async () => {
console.log('Received SIGINT signal. Gracefully shutting down...')
await terminateServices()
process.exit(0)
})
process.on('uncaughtException', async (error) => {
console.error('Uncaught Exception. Exiting...', error)
await terminateServices()
process.exit(1)
})
main().then(() => console.log('Bot started'))