-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.js
255 lines (213 loc) · 8.5 KB
/
application.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const fs = require('fs');
const fsp = require('fs').promises;
const config = require('./config.json');
const { google } = require("googleapis");
const service = google.sheets("v4");
const credentials = require("./credentials.json");
const voting = require('./commands/vote.js');
const confirmEmote="✅";
const voteEmote="🇻";
const memberEmote="🇲";
const denyEmote="❌";
function getConf() {
let data = fs.readFileSync('appdata.json');
let apps = JSON.parse(data);
return apps;
}
// Runs in the background checking for new applications every x mintues
function checkForApps(bot) {
console.log('Checking for apps now')
if (!fs.existsSync('./appdata.json')) {
// stupid issue storing emojis in json bruh
let appdata = {
checked: 0,
appCategory:'',
newAppChannel:'',
archiveChannel:"",
internRole:"",
applications: [],
};
let data = JSON.stringify(appdata);
fs.writeFileSync('appdata.json', data);
}
// Configure auth client
const authClient = new google.auth.JWT(
credentials.client_email,
null,
credentials.private_key.replace(/\\n/g, "\n"),
["https://www.googleapis.com/auth/spreadsheets"]
);
// 5 minute timer thing
var minutes = 1;
setInterval(function() {
console.log("I am doing my 5 minutes check");
// do your stuff here
(async function () {
try {
const conf = getConf();
// Authorize the client
const token = await authClient.authorize();
// Set the client credentials
authClient.setCredentials(token);
// Get the rows
const res = await service.spreadsheets.values.get({
auth: authClient,
spreadsheetId: config.spreadsheetID,
range: "A:D", // determines the colums to grab
});
const rows = res.data.values;
// Check if we have any data and if we do add it to our answers array
if (rows.length > conf.checked+1) {
// Remove the headers
rows.shift();
// For each row
let executed = 0;
for (let i = conf.checked; i < rows.length; i++) {
executed++;
const appChannel = await bot.channels.cache.get('933431313405452390'); // using temp channel id
const message = await bot.channels.cache.get(getAppChannel()).send('Detected ' + rows[i][1])
const thread = await appChannel.threads.create({
name: rows[i][1] + '-discussion',
autoArchiveDuration:10080, // 1wk archive time
reason: 'Private conversation about an applicant',
});
await message.react(confirmEmote);
await message.react(denyEmote);
let app = parseJSONToApp(rows[i]);
app.messageID = message.id;
app.thread = thread.id
conf.applications.push(app);
if (i == rows.length-1) {
conf.checked += executed;
fs.writeFileSync('./appdata.json', JSON.stringify(conf));
}
}
} else {
console.log("No new data found.");
}
// add more stuff here
} catch (error) {
console.log(error);
// Exit the process with error
process.exit(1);
}
})();
}, minutes * 10 * 1000); // remember to change this back later :)
}
// Creates JS object
function parseJSONToApp(appInput) {
return application = {
applicant: appInput[1], // discord name for applicant (ID?)
startTime: appInput[0], // time form is submitted
messageID:"", // Id for the main message
confirmed: false, // approved for consideration
confirmedBy:"", // ID of person who confirmed the app
approved: false, // approved for intern
approvedBy:"", // ID of person who approved the app
appChannel: "", // id for application channel
appDiscussionChannel: "", // id for discussion channel
endTime: "", // time application is finished
}
}
// Creates CV like image to post
function parseJSONToImage(appInput) {
}
// Approves application for useage
function approveApplication(id, c, confirmedBy) {
let index = getApplicationIndexFromID(id);
let conf = getConf();
let app = conf.applications[index];
app.confirmedBy = confirmedBy;
app.confirmed = true;
app.appChannel = c;
fs.writeFileSync('./appdata.json', JSON.stringify(conf));
}
// Uses message id to get the applications index from the application list
function getApplicationIndexFromID(id) {
let conf = getConf();
for (let i = 0; i < conf.applications.length; i++) { //(application in apps.applications) {
if (conf.applications[i].messageID == id) {
return i;
}
}
}
// Confirms membership for an incoming applicant
async function confirmMembership(id, bot) {
let conf = getConf();
let application = conf.applications[getApplicationIndexFromID(id)];
let role = bot.guilds.cache.get('822581610749886505').roles.cache.get(conf.internRole);
let applicant = bot.users.cache.get('520407069183180802');
// let applicant = bot.users.cache.find(u => u.tag === application.applicant);
console.log(applicant);
console.log('gave applicant the role');
// cleanup here, just not yet
}
function getAppCategory() {
return getConf().appCategory;
}
function getAppChannel() {
return getConf().newAppChannel;
}
function getAppNameFromID(id) {
return getConf().applications[getApplicationIndexFromID(id)].applicant; // noooo this breaks
}
function appExists(id) {
if (typeof getApplicationIndexFromID(id) === 'undefined') {
return false;
}
return true;
}
async function handleReaction(reaction, user, bot) {
switch(reaction.emoji.name) {
// Approve initial application
case (confirmEmote):
confirmApplicant(reaction, user, bot);
break;
// Create application vote
case (voteEmote):
voting.saveApplicationVote(getAppNameFromID(reaction.message.id), " thing ", reaction.message);
break;
// Approve for archive and membership
case (memberEmote):
console.log('confiming membership');
confirmMembership(reaction.message.id, bot);
break;
// deny and wipe application record
case (denyEmote):
cleanup(reaction.message.id, bot);
break;
}
}
async function confirmApplicant(reaction, user, bot) {
const name = getAppNameFromID(reaction.message.id)
reaction.message.react(voteEmote);
const appChannel = await bot.channels.cache.get('933431313405452390'); // using temp channel id
reaction.message.guild.channels.create(getAppNameFromID(reaction.message.id) + "-application" ).then( channel => {
channel.setParent(getAppCategory());
approveApplication(reaction.message.id, channel.id, user.id);
});
}
async function updateFromVote(id, passed, bot) {
let conf = getConf();
let vote = getAppNameFromID(id);
if(passed) {
let msg = await bot.channels.cache.get(conf.newAppChannel).messages.fetch(id);
msg.react(memberEmote);
}
}
// an alternate idea here would be to edit the og message and link to the message archive
// end and save the app at any point
// fix early exit btw
async function cleanup(id, bot) {
console.log('cleaning up');
let conf = getConf();
let index = getApplicationIndexFromID(id);
let app = conf.applications[index];
// here we archive stuff later
await bot.channels.cache.get(conf.newAppChannel).messages.fetch(id).then(msg => msg.delete());
await bot.channels.cache.get(app.appChannel).delete();
await bot.channels.cache.get(app.appDiscussionChannel).delete();//will need to change this
conf.applications.splice(index--,1);
fs.writeFileSync('./appdata.json', JSON.stringify(conf));
}
module.exports = { checkForApps, appExists, handleReaction, updateFromVote };