This snippet will prevent any bot raid on your server. Just set the verify role and you are done! Run the command !verify and follow the steps!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let event = context.params.event;
//If you copied the code change the value to the role id
let VERIFY_ROLE = process.env.VERIFY_ROLE;
//user id who triggerd the command
let user = context.params.event.author.id;
//getting the key value of the user
let captcha = await lib.utils.kv['@0.1.16'].get({
key: `captcha_${user}`,
});
if (!event.member.roles.includes(VERIFY_ROLE)) {
//checking if does not have verify role
if (!captcha) {
//checking if the key isn't set already
//all the random numbers
var a = Math.ceil(Math.random() * 9) + '';
var b = Math.ceil(Math.random() * 9) + '';
var c = Math.ceil(Math.random() * 9) + '';
var d = Math.ceil(Math.random() * 9) + '';
var e = Math.ceil(Math.random() * 9) + '';
//addinng them all into one var
var all = a + b + c + d + e;
//setting the key of the user to the captcha code
await lib.utils.kv['@0.1.16'].set({
key: `captcha_${user}`,
value: `${all}`,
ttl: 30, //this will make the key expire after 30 seconds so it's more secure. Change it if you want
});
//sending code to the user
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: ` Please enter this code -> \`${all}\` below \n\n example: \`!verify 12345\` you have 30 seconds`,
});
} else {
//this code bellow gets triggered if the captcha key of the user is already set
//getting the code after the !verify and making it a string
let content = event.content.split(' ').slice(1).toString();
if (content === captcha) {
//if the code sent is equal to the captcha key of the user
//giving verify role to user
await lib.discord.guilds['@0.2.3'].members.roles.update({
role_id: `${VERIFY_ROLE}`,
user_id: `${user}`,
guild_id: `${event.guild_id}`,
});
//sending confirmation msg
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: `succesfully verified <@${user}> ✅`,
});
//clearing the key of the user (so you save kv storage)
await lib.utils.kv['@0.1.16'].clear({
key: `captcha_${user}`,
});
} else {
//if the code sent is wrong
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: `incorrect code! Please enter \`!verify ${captcha}\` \n\n you have less than 30 seconds!`,
});
}
}
} else {
//if user already has role
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: `You can't verify again :wink:`,
});
}