From fc427fd008df3ed4c85dca2aa0fbd3c4efc06f19 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 26 Jul 2022 21:45:21 +0200 Subject: [PATCH] check if message exists --- src/events/message/messageCreate.ts | 70 +++++++++++++++++------------ 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/src/events/message/messageCreate.ts b/src/events/message/messageCreate.ts index b84ee0a..46f73c8 100644 --- a/src/events/message/messageCreate.ts +++ b/src/events/message/messageCreate.ts @@ -1,7 +1,7 @@ -import { GuildBasedChannel, Message } from 'discord.js'; +import { Message, TextBasedChannel } from 'discord.js'; /** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-messageCreate */ -export default (message: Message) => { +export default async (message: Message) => { // Ignore message if if ( // Author is a bot @@ -28,37 +28,49 @@ export default (message: Message) => { } const messages = ( - urls.reduce( - (data: { - message_id: string; - found_channel: GuildBasedChannel; - }[] = [], match) => { - const [, - guild_id, - channel_id, - message_id, - ] = new RegExp(regex).exec(match) as RegExpExecArray; + await Promise.all( + urls.reduce( + (data: { + message_id: string; + channel: TextBasedChannel; + }[] = [], match) => { + const [, + guild_id, + channel_id, + message_id, + ] = new RegExp(regex).exec(match) as RegExpExecArray; + + // If message posted in another guild + if (guild_id !== message.guild?.id) { + return data; + } + + const channel = + message.guild.channels.cache.get(channel_id) as TextBasedChannel; + + // If channel doesn't exist in the guild and isn't text + if (!channel) { + return data; + } + + data.push({ message_id, channel }); - // If message posted in another guild - if (guild_id !== message.guild?.id) { return data; + }, [] + ).map(async ({ message_id, channel }) => { + const quoted_message = await channel.messages + .fetch(message_id) + .catch(() => undefined); + + // If message doesn't exist or empty + if (!quoted_message || (!quoted_message.content && quoted_message.attachments.size == 0)) { + return; } - const found_channel = - message.guild.channels.cache.get(channel_id); - - // If channel doesn't exist in the guild - if (!found_channel) { - return data; - } - - data.push({ message_id, found_channel }); - - return data; - }, - [] + return quoted_message; + }) ) - ); + // Remove undefined elements + ).filter(Boolean); - console.log(messages); };