From a04d39a3cbf10e7335889615a36a78827857b566 Mon Sep 17 00:00:00 2001 From: destruc7i0n Date: Wed, 29 Dec 2021 16:39:46 -0500 Subject: escape unicode characters closes #76 --- src/Discord.ts | 23 ++++++++++++++--------- src/MinecraftHandler.ts | 10 ++++------ src/Shulker.ts | 6 +----- src/lib/util.ts | 4 ++++ 4 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 src/lib/util.ts (limited to 'src') 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, '') -- cgit 1.4.1