refactor: use matchAll instead of while loop
Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
parent
f099cc9900
commit
2b99eb0579
1 changed files with 9 additions and 24 deletions
|
@ -22,41 +22,30 @@ function findFirstImage(message: Message): string | undefined {
|
||||||
export async function expandDiscordLink(message: Message): Promise<void> {
|
export async function expandDiscordLink(message: Message): Promise<void> {
|
||||||
const re =
|
const re =
|
||||||
/(https?:\/\/)?(?:canary\.|ptb\.)?discord(?:app)?\.com\/channels\/(?<server_id>\d+)\/(?<channel_id>\d+)\/(?<message_id>\d+)/g;
|
/(https?:\/\/)?(?:canary\.|ptb\.)?discord(?:app)?\.com\/channels\/(?<server_id>\d+)\/(?<channel_id>\d+)\/(?<message_id>\d+)/g;
|
||||||
let execResult = re.exec(message.content);
|
|
||||||
|
|
||||||
while (!(execResult == null || execResult.groups == undefined)) {
|
const results = message.content.matchAll(re);
|
||||||
if (execResult.groups.server_id != message.guildId) {
|
|
||||||
execResult = re.exec(message.content);
|
for (const r of results) {
|
||||||
|
if (r.groups == undefined && r.groups.server_id != message.guildId)
|
||||||
continue; // do not let the bot leak messages from one server to another
|
continue; // do not let the bot leak messages from one server to another
|
||||||
}
|
|
||||||
|
|
||||||
const channel = await message.guild?.channels.fetch(
|
const channel = await message.guild?.channels.fetch(
|
||||||
execResult.groups.channel_id
|
r.groups.channel_id
|
||||||
);
|
);
|
||||||
|
|
||||||
if (channel == undefined || channel == null || !channel.isTextBased()) {
|
if (!channel || !channel.isTextBased())
|
||||||
execResult = re.exec(message.content);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
if (channel instanceof ThreadChannel) {
|
if (channel instanceof ThreadChannel) {
|
||||||
if (
|
if (!channel.parent?.members?.some((user) => user.id == message.author.id))
|
||||||
!channel.parent?.members?.some((user) => user.id == message.author.id)
|
|
||||||
) {
|
|
||||||
execResult = re.exec(message.content);
|
|
||||||
continue; // do not reveal a message to a user who can't see it
|
continue; // do not reveal a message to a user who can't see it
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (!channel.members?.some((user) => user.id == message.author.id)) {
|
if (!channel.members?.some((user) => user.id == message.author.id))
|
||||||
execResult = re.exec(message.content);
|
|
||||||
continue; // do not reveal a message to a user who can't see it
|
continue; // do not reveal a message to a user who can't see it
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const messageToShow = await channel.messages.fetch(
|
const messageToShow = await channel.messages.fetch(r.groups.message_id);
|
||||||
execResult.groups.message_id
|
|
||||||
);
|
|
||||||
|
|
||||||
const builder = new EmbedBuilder()
|
const builder = new EmbedBuilder()
|
||||||
.setAuthor({
|
.setAuthor({
|
||||||
|
@ -93,10 +82,6 @@ export async function expandDiscordLink(message: Message): Promise<void> {
|
||||||
await message.channel.send({ embeds: [builder], components: [row] });
|
await message.channel.send({ embeds: [builder], components: [row] });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
execResult = re.exec(message.content);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
execResult = re.exec(message.content);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue