feat: quote #42

Merged
Anri merged 16 commits from feat/citation into main 2022-07-27 13:00:24 +02:00
Showing only changes of commit fc427fd008 - Show all commits

View file

@ -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 */ /** 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 // Ignore message if
if ( if (
// Author is a bot // Author is a bot
@ -28,37 +28,49 @@ export default (message: Message) => {
} }
const messages = ( const messages = (
urls.reduce( await Promise.all(
(data: { urls.reduce(
message_id: string; (data: {
found_channel: GuildBasedChannel; message_id: string;
}[] = [], match) => { channel: TextBasedChannel;
const [, }[] = [], match) => {
guild_id, const [,
channel_id, guild_id,
message_id, channel_id,
] = new RegExp(regex).exec(match) as RegExpExecArray; 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; 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 = return quoted_message;
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;
},
[]
) )
); // Remove undefined elements
).filter(Boolean);
console.log(messages);
}; };