-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiscordTools.js
284 lines (259 loc) · 7.57 KB
/
DiscordTools.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
module.exports.DiscordTools = function (props) {
const admin = require("firebase-admin");
const Discord = require("discord.js");
var dbData = {};
var DiscordAPI = "";
var userID = props.userID;
console.log("|D| Booting Discord API Bot");
async function getDBData() {
var db = admin.firestore();
db
.collection("Secrets")
.get()
.then((snapshot) => {
snapshot.forEach((doc) => {
var key = doc.id;
var data = doc.data();
data["key"] = key;
dbData[key] = data;
});
DiscordAPI = String(dbData.APIKeys.DiscordAPI);
//Begin Development With ENV Variables
function setDiscordOnline() {
var db3 = admin.firestore();
db3
.collection("Secrets")
.doc("MetaData")
.set({ DiscordOn: true }, { merge: true });
}
setDiscordOnline();
const client = new Discord.Client();
const data = [];
//Login To DiscordAPI
client.login(DiscordAPI);
// Log When Ready
client.on("ready", () => {
console.log(`|D| Logged in as ${client.user.tag}!`);
///Find User and Send Message
const list = client.users.cache.find(
(user) => user.username === "JasonHoku"
);
// client.channels.cache.forEach((channelEl) => {
// console.log(channelEl.name);
//
// if (channelEl.name === "🤖-bot-feed") {
// channelEl.send("I am HokuBot! Beep.");
// }
// });
//
//
// Detect On Message Events + Reply
client.on("message", (msg) => {
async function addCount() {
var db = admin.firestore();
db
.collection("Public")
.doc("RunCounter")
.get()
.then((doc) => {
db
.collection("Public")
.doc("RunCounter")
.set({ count: parseInt(doc.data().count) + 1 });
});
}
data.push(msg);
if (msg.content === "ping") {
msg.reply("pong");
}
if (msg.content === "!stats") {
addCount();
var db = admin.firestore();
db
.collection("Public")
.doc("RunCounter")
.get()
.then((doc) => {
msg.reply(`Run Counter: ${parseInt(doc.data().count) + 1}`);
});
}
if (msg.content.includes("!todo")) {
let todoList;
let listArray = [];
var db = admin.firestore();
db
.collection("Users")
.doc(String(userID))
.get()
.then((doc) => {
todoList = JSON.parse(JSON.stringify(doc.data())).Todo;
String(todoList)
.split(",")
.forEach((todo) => listArray.push(todo));
db.collection("Public").doc("GeneratedData").set(
{
LatestRun: admin.firestore.FieldValue.serverTimestamp(),
ToDoList: todoList,
},
{ merge: true }
);
});
addCount();
//
//Split !todo Message
let splittedMessage = String(msg.content).split(" ");
if (splittedMessage[1] === "add") {
//
//Todo Add !todo add
}
//remove todo item !todo fin X
if (splittedMessage[1] === "fin") {
async function runTodoRemove() {
let todoList = "";
let listArray = [];
let removedTodoList = "";
let removedListArray = [];
let newJoinedListArray = [];
var db = admin.firestore();
// Remove from current Todo
db
.collection("Users")
.doc(String(userID))
.get()
.then((doc) => {
todoList = JSON.parse(JSON.stringify(doc.data())).Todo;
// Detect if in Todo
todoList.forEach((todo) => {
if (!String(todo).includes(splittedMessage[2])) {
if (todo != ",") {
if (todo != " ") {
listArray.push(todo);
//set new array
db
.collection("Users")
.doc(String(userID))
.update({ Todo: listArray });
}
}
}
});
//Development
//Add to FinTodo
//Query FinTodo
db
.collection("Users")
.doc(String(userID))
.get()
.then((doc) => {
removedTodoList = JSON.parse(JSON.stringify(doc.data())).TodoFin;
//Query Join New Fin Item to Fin List
removedTodoList.forEach((todo) => {
removedListArray.push(todo);
//Check for dupes
removedListArray.forEach((newTodo) => {
if (!String(splittedMessage[2]).includes(newTodo)) {
removedListArray.push(splittedMessage[2]);
}
});
});
msg.reply(`${splittedMessage[2]} Removed!`);
});
});
}
// const todoList = JSON.parse(
// await fs.readFile(
// "./liveData/todo.json",
// "UTF-8"
// )
// );
// const listArray = todoList.List;
// const removedListArray =
// todoList.RemovedList;
// listArray.forEach((element) => {
// if (
// !element.includes(splittedMessage[2])
// ) {
// joinArrays.List.push(element);
// fs.writeFile(
// "./liveData/todo.json",
// JSON.stringify(joinArrays, null, 4),
// "UTF-8"
// );
// }
// });
// removedListArray.forEach((element) => {
// joinArrays.RemovedList.push(element);
// fs.writeFile(
// "./liveData/todo.json",
// JSON.stringify(joinArrays, null, 4),
// "UTF-8"
// );
// });
runTodoRemove().then(msg.reply(`${splittedMessage[2]} Finished!`));
} else {
if (msg.content === "!todo") {
//run todo echo stats
async function runTodo() {
addCount();
async function readTodo() {
// console.log(todoList);
// console.log(listArray);
setTimeout(() => {
msg.reply(`${listArray.length} ToDos: ${listArray}`);
}, 1500);
}
readTodo();
}
runTodo();
}
}
}
});
});
//
function detectDiscordOffOn() {
if (detectDiscordOffOnInterval) {
clearInterval(detectDiscordOffOnInterval);
}
var detectDiscordOffOnInterval;
detectDiscordOffOnInterval = setInterval(() => {
var db = admin.firestore();
db
.collection("Secrets")
.get()
.then((snapshot) => {
snapshot.forEach((doc) => {
var key = doc.id;
var data = doc.data();
data["key"] = key;
dbData[key] = data;
});
if (client && client.user)
console.log("|D| Discord Connection: " + client.user.bot);
if (!dbData.MetaData.DiscordOn) {
if (client && client.user) client.destroy();
if (client && client.user)
console.log("|D| Quitting Discord Connection: " + client.user.bot);
async function setDiscordOffline() {
var db4 = admin.firestore();
db4
.collection("Secrets")
.doc("MetaData")
.set({ DiscordOn: false }, { merge: true });
}
if (!dbData.MetaData.DiscordOn) {
console.log("|D| Discord Connection Quit");
setDiscordOffline().then(() => {
clearInterval(detectDiscordOffOnInterval);
client.destroy();
});
}
}
});
}, 60000);
}
detectDiscordOffOn();
});
}
getDBData();
};