summary refs log tree commit diff homepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Discord.ts12
-rw-r--r--tests/Discord.test.ts43
-rw-r--r--tests/DiscordWebhooks.test.ts10
-rw-r--r--tests/MinecraftHandler.test.ts6
4 files changed, 54 insertions, 17 deletions
diff --git a/src/Discord.ts b/src/Discord.ts
index 110ce64..0da51ac 100644
--- a/src/Discord.ts
+++ b/src/Discord.ts
@@ -145,11 +145,6 @@ class Discord {
       text: emojiStrip(message.cleanContent),
     }
 
-    // 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) {
       return this.config.MINECRAFT_TELLRAW_DOESNT_EXIST_SAY_TEMPLATE
         .replace(/%username%/g, variables.username)
@@ -158,7 +153,12 @@ class Discord {
         .replace(/%message%/g, variables.text)
     }
 
-    variables.text = escapeUnicode(variables.text)
+    for (const [k, v] of Object.entries(variables)) {
+      // use JSON to encode the strings for tellraw
+      const stringified = JSON.stringify(v).slice(1, -1)
+      // encode unicode characters for all fields
+      variables[k] = escapeUnicode(stringified)
+    }
 
     return this.config.MINECRAFT_TELLRAW_TEMPLATE
       .replace(/%username%/g, variables.username)
