summary refs log tree commit diff homepage
path: root/src/Shulker.ts
diff options
context:
space:
mode:
authordestruc7i0n <dscdsouza@outlook.com>2020-02-04 00:51:15 -0500
committerdestruc7i0n <dscdsouza@outlook.com>2020-02-04 00:51:15 -0500
commit9ac2bbe9781f7ab5a798621cc9dd46b4ff8befda (patch)
treef186c69fb565b09fa30357aaea572ad7f71b91bd /src/Shulker.ts
parentMerge pull request #37 from destruc7i0n/webhooks (diff)
downloadshulker-9ac2bbe9781f7ab5a798621cc9dd46b4ff8befda.tar.gz
shulker-9ac2bbe9781f7ab5a798621cc9dd46b4ff8befda.zip
Refactor and rebuild to TypeScript
Diffstat (limited to 'src/Shulker.ts')
-rw-r--r--src/Shulker.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/Shulker.ts b/src/Shulker.ts
new file mode 100644
index 0000000..9de927d
--- /dev/null
+++ b/src/Shulker.ts
@@ -0,0 +1,56 @@
+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
+  }
+
+  fixMinecraftUsername (username: string) {
+    return username.replace(/(ยง[A-Z-a-z0-9])/g, '')
+  }
+
+  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