From 2a20d9fdaa34a3c3c2ac9b70bc068d511458470f Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 28 Mar 2019 05:03:47 +0200 Subject: [PATCH] Escape markdown in user nickname and some other places --- src/data/threads.js | 6 +++--- src/utils.js | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/data/threads.js b/src/data/threads.js index 66cce60..2716c84 100644 --- a/src/data/threads.js +++ b/src/data/threads.js @@ -177,14 +177,14 @@ async function createNewThreadForUser(user, quiet = false) { for (const [guildId, guildData] of userGuildData.entries()) { const {nickname, joinDate} = getHeaderGuildInfo(guildData.member); const headerItems = [ - `NICKNAME **${nickname}**`, + `NICKNAME **${utils.escapeMarkdown(nickname)}**`, `JOINED **${joinDate}** ago` ]; if (guildData.member.voiceState.channelID) { const voiceChannel = guildData.guild.channels.get(guildData.member.voiceState.channelID); if (voiceChannel) { - headerItems.push(`VOICE CHANNEL **${voiceChannel.name}**`); + headerItems.push(`VOICE CHANNEL **${utils.escapeMarkdown(voiceChannel.name)}**`); } } @@ -193,7 +193,7 @@ async function createNewThreadForUser(user, quiet = false) { if (mainGuilds.length === 1) { infoHeader += `\n${headerStr}`; } else { - infoHeader += `\n**[${guildData.guild.name}]** ${headerStr}`; + infoHeader += `\n**[${utils.escapeMarkdown(guildData.guild.name)}]** ${headerStr}`; } } diff --git a/src/utils.js b/src/utils.js index 69f3058..fddb719 100644 --- a/src/utils.js +++ b/src/utils.js @@ -294,6 +294,11 @@ function isSnowflake(str) { const humanizeDelay = (delay, opts = {}) => humanizeDuration(delay, Object.assign({conjunction: ' and '}, opts)); +const markdownCharsRegex = /([\\_*|`~])/g; +function escapeMarkdown(str) { + return str.replace(markdownCharsRegex, '\\$1'); +} + module.exports = { BotError, @@ -327,4 +332,6 @@ module.exports = { isSnowflake, humanizeDelay, + + escapeMarkdown, };