This code checks if the sent message contains any discord invite link and if it do then it checks if the link is valid or not and if its valid link then it deletes the link and remind the member not to send these links again. This code ignores the links of the server where message is sent.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const axios = require('axios');
const {content, id, channel_id, guild_id, author} = context.params.event;
const invites = content.match(/(?<=discord.gg\/)(\w+)/g);
if (invites?.length) {
for (let code of invites) {
const data = await axios(`https://discordapp.com/api/invite/${code}`)
.then((res) => {
return res.data;
})
.catch((err) => {});
if (!data || data.message || data.guild.id === guild_id) continue;
await lib.discord.channels['@0.2.2'].messages.destroy({
message_id: id,
channel_id,
});
await lib.discord.channels['@0.2.2'].messages.create({
content: `<@${author.id}>, Please do not send discord invite links of another server in this server`,
channel_id,
});
break;
}
}