-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathverify.js
More file actions
209 lines (182 loc) · 6.5 KB
/
verify.js
File metadata and controls
209 lines (182 loc) · 6.5 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var otpGenerator = require("otp-generator");
var nodemailer = require("nodemailer");
const user = require("./utils/db/discordMemberHandlers");
const { addRole, ban, removeRole } = require("./utils/guildMemberHandlers");
const getInstructions = `**✨Hey! Welcome to the KPH server✨**
You will need to enter here your institute email-id so that we can verify you.
Enter the email-id with '/' as prefix.
For example: If your email-id is abcd@example.com, reply /abcd@example.com.
After verifying it, the bot will send an OTP to the given mail-id.
*You will only get 5 minutes to enter a valid institute email-id.*
**In case of any issues, seek help in the #admin-support channel on the server.**`;
const getOTPInstructions = `**OTP sent! Please check your email!**
Enter the OTP with '/' as prefix,
e.g. if OTP is 123456, send /123456.
*OTP expires in 5 minutes.*`;
const sendMail = async (email) => {
let mailTransporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: process.env.SENDER_MAIL_ID,
clientId: process.env.OAUTH_CLIENT_ID,
clientSecret: process.env.OAUTH_CLIENT_SECRET,
refreshToken: process.env.OAUTH_REFRESH_TOKEN,
accessToken: process.env.OAUTH_ACCESS_TOKEN,
},
});
let OTP = otpGenerator.generate(6, {
upperCase: false,
specialChars: false,
});
let mailDetails = {
from: process.env.SENDER_MAIL_ID,
to: email,
subject: "Knuth Programming Hub Discord Server - Verification",
text: `This is your OTP for verification: "${OTP}".
Do not share this with anyone!
If you didn't request for it, then please ignore this mail.`,
};
await mailTransporter.sendMail(mailDetails).then(
(data) => {},
(err) => {
OTP = null;
}
);
mailTransporter.close();
return OTP;
};
const checkIfVerified = async (bot, discordUser) => {
var flag = false;
await bot.guilds.fetch(process.env.SERVER_GUILD_ID).then((guild) => {
guild.members.fetch(discordUser.id).then((member) => {
if (member.roles.cache.find((r) => r.name === "JIITian")) {
flag = true;
}
});
});
return flag;
};
const validateEmail = (email) => {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
};
const validateCollegeEmail = (email) => {
email = email.toLowerCase();
const name = email.substring(0, email.lastIndexOf("@"));
const domain = email.substring(email.lastIndexOf("@") + 1);
let isnum = /^\d+$/.test(name); // Check if string is all numbers, to avoid faculties from joining server.
let batch = "20" + name[0] + name[1];
if (name[0] == "9" && name[1] == "9") {
batch = batch = "20" + name[2] + name[3]; // Enroll of 128 guys start from 99.
}
batch = Number(batch);
if (domain !== "mail.jiit.ac.in" || isnum === false) {
return [false, "notJIIT"];
} else {
return [true, String(batch + 4)];
}
};
const handleFail = async (bot, dmChannel, discordUserId) => {
const failCount = await user.updateFailCount(discordUserId);
await dmChannel.send(`*No. of attempts left: ${3 - failCount}*`);
if (failCount >= 3) {
user.remove(discordUserId);
await dmChannel.send("You are banned from the server!!");
ban(bot, discordUserId);
}
};
const filter = (m) => m.content.startsWith("/");
module.exports = async (bot, discordUser, prefix) => {
const dmChannel = await discordUser.createDM();
const check = await checkIfVerified(bot, discordUser);
if (check === true) {
dmChannel.send("You have been already verified as JIITian.");
return;
}
dmChannel.send(getInstructions).then(async () => {
let flag = false;
let batchTag = "";
await dmChannel
.awaitMessages(filter, {
max: 1,
time: 300000,
errors: ["time"],
})
.then(async (message) => {
message = message.first(); // User should now reply with email.
const email = message.content.substring(1);
if (validateEmail(email) === false) {
flag = true;
dmChannel.send(
`Hmm, that doesn't look like an email address. Send ${prefix}verify on the server to try again.`
);
handleFail(bot, dmChannel, discordUser.id);
return;
}
const [ch, batch] = validateCollegeEmail(email);
batchTag = batch;
if (ch === false) {
flag = true;
dmChannel.send(
`Hmm, that doesn't look like a JIIT email address. Note that this verification is for **JIIT students** only. Send ${prefix}verify on the server to try again.`
);
handleFail(bot, dmChannel, discordUser.id);
return;
}
sentOTP = await sendMail(email);
if (sentOTP === null) {
flag = true;
dmChannel.send(
`It seems that there was some error. Send ${prefix}verify on the server to try again.`
);
}
})
.catch((err) => {
flag = true;
discordUser.send(
`Time's up! Send ${prefix}verify on the server to try again.`
);
handleFail(bot, dmChannel, discordUser.id);
});
if (flag === true) {
return;
}
dmChannel.send(getOTPInstructions).then(async () => {
await dmChannel
.awaitMessages(filter, {
max: 1,
time: 300000,
errors: ["time"],
})
.then((message) => {
message = message.first(); //User should now reply with the OTP
let recvOTP = message.content.substring(1);
if (String(sentOTP) !== String(recvOTP)) {
flag = true;
dmChannel.send(
`Wrong OTP! Send ${prefix}verify on the server to try again.`
);
handleFail(bot, dmChannel, discordUser.id);
} else {
removeRole(bot, discordUser, "Member");
addRole(bot, discordUser, "JIITian");
addRole(bot, discordUser, batchTag);
user.updateBatch(discordUser.id, batchTag);
dmChannel.send(
"Yay! You have verified yourself as a JIITian and are now officially a member of the KPH Discord server! 🎉"
);
}
})
.catch((err) => {
flag = true;
discordUser.send(
`Time's up! Send ${prefix}verify on the server to try again.`
);
handleFail(bot, dmChannel, discordUser.id);
});
});
});
};