Compare commits

..

2 commits

Author SHA1 Message Date
403cefc737
* support an event triggered when a message appear
* WIP Quotes
2022-07-26 14:57:21 +02:00
bba614198a
add message support 2022-07-26 14:56:41 +02:00
2 changed files with 65 additions and 0 deletions

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);
};

View file

@ -45,6 +45,7 @@ export default async () => {
const client: Client = new Client({ const client: Client = new Client({
intents: [ intents: [
Intents.FLAGS.GUILDS, Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
], ],
}); });