diff --git a/tests/Discord.test.ts b/tests/Discord.test.ts
index 400aa98..2c47b75 100644
--- a/tests/Discord.test.ts
+++ b/tests/Discord.test.ts
@@ -4,7 +4,7 @@ import { defaultConfig } from './constants'
 
 describe('Discord', () => {
   describe('replace mentions', () => {
-    test('does not replace mentions if config is disabled', async () => {
+    it('does not replace mentions if config is disabled', async () => {
       const discord = new Discord(defaultConfig)
 
       const replacedMessage = await discord['replaceDiscordMentions']('hey @destruc7i0n#7070')
@@ -19,7 +19,7 @@ describe('Discord', () => {
     //   expect(replacedMessage).toBe('hey <@129277271843274752>')
     // })
 
-    test('removes @everyone and @here if disabled', async () => {
+    it('removes @everyone and @here if disabled', async () => {
       const discord = new Discord({...defaultConfig, ALLOW_HERE_EVERYONE_MENTIONS: false})
 
       const everyone = await discord['replaceDiscordMentions']('hey @everyone')
@@ -29,7 +29,7 @@ describe('Discord', () => {
       expect(here).toBe('hey @ here')
     })
 
-    test('keeps @everyone and @here if enabled', async () => {
+    it('keeps @everyone and @here if enabled', async () => {
       const discord = new Discord({...defaultConfig, ALLOW_HERE_EVERYONE_MENTIONS: true})
 
       const everyone = await discord['replaceDiscordMentions']('hey @everyone')
@@ -39,4 +39,41 @@ describe('Discord', () => {
       expect(here).toBe('hey @here')
     })
   })
+
+  describe('minecraft messages', () => {
+    it('correctly generates a tellraw string in messages', async () => {
+      const discord = new Discord(defaultConfig)
+
+      const message = discord['makeMinecraftMessage']({ author: { username: 'destruc7i0n', discriminator: '7070' }, member: { nickname: 't70' }, cleanContent: 'test' } as any)
+      expect(message).toBe('[{"color": "white", "text": "<@destruc7i0n> test"}]')
+    })
+
+    it('correctly generates a tellraw string with unicode characters in message', async () => {
+      const discord = new Discord(defaultConfig)
+
+      const message = discord['makeMinecraftMessage']({ author: { username: 'destruc7i0n', discriminator: '7070' }, member: { nickname: 't70' }, cleanContent: 'æ, ø, å (Æ, Ø, Å) 건희' } as any)
+      expect(message).toBe('[{"color": "white", "text": "<@destruc7i0n> \\u00e6, \\u00f8, \\u00e5 (\\u00c6, \\u00d8, \\u00c5) \\uac74\\ud76c"}]')
+    })
+
+    it('correctly generates a tellraw string with unicode characters in username ', async () => {
+      const discord = new Discord(defaultConfig)
+
+      const message = discord['makeMinecraftMessage']({ author: { username: '건희destruc7i0n건희', discriminator: '7070' }, member: { nickname: 't70' }, cleanContent: 'huh' } as any)
+      expect(message).toBe('[{"color": "white", "text": "<@\\uac74\\ud76cdestruc7i0n\\uac74\\ud76c> huh"}]')
+    })
+
+    it('correctly replaces all parts of a message string', () => {
+      const discord = new Discord({ ...defaultConfig, MINECRAFT_TELLRAW_TEMPLATE: '[{\"color\": \"white\", \"text\": \"<@%username%#%discriminator% (%nickname%)> %message%\"}]' })
+      
+      const message = discord['makeMinecraftMessage']({ author: { username: 'destruc7i0n', discriminator: '7070' }, member: { nickname: 't70' }, cleanContent: 'test' } as any)
+      expect(message).toBe('[{\"color\": \"white\", \"text\": \"<@destruc7i0n#7070 (t70)> test\"}]')
+    })
+
+    it('correctly replaces all parts of a message string with unicode everywhere', () => {
+      const discord = new Discord({ ...defaultConfig, MINECRAFT_TELLRAW_TEMPLATE: '[{\"color\": \"white\", \"text\": \"<@%username%#%discriminator% (%nickname%)> %message%\"}]' })
+      
+      const message = discord['makeMinecraftMessage']({ author: { username: '건희destruc7i0n건희', discriminator: '7070' }, member: { nickname: '건희t70건희' }, cleanContent: 'æ, ø, å (Æ, Ø, Å) 건희' } as any)
+      expect(message).toBe('[{\"color\": \"white\", \"text\": \"<@\\uac74\\ud76cdestruc7i0n\\uac74\\ud76c#7070 (\\uac74\\ud76ct70\\uac74\\ud76c)> \\u00e6, \\u00f8, \\u00e5 (\\u00c6, \\u00d8, \\u00c5) \\uac74\\ud76c\"}]')
+    })
+  })
 })
\ No newline at end of file
diff --git a/tests/DiscordWebhooks.test.ts b/tests/DiscordWebhooks.test.ts
index 0965c3c..985b118 100644
--- a/tests/DiscordWebhooks.test.ts
+++ b/tests/DiscordWebhooks.test.ts
@@ -3,7 +3,7 @@ import DiscordWebhooks from '../src/DiscordWebhooks'
 import { defaultConfig } from './constants'
 
 describe('DiscordWebhooks', () => {
-  test('parses discord webhooks', () => {
+  it('parses discord webhooks', () => {
     const dw = new DiscordWebhooks(defaultConfig)
 
     expect(dw['parseDiscordWebhook']('https://discordapp.com/api/webhooks/id/token')).toStrictEqual({ id: 'id', token: 'token' })
@@ -14,7 +14,7 @@ describe('DiscordWebhooks', () => {
     expect(dw['parseDiscordWebhook']('https://diskrod.com/api/webhooks/id/token')).toStrictEqual({ id: null, token: null })
   })
   
-  test('gets uuid from username and caches', async () => {
+  it('gets uuid from username and caches', async () => {
     const dw = new DiscordWebhooks(defaultConfig)
 
     expect(await dw['getUUIDFromUsername']('destruc7i0n')).toBe('2d8cf844fa3441c38d4e597b32697909')
@@ -26,7 +26,7 @@ describe('DiscordWebhooks', () => {
   })
 
   describe('webhook generation', () => {
-    test('creates for valid players', async () => {
+    it('creates for valid players', async () => {
       const dw = new DiscordWebhooks(defaultConfig)
 
       expect(await dw['makeDiscordWebhook']('destruc7i0n', 'hey')).toStrictEqual(
@@ -46,7 +46,7 @@ describe('DiscordWebhooks', () => {
       )
     })
 
-    test('creates with default player head for invalid players', async () => {
+    it('creates with default player head for invalid players', async () => {
       const dw = new DiscordWebhooks(defaultConfig)
 
       // inb4 someone makes `fakedestruc7i0n`
@@ -71,7 +71,7 @@ describe('DiscordWebhooks', () => {
       )
     })
 
-    test('creates for server message', async () => {
+    it('creates for server message', async () => {
       const dw = new DiscordWebhooks(defaultConfig)
 
       expect(await dw['makeDiscordWebhook']('Shulker - Server', 'hey')).toStrictEqual(
diff --git a/tests/MinecraftHandler.test.ts b/tests/MinecraftHandler.test.ts
index 4bb10f4..f73d983 100644
--- a/tests/MinecraftHandler.test.ts
+++ b/tests/MinecraftHandler.test.ts
@@ -3,7 +3,7 @@ import MinecraftHandler from '../src/MinecraftHandler'
 import { defaultConfig } from './constants'
 
 describe('MinecraftHandler', () => {
-  test('parses player join connection stat', () => {
+  it('parses player join connection stat', () => {
     const handler = new MinecraftHandler(defaultConfig)
 
     const { message } = handler['parseLogLine']('[Server thread/INFO]: destruc7i0n joined the game')!
@@ -11,7 +11,7 @@ describe('MinecraftHandler', () => {
     expect(message).toBe('destruc7i0n joined the game')
   })
 
-  test('parses player leave connection stat', () => {
+  it('parses player leave connection stat', () => {
     const handler = new MinecraftHandler(defaultConfig)
 
     const { message } = handler['parseLogLine']('[Server thread/INFO]: destruc7i0n left the game')!
@@ -19,7 +19,7 @@ describe('MinecraftHandler', () => {
     expect(message).toBe('destruc7i0n left the game')
   })
 
-  test('parses death messages', () => {
+  it('parses death messages', () => {
     const handler = new MinecraftHandler(defaultConfig)
 
     const tests = [