summary refs log tree commit diff homepage
path: root/src/Shulker.ts
blob: 16921d61762198f45537acff52899a7c5f59b907 (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
import DiscordClient from './Discord'
import Handler, { LogLine } from './MinecraftHandler'

import { Config } from './Config'

class Shulker {
  config: Config
  discordClient: DiscordClient
  handler: Handler

  constructor() {
  }

  loadConfig () {
    const configFile = (process.argv.length > 2) ? process.argv[2] : '../config.json'
    console.log('[INFO] Using configuration file:', configFile)
    this.config = require(configFile)
    if (!this.config) {
      console.log('[ERROR] Could not load config file!')
      return false
    }

    if (this.config.USE_WEBHOOKS) {
      console.log('[INFO] Using Discord WebHooks to send messages')
    } else {
      console.log('[INFO] Using Discord bot to send messages')
    }

    return true
  }

  onDiscordReady () {
    this.handler.init(async (data: LogLine) => {
      if (data) {
        const { username, message } = data
        await this.discordClient.sendMessage(username, message)
      }
    })
  }

  async init () {
    const loaded = this.loadConfig()
    if (!loaded) return

    this.discordClient = new DiscordClient(this.config, () => this.onDiscordReady())
    this.handler = new Handler(this.config)

    await this.discordClient.init()
  }
}

export default Shulker