Adding in a more complex captcha with noise and random generated lines

This commit is contained in:
ExtrasContainer
2024-02-04 19:36:11 +00:00
parent 477fda20a0
commit ed2faadec1
+36 -8
View File
@@ -13,7 +13,7 @@ module.exports = {
var upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowchars = "abcdefghijklmnopqrstuvwxyz";
var nums = "0123456789";
//get a random letter/number from upchars, lowchars, and nums
var chars = upchars + lowchars + nums;
var captcha = "";
@@ -35,16 +35,44 @@ module.exports = {
async function createCaptcha(captcha) {
const canvas = createCanvas(200, 75);
const ctx = canvas.getContext('2d');
// Background color
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Text
ctx.font = '30px Arial';
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillStyle = 'white';
ctx.fillStyle = '#ffffff';
ctx.fillText(captcha, 100, 37.5);
// Adding noise
for (let i = 0; i < 50; i++) {
ctx.fillStyle = `rgba(255,255,255,${Math.random() * 0.9})`;
ctx.fillRect(Math.random() * canvas.width, Math.random() * canvas.height, 1, 1);
}
// Adding lines
for (let i = 0; i < 30; i++) {
ctx.strokeStyle = `rgba(255,255,255,${Math.random() * 1.8})`;
ctx.beginPath();
ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height);
ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height);
ctx.stroke();
}
// Rotation
const rotation = Math.random() * 0.2 - 0.1; // Random rotation between -0.1 and 0.1 radians
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(rotation);
ctx.translate(-canvas.width / 2, -canvas.height / 2);
const buffer = canvas.toBuffer('image/png');
return buffer;
}
if (interaction.channel.id !== cfg.guilds[guildId].channelId) {
return interaction.reply({ content: cfg.messages.wrongChannel, ephemeral: true })
}
@@ -105,8 +133,8 @@ module.exports = {
}
}
});
collector.on('end', async (collected, reason) => {
collector.on('end', async (collected, reason) => {
if (reason === 'time' && !validated) { // Send timeout message only if not already validated
await interaction.user.send(cfg.messages.tookTooLong);
} else if (!validated) {
@@ -117,11 +145,11 @@ module.exports = {
});
}).catch(async (err) => {
await interaction.reply({
content: cfg.messages.unableToDM,
ephemeral: true
});
await interaction.reply({
content: cfg.messages.unableToDM,
ephemeral: true
});
});
}
const captcha = await generateCaptcha(cfg.captchaCount);