markdown fixes
Some checks are pending
njsscan sarif / njsscan code scanning (push) Waiting to run
Update AUTHORS File / update-authors (push) Waiting to run

This commit is contained in:
Aidan 2025-06-30 02:29:58 -04:00
parent 04271f87b1
commit 962df1fd56
3 changed files with 19 additions and 11 deletions

View file

@ -145,6 +145,12 @@ export function sanitizeForJson(text: string): string {
.replace(/\t/g, '\\t') .replace(/\t/g, '\\t')
} }
export function escapeMarkdown(text: string): string {
if (!text) return '';
return text
.replace(/([_\*\[\]()~`>#+\-=|{}.!])/g, '\\$1');
}
export async function preChecks() { export async function preChecks() {
const envs = [ const envs = [
"ollamaApi", "ollamaApi",
@ -354,16 +360,17 @@ async function handleAiReply(ctx: TextContext, db: NodePgDatabase<typeof schema>
if (!ctx.chat) return; if (!ctx.chat) return;
if (aiResponse.success && aiResponse.response) { if (aiResponse.success && aiResponse.response) {
const modelHeader = `🤖 *${model}* | 🌡️ *${aiTemperature}*\n\n`; const modelHeader = `🤖 *${model}* | 🌡️ *${aiTemperature}*\n\n`;
const safeResponse = escapeMarkdown(modelHeader + aiResponse.response);
await rateLimiter.editMessageWithRetry( await rateLimiter.editMessageWithRetry(
ctx, ctx,
ctx.chat.id, ctx.chat.id,
replyGenerating.message_id, replyGenerating.message_id,
modelHeader + aiResponse.response, safeResponse,
{ parse_mode: 'Markdown' } { parse_mode: 'Markdown' }
); );
return; return;
} }
const error = Strings.unexpectedErr.replace("{error}", aiResponse.error); const error = escapeMarkdown(Strings.unexpectedErr.replace("{error}", aiResponse.error));
await rateLimiter.editMessageWithRetry( await rateLimiter.editMessageWithRetry(
ctx, ctx,
ctx.chat.id, ctx.chat.id,

View file

@ -4,6 +4,7 @@ import axios from 'axios';
import { getStrings } from '../plugins/checklang'; import { getStrings } from '../plugins/checklang';
import { isOnSpamWatch } from '../spamwatch/spamwatch'; import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import { escapeMarkdown } from './ai';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
@ -69,7 +70,7 @@ export default (bot) => {
const lastUser = ctx.message.text.split(' ')[1]; const lastUser = ctx.message.text.split(' ')[1];
if (!lastUser) { if (!lastUser) {
return ctx.reply(Strings.lastFm.noUser, { return ctx.reply(escapeMarkdown(Strings.lastFm.noUser), {
parse_mode: "Markdown", parse_mode: "Markdown",
disable_web_page_preview: true, disable_web_page_preview: true,
...(ctx.message?.message_id ? { reply_parameters: { message_id: ctx.message.message_id } } : {}) ...(ctx.message?.message_id ? { reply_parameters: { message_id: ctx.message.message_id } } : {})
@ -79,7 +80,7 @@ export default (bot) => {
users[userId] = lastUser; users[userId] = lastUser;
saveUsers(); saveUsers();
const message = Strings.lastFm.userHasBeenSet.replace('{lastUser}', lastUser); const message = escapeMarkdown(Strings.lastFm.userHasBeenSet.replace('{lastUser}', lastUser));
ctx.reply(message, { ctx.reply(message, {
parse_mode: "Markdown", parse_mode: "Markdown",
@ -170,9 +171,9 @@ export default (bot) => {
num_plays = response_plays.data.track.userplaycount; num_plays = response_plays.data.track.userplaycount;
} catch (err) { } catch (err) {
console.log(err) console.log(err)
const message = Strings.lastFm.apiErr const message = escapeMarkdown(Strings.lastFm.apiErr
.replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`) .replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`)
.replace("{err}", err); .replace("{err}", err));
ctx.reply(message, { ctx.reply(message, {
parse_mode: "Markdown", parse_mode: "Markdown",
disable_web_page_preview: true, disable_web_page_preview: true,
@ -180,11 +181,11 @@ export default (bot) => {
}); });
}; };
let message = Strings.lastFm.listeningTo let message = escapeMarkdown(Strings.lastFm.listeningTo
.replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`) .replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`)
.replace("{nowPlaying}", nowPlaying) .replace("{nowPlaying}", nowPlaying)
.replace("{trackName}", `[${trackName}](${trackUrl})`) .replace("{trackName}", `[${trackName}](${trackUrl})`)
.replace("{artistName}", `[${artistName}](${artistUrl})`) .replace("{artistName}", `[${artistName}](${artistUrl})`));
if (`${num_plays}` !== "0" && `${num_plays}` !== "1" && `${num_plays}` !== "2" && `${num_plays}` !== "3") { if (`${num_plays}` !== "0" && `${num_plays}` !== "1" && `${num_plays}` !== "2" && `${num_plays}` !== "3") {
message = message message = message
@ -211,9 +212,9 @@ export default (bot) => {
}; };
} catch (err) { } catch (err) {
const userUrl = `https://www.last.fm/user/${encodeURIComponent(lastfmUser)}`; const userUrl = `https://www.last.fm/user/${encodeURIComponent(lastfmUser)}`;
const message = Strings.lastFm.apiErr const message = escapeMarkdown(Strings.lastFm.apiErr
.replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`) .replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`)
.replace("{err}", err); .replace("{err}", err));
ctx.reply(message, { ctx.reply(message, {
parse_mode: "Markdown", parse_mode: "Markdown",
disable_web_page_preview: true, disable_web_page_preview: true,

@ -1 +1 @@
Subproject commit cee30dc64217e7ec235635f5bf5066eac56eec87 Subproject commit 5e9de56c7e117811f8dba16fb671b65469688144