diff options
author | destruc7i0n <destruc7i0n@users.noreply.github.com> | 2021-12-29 16:39:46 -0500 |
---|---|---|
committer | destruc7i0n <destruc7i0n@users.noreply.github.com> | 2021-12-29 16:39:46 -0500 |
commit | a04d39a3cbf10e7335889615a36a78827857b566 (patch) | |
tree | ab132aeeb0e931e1131c1a46182a9cb5a7e882e5 | |
parent | added config option for watchFile (diff) | |
download | shulker-a04d39a3cbf10e7335889615a36a78827857b566.tar.gz shulker-a04d39a3cbf10e7335889615a36a78827857b566.zip |
escape unicode characters
closes #76
-rw-r--r-- | src/Discord.ts | 23 | ||||
-rw-r--r-- | src/MinecraftHandler.ts | 10 | ||||
-rw-r--r-- | src/Shulker.ts | 6 | ||||
-rw-r--r-- | src/lib/util.ts | 4 |
4 files changed, 23 insertions, 20 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) diff --git a/src/MinecraftHandler.ts b/src/MinecraftHandler.ts index bddc515..3353994 100644 --- a/src/MinecraftHandler.ts +++ b/src/MinecraftHandler.ts @@ -3,7 +3,9 @@ import path from 'path' import { Tail } from 'tail' import express from 'express' -import { Config } from './Config' +import type { Config } from './Config' + +import { fixMinecraftUsername } from './lib/util' export type LogLine = { username: string @@ -22,10 +24,6 @@ class MinecraftHandler { this.config = config } - private static fixMinecraftUsername (username: string) { - return username.replace(/(§[A-Z-a-z0-9])/g, '') - } - private parseLogLine (data: string): LogLine { const ignored = new RegExp(this.config.REGEX_IGNORED_CHAT) @@ -69,7 +67,7 @@ class MinecraftHandler { return null } - const username = MinecraftHandler.fixMinecraftUsername(matches[1]) + const username = fixMinecraftUsername(matches[1]) const message = matches[2] if (this.config.DEBUG) { console.log('[DEBUG] Username: ' + matches[1]) diff --git a/src/Shulker.ts b/src/Shulker.ts index df635ad..220cd03 100644 --- a/src/Shulker.ts +++ b/src/Shulker.ts @@ -1,19 +1,15 @@ -import path from 'path' import fs from 'fs' import DiscordClient from './Discord' import Handler, { LogLine } from './MinecraftHandler' -import { Config } from './Config' +import type { Config } from './Config' class Shulker { config: Config discordClient: DiscordClient handler: Handler - constructor() { - } - loadConfig () { const configFile = process.argv.length > 2 ? process.argv[2] : './config.json' if (!fs.existsSync(configFile)) { diff --git a/src/lib/util.ts b/src/lib/util.ts new file mode 100644 index 0000000..bd94795 --- /dev/null +++ b/src/lib/util.ts @@ -0,0 +1,4 @@ +export const escapeUnicode = (str: string) => + str.replace(/[^\x00-\x7F]/g, (char: string) => '\\u' + char.charCodeAt(0).toString(16).padStart(4, '0')) + +export const fixMinecraftUsername = (username: string) => username.replace(/(§[A-Z-a-z0-9])/g, '') |