Restrict a VCs where user requires to turn their camera ON otherwise kick them!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const { VC_CHANNEL_IDS, DURATION_BEFORE_KICK, KICKMESSAGE_CHANNEL } = process.env;
const { member, guild_id, channel_id, self_video, } = context.params.event;
try {
if (DURATION_BEFORE_KICK.match(/\D/gi)) throw new Error(`The Duration should be a number, please check your environmental variables!`);
else if (VC_CHANNEL_IDS.replace(/, /, '').match(/\D/gi)) throw new Error(`Invalid channel ID's, please check your environmental variables`);
let VCs = new Set(VC_CHANNEL_IDS.split(', '));
if (!VCs.has(channel_id)) return; // If the VC you joined doesn't match to the VCs that requires camera
const VCState = await GetKey(`VC:${channel_id}`, {}) // Retrieving the VC Data
if (!channel_id && !Object.keys(VCState).length || !channel_id && Object.keys(VCState).includes(member.user.id)) { // If user left the VC
delete VCState[member.user.id]; // Clearing the user's record
return SetKey(`VC:${channel_id}`, VCState); // Set a new value.
// This method prevents the value to exceed its maximum storage.
}
if (!channel_id) return; // If the user left on VC
if (!Object.keys(VCState).length) { // If no kv setted up
await SetKey(`VC:${channel_id}`, { [member.user.id]: self_video });
}
let duration = !+DURATION_BEFORE_KICK ? 10000 : +`${DURATION_BEFORE_KICK.slice(0,2)}000`; // The duration before kicking you out inside the VC.
VCState[member.user.id] = self_video;
await SetKey(`VC:${channel_id}`, VCState);
let reportMsg;
if (!self_video) { // if the user didn't turn the camera On, warn the user.
reportMsg = await lib.discord.channels['@release'].messages.create({
channel_id, content: `Hey <@${member.user.id}>, you are given ${String(duration).slice(0,2)} seconds to turn your camera on.`
});
}
await Delay(duration); // stops the process for (x) seconds
await lib.discord.channels['@release'].messages.destroy({ // destroying the warning message
message_id: reportMsg.id, channel_id: reportMsg.channel_id
});
const newVCState = await GetKey(`VC:${channel_id}`, false); // retrieving the new VC Data to check if the user has their camera on or off
if (newVCState[member?.user?.id]) return console.log('Safe') // if user turned their camera ON, return the process...
await lib.discord.guilds['@release'].members.voice.disconnect({ // kick the user inside the VC
user_id: member.user.id, guild_id,
});
return lib.discord.channels['@release'].messages.create({ // and send a followup message on why they got kicked.
channel_id: KICKMESSAGE_CHANNEL,
content: `<@${member.user.id}>, You were kicked on the VC.`
});
} catch (err) {
if (err.message.startsWith(`Cannot read property 'id' of undefined`)) return console.log(`Process Complete.`);
else console.error(err);
}
// Functions:
async function Delay(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => {resolve()}, ms || 0)});
}
async function SetKey(key, value) {
return lib.keyvalue.store['@release'].set({ key, value });
}
async function GetKey(key, defaultValue) {
return lib.keyvalue.store['@release'].get({ key, defaultValue });
}