pain
This commit is contained in:
parent
f34d01745d
commit
eafb6e2373
4 changed files with 34 additions and 48 deletions
|
@ -21,6 +21,7 @@
|
||||||
"log75": "^2.2.0",
|
"log75": "^2.2.0",
|
||||||
"monk": "^7.3.4",
|
"monk": "^7.3.4",
|
||||||
"revolt-api": "^0.5.3-rc.8",
|
"revolt-api": "^0.5.3-rc.8",
|
||||||
|
"smart-replace": "^1.0.1",
|
||||||
"ulid": "^2.3.0"
|
"ulid": "^2.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,12 @@ import GenericEmbed from "../types/GenericEmbed";
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
import { discordFetchUser, revoltFetchMessage } from "../util";
|
import { discordFetchUser, revoltFetchMessage } from "../util";
|
||||||
import { TextChannel } from "discord.js";
|
import { TextChannel } from "discord.js";
|
||||||
|
import { smartReplace } from "smart-replace";
|
||||||
|
|
||||||
const MAX_BRIDGED_FILE_SIZE = 8_000_000; // 8 MB
|
const MAX_BRIDGED_FILE_SIZE = 8_000_000; // 8 MB
|
||||||
const RE_MENTION_USER = /<@!*[0-9]+>/g;
|
const RE_MENTION_USER = /<@!*[0-9]+>/g;
|
||||||
const RE_MENTION_CHANNEL = /<#[0-9]+>/g;
|
const RE_MENTION_CHANNEL = /<#[0-9]+>/g;
|
||||||
|
const RE_EMOJI = /<(a?)?:\w+:\d{18}?>/g;
|
||||||
|
|
||||||
client.on('messageDelete', async message => {
|
client.on('messageDelete', async message => {
|
||||||
try {
|
try {
|
||||||
|
@ -198,41 +200,31 @@ client.on('messageCreate', async message => {
|
||||||
|
|
||||||
// Replaces @mentions and #channel mentions
|
// Replaces @mentions and #channel mentions
|
||||||
async function renderMessageBody(message: string): Promise<string> {
|
async function renderMessageBody(message: string): Promise<string> {
|
||||||
// We don't want users to generate large amounts of database and API queries
|
|
||||||
let failsafe = 0;
|
|
||||||
|
|
||||||
// @mentions
|
// @mentions
|
||||||
while (failsafe < 10) {
|
message = await smartReplace(message, RE_MENTION_USER, async (match: string) => {
|
||||||
failsafe++;
|
const id = match.replace('<@!', '').replace('<@', '').replace('>', '');
|
||||||
|
|
||||||
const text = message.match(RE_MENTION_USER)?.[0];
|
|
||||||
if (!text) break;
|
|
||||||
|
|
||||||
const id = text.replace('<@!', '').replace('<@', '').replace('>', '');
|
|
||||||
const user = await discordFetchUser(id);
|
const user = await discordFetchUser(id);
|
||||||
|
return `@${user?.username || id}`;
|
||||||
// replaceAll() when
|
}, { cacheMatchResults: true, maxMatches: 10 });
|
||||||
while (message.includes(text)) message = message.replace(text, `@${user?.username || id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// #channels
|
// #channels
|
||||||
while (failsafe < 10) {
|
message = await smartReplace(message, RE_MENTION_CHANNEL, async (match: string) => {
|
||||||
failsafe++;
|
const id = match.replace('<#', '').replace('>', '');
|
||||||
|
|
||||||
const text = message.match(RE_MENTION_CHANNEL)?.[0];
|
|
||||||
if (!text) break;
|
|
||||||
|
|
||||||
const id = text.replace('<#', '').replace('>', '');
|
|
||||||
const channel = client.channels.cache.get(id);
|
const channel = client.channels.cache.get(id);
|
||||||
const bridgeCfg = channel ? await BRIDGE_CONFIG.findOne({ discord: channel.id }) : undefined;
|
const bridgeCfg = channel ? await BRIDGE_CONFIG.findOne({ discord: channel.id }) : undefined;
|
||||||
const revoltChannel = bridgeCfg?.revolt
|
const revoltChannel = bridgeCfg?.revolt
|
||||||
? revoltClient.channels.get(bridgeCfg.revolt)
|
? revoltClient.channels.get(bridgeCfg.revolt)
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
while (message.includes(text)) {
|
return revoltChannel ? `<#${revoltChannel._id}>` : `#${(channel as TextChannel)?.name || id}`;
|
||||||
message = message.replace(text, revoltChannel ? `<#${revoltChannel._id}>` : `#${(channel as TextChannel)?.name || id}`);
|
}, { cacheMatchResults: true, maxMatches: 10 });
|
||||||
}
|
|
||||||
}
|
// :emojis:
|
||||||
|
message = await smartReplace(message, RE_EMOJI, async (match: string) => {
|
||||||
|
return match
|
||||||
|
.replace(/<(a?)?:/, ':\u200b') // We don't want to accidentally send an unrelated emoji, so we add a zero width space here
|
||||||
|
.replace(/(:\d{18}?>)/, ':');
|
||||||
|
}, { cacheMatchResults: true });
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { MessageEmbed, MessagePayload, TextChannel, WebhookClient, WebhookMessag
|
||||||
import GenericEmbed from "../types/GenericEmbed";
|
import GenericEmbed from "../types/GenericEmbed";
|
||||||
import { SendableEmbed } from "revolt-api";
|
import { SendableEmbed } from "revolt-api";
|
||||||
import { clipText, discordFetchMessage, revoltFetchUser } from "../util";
|
import { clipText, discordFetchMessage, revoltFetchUser } from "../util";
|
||||||
|
import { smartReplace } from "smart-replace";
|
||||||
|
|
||||||
const RE_MENTION_USER = /<@[0-9A-HJ-KM-NP-TV-Z]{26}>/g;
|
const RE_MENTION_USER = /<@[0-9A-HJ-KM-NP-TV-Z]{26}>/g;
|
||||||
const RE_MENTION_CHANNEL = /<#[0-9A-HJ-KM-NP-TV-Z]{26}>/g;
|
const RE_MENTION_CHANNEL = /<#[0-9A-HJ-KM-NP-TV-Z]{26}>/g;
|
||||||
|
@ -189,39 +190,26 @@ async function renderMessageBody(message: string): Promise<string> {
|
||||||
let failsafe = 0;
|
let failsafe = 0;
|
||||||
|
|
||||||
// @mentions
|
// @mentions
|
||||||
while (failsafe < 10) {
|
message = await smartReplace(message, RE_MENTION_USER, async (match) => {
|
||||||
failsafe++;
|
const id = match.replace('<@', '').replace('>', '');
|
||||||
|
|
||||||
const text = message.match(RE_MENTION_USER)?.[0];
|
|
||||||
if (!text) break;
|
|
||||||
|
|
||||||
const id = text.replace('<@', '').replace('>', '');
|
|
||||||
const user = await revoltFetchUser(id);
|
const user = await revoltFetchUser(id);
|
||||||
|
return `@${user?.username || id}`;
|
||||||
// replaceAll() when
|
}, { cacheMatchResults: true, maxMatches: 10 });
|
||||||
while (message.includes(text)) message = message.replace(text, `@${user?.username || id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// #channels
|
// #channels
|
||||||
while (failsafe < 10) {
|
message = await smartReplace(message, RE_MENTION_CHANNEL, async (match) => {
|
||||||
failsafe++;
|
const id = match.replace('<#', '').replace('>', '');
|
||||||
|
|
||||||
const text = message.match(RE_MENTION_CHANNEL)?.[0];
|
|
||||||
if (!text) break;
|
|
||||||
|
|
||||||
const id = text.replace('<#', '').replace('>', '');
|
|
||||||
const channel = client.channels.get(id);
|
const channel = client.channels.get(id);
|
||||||
|
|
||||||
const bridgeCfg = channel ? await BRIDGE_CONFIG.findOne({ revolt: channel._id }) : undefined;
|
const bridgeCfg = channel ? await BRIDGE_CONFIG.findOne({ revolt: channel._id }) : undefined;
|
||||||
const discordChannel = bridgeCfg?.discord
|
const discordChannel = bridgeCfg?.discord
|
||||||
? discordClient.channels.cache.get(bridgeCfg.discord)
|
? discordClient.channels.cache.get(bridgeCfg.discord)
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
while (message.includes(text)) {
|
return discordChannel ? `<#${discordChannel.id}>` : `#${channel?.name || id}`;
|
||||||
message = message.replace(text, discordChannel ? `<#${discordChannel.id}>` : `#${channel?.name || id}`);
|
}, { cacheMatchResults: true, maxMatches: 10 });
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// :emojis: (todo lol)
|
// TODO: fetch emojis and upload them to Discord or smth
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
|
@ -479,6 +479,11 @@ saslprep@^1.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
sparse-bitfield "^3.0.3"
|
sparse-bitfield "^3.0.3"
|
||||||
|
|
||||||
|
smart-replace@^1.0.1:
|
||||||
|
version "1.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/smart-replace/-/smart-replace-1.0.1.tgz#4a99193ffc60bd3004370d79e1fd5103fb546ee5"
|
||||||
|
integrity sha512-2XhBOaa8I+OhjeXk4heFJ6VnFn6qfmT1JA5yFolH1CfGmg5fgV6/7izu8fL5+AFM3bIxFbnPiliFY8lDQ+33WQ==
|
||||||
|
|
||||||
sparse-bitfield@^3.0.3:
|
sparse-bitfield@^3.0.3:
|
||||||
version "3.0.3"
|
version "3.0.3"
|
||||||
resolved "https://registry.yarnpkg.com/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz#ff4ae6e68656056ba4b3e792ab3334d38273ca11"
|
resolved "https://registry.yarnpkg.com/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz#ff4ae6e68656056ba4b3e792ab3334d38273ca11"
|
||||||
|
|
Loading…
Reference in a new issue