summary refs log tree commit diff homepage
path: root/src/Discord.ts
diff options
context:
space:
mode:
authordestruc7i0n <destruc7i0n@users.noreply.github.com>2021-12-29 16:39:46 -0500
committerdestruc7i0n <destruc7i0n@users.noreply.github.com>2021-12-29 16:39:46 -0500
commita04d39a3cbf10e7335889615a36a78827857b566 (patch)
treeab132aeeb0e931e1131c1a46182a9cb5a7e882e5 /src/Discord.ts
parentadded config option for watchFile (diff)
downloadshulker-a04d39a3cbf10e7335889615a36a78827857b566.tar.gz
shulker-a04d39a3cbf10e7335889615a36a78827857b566.zip
escape unicode characters
closes #76
Diffstat (limited to '')
-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)