-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheets.js
115 lines (102 loc) · 2.68 KB
/
sheets.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
const { GoogleSpreadsheet } = require('google-spreadsheet');
const creds = require('./google_creds.json');
require('dotenv').config();
const loadData = async () => {
try {
const doc = new GoogleSpreadsheet(process.env.SHEETS_ID);
await doc.useServiceAccountAuth(creds);
await doc.loadInfo();
return doc;
}
catch (error) {
console.log(error);
return false;
}
};
const addUser = async (discord, email, token) => {
try {
const timestamp = Date().toLocaleString('en-US', { timeZone: 'EST' });
const doc = await loadData();
const sheet = doc.sheetsByIndex[0];
await sheet.loadCells();
const rows = await sheet.getRows();
let row;
// retrieve row if it exists
for (let i = 0 ; i < rows.length; i += 1) {
if (rows[i].discord == discord || email == rows[i].email) {
row = rows[i];
break;
}
}
if (row) {
// disallow changes once verified
if (row.verified == 'Yes') {
// email was verified to a different discord
if (discord != row.discord) {
return [false, `Email ${email} has already been vefied to a different discord!`];
}
// otherwise
return [false, 'You are already verified! If you don\'t get the new role please message an admin!'];
}
else {
// update timestamp, token, and email
row.timestamp = timestamp;
row.token = token;
row.email = email;
row.discord = discord;
row.save();
return [true, ''];
}
}
else {
await sheet.addRow({
timestamp: timestamp,
email: email,
discord: discord,
verified: 'No',
token: token,
});
console.log(`Added a new unverified user! ${discord} ${email}`);
return [true, ''];
}
}
catch (error) {
console.log(error);
return [false, 'An Error has occurrred! Please Contact an admin for help.'];
}
};
const verifyUser = async (discord, token) => {
try {
const timestamp = Date().toLocaleString('en-US', { timeZone: 'EST' });
const doc = await loadData();
const sheet = doc.sheetsByIndex[0];
await sheet.loadCells();
const rows = await sheet.getRows();
for (let i = 0 ; i < rows.length; i += 1) {
if (rows[i].discord == discord) {
if (rows[i].verified == 'Yes') {
return 'You\'re already verified!';
}
else if (rows[i].token == token) {
rows[i].timestamp = timestamp;
rows[i].verified = 'Yes';
await rows[i].save();
console.log(`Verified a new user! ${discord} ${rows[i].email}`);
return 'You are verified!';
}
else {
return 'Wrong token!';
}
}
}
return 'No entries found, did you try /email?';
}
catch (error) {
console.log(error);
return 'An error has occurred! Please contact an admin';
}
};
module.exports = {
addUser: addUser,
verifyUser: verifyUser,
};