forked from shomq/discordcaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (129 loc) Β· 6.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Module Imports and instances
const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs");
const snekfetch = require("snekfetch");
const jimp = require("jimp");
const sql = require("sqlite");
sql.open("./src/db.sqlite");
class Captcha {
/**
* @param {string} captcha - The captcha (pass null and call generate method if it shall be random)
* @param {object} author - The author object (Has to has an id property and should look like <@123456789>)
* @param {buffer} image buffer - Initialize object with an already existing image buffer
*/
constructor(captcha, author, buff) {
this._captcha = captcha;
}
/**
* @returns {string} Captcha value of class
*/
generate() {
let rand = Math.random().toString(36).substr(2, 6);
this.captcha = rand;
return this.captcha;
}
get captcha() {
return this._captcha;
}
set captcha(value) {
this._captcha = value;
}
}
// Command Imports
const config = require("./src/config.json");
const callback_ = err => {
if (err) console.log(err);
};
let queue = [],
latestVersion;
snekfetch.get("https://raw.githubusercontent.com/y21/discordcaptcha/master/src/config.json")
.then(r => {
if (JSON.parse(r.body).version != config.version) {
console.log("### A new version of discordcaptcha is available! (Latest: " + JSON.parse(r.body).version + ")\n\n");
}
latestVersion = JSON.parse(r.body).version;
}).catch(console.log);
client.on("ready", () => {
try {
console.log("Logged in!");
client.user.setActivity(config.streamingGame, {
url: config.streamingLink,
type: "STREAMING"
});
if (client.guilds.size > 1) {
console.log("It looks like this bot is on more than one guild. It is recommended not to have this bot on more than one since it could do random stuff.")
}
client.guilds.forEach(guild => {
if (!guild.roles.get(config.userrole)) console.log(`${guild.name} has no userrole or the snowflake that was given in the config file is invalid.`);
});
} catch (e) {
console.log("[DISCORDCAPTCHA-readyEvent] >> " + e);
}
});
client.on("warn", console.warn);
client.on("error", console.error);
client.on("disconnect", () => console.log("Bot disconnected from WebSocket!"));
client.on("reconnect", () => console.log("Reconnecting to WebSocket ..."));
client.on("message", async (message) => {
try {
let blocked = await sql.get('select * from blocked where id="' + message.author.id + '"');
if (blocked) message.member.kick();
if (message.channel.name === "verify") {
if (message.author.id != client.user.id) message.delete();
else setTimeout(() => message.delete(), 2500);
if (message.content === `${config.prefix}verify`) {
if (await sql.get('select * from queries where id="' + message.author.id + '"') || message.member.roles.has(config.userrole)) return message.reply("Already verified or in queue!");
let captchaInstance = new Captcha(null, message.author);
let captcha = captchaInstance.generate();
if (config.captchaType == "image") {
let _image = await jimp.read("https://i.imgur.com/mkoc2Fh.png");
let _font = await jimp.loadFont(jimp.FONT_SANS_64_BLACK);
let _coordinates = [Math.random() * 400, Math.random() * 400]; // x & y coordinates for text on image
_image.resize(750, 750); // make bigger
_image.print(_font, _coordinates[0], _coordinates[1], captcha); // print captcha on image
message.author.send(new Discord.RichEmbed()
.setTitle("Verification")
.setDescription("This guild is protected by discordcaptcha, an open-source verification bot made by y21#0909.")
.addField("Instructions", `In a few seconds an image will be sent to you which includes a number. Please send ${config.prefix}verify <captcha> into the channel ${message.channel.name} (${message.channel})`)
.setColor("RANDOM")
.setTimestamp()
).catch(e => e.toString().includes("Cannot send messages to this user") ? message.reply("please turn on dms") : null);
_image.getBuffer(jimp.MIME_PNG, (err, buff) => {
message.author.send(new Discord.Attachment(buff, "captcha.png"));
});
} else if (config.captchaType == "text") {
message.author.send(new Discord.RichEmbed()
.setDescription("Paste the code below in the verify channel to get verified.\n\n**Verification Bot made by y21#0909**")
);
message.author.send(`\`\`\`${config.prefix}verify ${captchaInstance.captcha}\`\`\``);
}
sql.run('insert into queries values ("' + message.author.id + '")');
message.channel.awaitMessages(msg => msg.content === config.prefix + "verify " + captchaInstance.captcha && msg.author === message.author, {
max: 1,
errors: ["time"]
})
.then(() => {
message.author.send({
embed: {
color: 0x00ff00,
description: "Successfully verified on `" + message.guild.name + "`"
}
});
let logChannel = client.channels.get(config.chat) || client.channels.find("name", config.chat);
if (logChannel && logChannel.type === "text") logChannel.send(`${message.author.toString()} was successfully verified.`);
if (config.logging) sql.run('insert into logs values ("' + message.author.id + '", "' + Date.now() + '")');
sql.run('delete from queries where id="' + message.author.id + '"');
queue.pop();
message.member.addRole(config.userrole).catch(console.log);
delete captchaInstance;
}).catch(console.log);
}
}
require("./src/Commands.js")(message, config, Discord, fs, latestVersion); // Command Handler
} catch (e) {
console.log(e);
}
});
process.on("unhandledRejection", console.log);
client.login(config.token);