Skip to content

Commit ae25ca1

Browse files
committed
fix: discord better escape.
1 parent 7f86614 commit ae25ca1

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

monitor/monitor.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ async function sendToDiscord(message: string): Promise<void> {
6060
// Add message to queue
6161
messageQueue.push(message)
6262
}
63-
63+
function sanitizeDiscordInput(name: string): string {
64+
return name
65+
.replace(/@everyone/g, '@\u200Beveryone')
66+
.replace(/@here/g, '@\u200Bhere')
67+
.replace(/@/g, '@\u200B') // escape other @
68+
.replace(/([_*~`>|])/g, '\\$1'); // escape markdown
69+
}
6470
// Function to process the message queue and send batched messages
6571
async function processMessageQueue(): Promise<void> {
6672
// If queue is empty or already processing, do nothing
@@ -79,21 +85,26 @@ async function processMessageQueue(): Promise<void> {
7985
const content = messagesToSend.join('\n')
8086
console.log(`Sending batch of ${messagesToSend.length} messages to Discord`)
8187

82-
const payload = {
83-
content,
84-
}
8588

8689
console.log('SENDING', content)
8790
if (!webhookUrl) {
8891
console.error('Error: DISCORD_WEBHOOK_URL environment variable is not set')
8992
process.exit(1)
9093
}
91-
await fetch(webhookUrl, {
94+
const embed = {
95+
title: 'NotBlox',
96+
description: sanitizeDiscordInput(content),
97+
color: 0x00ff00,
98+
}
99+
100+
const response = await fetch(webhookUrl, {
92101
method: 'POST',
93102
headers: {
94103
'Content-Type': 'application/json',
95104
},
96-
body: JSON.stringify(payload),
105+
body: JSON.stringify({
106+
embeds: [embed],
107+
}),
97108
})
98109
} catch (error) {
99110
console.error('Error sending batched messages to Discord:', error)

0 commit comments

Comments
 (0)