summary refs log tree commit diff homepage
path: root/src/Discord.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/Discord.ts')
-rw-r--r--src/Discord.ts23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/Discord.ts b/src/Discord.ts
index 2b086ae..3a4dab8 100644
--- a/src/Discord.ts
+++ b/src/Discord.ts
@@ -1,11 +1,12 @@
-import {Client, Intents, Message, TextChannel, User} from 'discord.js'
+import { Client, Intents, Message, TextChannel, User } from 'discord.js'
 
 import emojiStrip from 'emoji-strip'
 import axios from 'axios'
 
-import { Config } from './Config'
+import type { Config } from './Config'
 
 import Rcon from './Rcon'
+import { escapeUnicode } from './lib/util'
 
 class Discord {
   config: Config
@@ -143,9 +144,9 @@ class Discord {
       }
     } else {
       if (this.config.MINECRAFT_TELLRAW_DOESNT_EXIST) {
-        command = `/say ${this.makeMinecraftTellraw(message)}`
+        command = `/say ${this.makeMinecraftMessage(message)}`
       } else {
-        command = `/tellraw @a ${this.makeMinecraftTellraw(message)}`
+        command = `/tellraw @a ${this.makeMinecraftMessage(message)}`
       }
     }
 
@@ -170,17 +171,19 @@ class Discord {
     rcon.close()
   }
 
-  private makeMinecraftTellraw(message: Message): string {
+  private makeMinecraftMessage(message: Message): string {
     const username = emojiStrip(message.author.username)
+
     const variables: {[index: string]: string} = {
       username,
       nickname: !!message.member?.nickname ? emojiStrip(message.member.nickname) : username,
       discriminator: message.author.discriminator,
-      text: emojiStrip(message.cleanContent)
+      text: emojiStrip(message.cleanContent),
     }
-    // hastily use JSON to encode the strings
-    for (const v of Object.keys(variables)) {
-      variables[v] = JSON.stringify(variables[v]).slice(1,-1)
+
+    // use JSON to encode the strings for tellraw
+    for (const [k, v] of Object.entries(variables)) {
+      variables[k] = JSON.stringify(v).slice(1,-1)
     }
     
     if (this.config.MINECRAFT_TELLRAW_DOESNT_EXIST) {
@@ -191,6 +194,8 @@ class Discord {
         .replace(/%message%/g, variables.text)
     }
 
+    variables.text = escapeUnicode(variables.text)
+
     return this.config.MINECRAFT_TELLRAW_TEMPLATE
       .replace(/%username%/g, variables.username)
       .replace(/%nickname%/g, variables.nickname)