* support an event triggered when a message appear
* WIP Quotes
This commit is contained in:
parent
bba614198a
commit
403cefc737
1 changed files with 64 additions and 0 deletions
64
src/events/message/messageCreate.ts
Normal file
64
src/events/message/messageCreate.ts
Normal 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);
|
||||||
|
};
|
Loading…
Reference in a new issue