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 403cefc737 - Show all commits

View file

@ -0,0 +1,64 @@
import { GuildBasedChannel, Message } from 'discord.js';
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-messageCreate */
export default (message: Message) => {
// Ignore message if
if (
// Author is a bot
message.author.bot ||
// Author is Discord
message.author.system ||
// Message isn't a message
message.system ||
// Message is in PM (future-proof if we add Intents.FLAGS.DIRECT_MESSAGES)
!message.guild ||
// Guild is offline
!message.guild.available
) {
return;
}
/* Citation */
const regex = 'https://(?:canary\\.|ptb\\.)?discord(?:app)?\\.com/channels/(\\d{17,19})/(\\d{17,19})/(\\d{17,19})';
const urls = message.content.match(new RegExp(regex, 'g'));
// Ignore message if there is no URLs
if (!urls) {
return;
}
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;
// If message posted in another guild
if (guild_id !== message.guild?.id) {
return data;
}
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;
},
[]
)
);
console.log(messages);
};