-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
365 lines (345 loc) · 13 KB
/
start.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const templateHandler = require("./handlers/templatehandler.js");
const Discord = require("discord.js");
const settingsHandler= require("./handlers/settingshandler.js");
const subHandler= require("./handlers/subhandler.js");
const fs = require("fs");
var settings,subs;
const TOKEN = ""; //tokenhere
const Intents = new Discord.IntentsBitField();
for(e in Discord.IntentsBitField.Flags){
Intents.add(e);
}
const bot = new Discord.Client({intents: Intents});
async function main() {
settings=await settingsHandler.loadsettings("./settings.json");
subs= await subHandler.readsubs("./subs.json");
bot.login(TOKEN);
}
main();
bot.on('ready', c => {
console.log(`Logged in as ${c.user.tag}!`);
bot.user.setActivity("Put your templates in the designated channel");
});
bot.on('messageCreate', msg =>{
if(msg.author.bot) return;
var command=msg.content.split(" ");
//Border to Admin commands
if(settings.AdminIDs.includes(msg.author.id)){
switch(command[0]){
case settings.prefix+"setprefix":
settings.prefix=command[1];
settingsHandler.writesettings(settings,"./settings.json");
msg.channel.send("Prefix set to: "+settings.prefix);
break;
//changes canvas size, on the next generating of the main template this will be applied
case settings.prefix+"setcanvas":
settings.canvasSize_x=command[1];
settings.canvasSize_y=command[2];
settingsHandler.writesettings(settings,"./settings.json");
msg.reply("Canvas size set to: "+settings.canvasSize_x+"x"+settings.canvasSize_y);
break;
case settings.prefix+"addadmin":
mention = msg.mentions.users.first().id;
if(settings.AdminIDs.includes(mention)){
msg.channel.send("Admin already exists");
}
else{
settings.AdminIDs.push(mention);
settingsHandler.writesettings(settings,"./settings.json");
msg.channel.send("Admin added")
}
break;
//addmanager command, adds a discordID to the settings.ManIDs array
case settings.prefix+"addmanager":
mention = msg.mentions.users.first().id;
if (settings.ManIDs.includes(mention)){
msg.channel.send("Manager already exists");
}
else{
settingsHandler.addmanager(settings,mention);
settingsHandler.writesettings(settings,"./settings.json");
msg.channel.send("Manager added: " +msg.mentions.users.first().username);
}
case settings.prefix+"bye":
msg.channel.send("**I'll be back**")
break;
//remmanager command, removes a discordID from the settings.ManIDs array
case settings.prefix+"remmanager":
mention = msg.mentions.users.first().id;
if (settings.ManIDs.includes(mention)){
settingsHandler.remmanager(settings,mention);
settingsHandler.writesettings(settings,"./settings.json");
}
}
}
//border to manager commands
if(settings.ManIDs.includes(msg.author.id) || settings.AdminIDs.includes(msg.author.id)){
switch(command[0]){
case settings.prefix+"gist":
if(typeof command[1] !== 'undefined' && typeof command[2] !== 'undefined'){
x=command[1];
y=command[2];
templateHandler.gisty(msg,settings,x,y).then(()=>{msg.reply("Gist updated!")})
}
else{
msg.reply("Please provide x and y coordinates")
}
break;
case settings.prefix+"OverlayOff":
if(command[1] in subs.subs){
msg.channel.send("Disabling "+command[1]+" for user Overlay")
templateHandler.toggleTemplate(msg,settings,command[1]);
}
else{
msg.channel.send("Subreddit not found, please add it first");
}
break;
case settings.prefix+"OverlayOn":
if(command[1] in subs.subs){
msg.channel.send("Enabling "+command[1]+" for user Overlay")
templateHandler.untoggleTemplate(msg,settings,command[1]);
}
else{
msg.channel.send("Subreddit not found, please add it first");
}
break;
case settings.prefix+"BotOff":
if(command[1] in subs.subs){
msg.channel.send("Disabling "+command[1]+" for bot Overlay")
templateHandler.togglebot(msg,settings,command[1]);
}
else{
msg.channel.send("Subreddit not found, please add it first");
}
break;
case settings.prefix+"BotOn":
if(command[1] in subs.subs){
msg.channel.send("Enabling "+command[1]+" for bot Overlay")
templateHandler.untogglebot(msg,settings,command[1]);
}
else{
msg.channel.send("Subreddit not found, please add it first");
}
break;
//addrep command, adds a discordID of an representative to the sub.reps array
case settings.prefix+"AddRep":
if(typeof msg.mentions.users.first() !== 'undefined'){
console.log(typeof msg.mentions.users)
mention = msg.mentions.users.first().id;
}
else{
msg.reply("please provide a user per @mention")
}
if (command[1] in subs.subs){
if (!subs.subs[command[1]].reps.includes(mention)){
newrep = msg.mentions.users.first();
subHandler.addrep(subs,command[1],mention);
subHandler.writesubs(subs,"./subs.json");
msg.channel.send("Rep added: "+msg.mentions.users.first().username);
}
else{
msg.channel.send("Rep already exists");
}
}
else{
msg.channel.send("Subreddit not found, please add a template first");
}
break;
case settings.prefix+"RemRep":
mention = msg.mentions.users.first().id;
if (command[1] in subs.subs){
if (subs.subs[command[1]].reps.includes(mention)){
subHandler.remrep(subs,command[1],mention);
subHandler.writesubs(subs,"./subs.json");
msg.channel.send("Rep removed: "+msg.mentions.users.first().username);
}
else{
msg.channel.send("Rep not found");
}
}
else{
msg.channel.send("Subreddit not found, please add a template first");
}
break;
case settings.prefix+"updateTemplate":
if(command[1] in subs.subs){
msg.channel.send("checking template \n please wait...")
file = msg.attachments.first();
if (!file) {
msg.reply("**ERROR!** No Template File given")
return;
}
if (file.width != settings.canvasSize_x || file.height != settings.canvasSize_y){
msg.reply("**ERROR!** Template size does not equal the current size of the r/place canvas!")
return false;
}
templateHandler.testTemplate(msg,settings,command[1]).then(result => {
isvalid = result;
if(isvalid=="false"){
msg.reply("This template will overwrite the current templates **SIZE** or **NUMBER OF ALOCATED PIXELS**, please confirm with yes");
let filter = message => message.author.id === msg.author.id
msg.channel.awaitMessages({
filter,
max: 1,
time: 600000,
errors: ['time']
}).then(collected=>{
msgreply=collected.first()
if(msgreply.content.split()[0]=="yes"){
msg.reply("Updating...")
templateHandler.updateTemplate(msg,settings,command[1]).then(() =>msg.reply("Template force-updated!"));
return;
}
else{
msg.reply("Template not updated");
return;
}
}).catch((error)=>{
msg.channel.send("No response, update aborted")
console.log(error)
}
);
}
else{
templateHandler.updateTemplate(msg,settings,command[1]).then(msg.reply("Template updated!"));
}
});
}
else{
msg.reply("Subreddit not found, please add a template first");
}
break;
//addsub command, adds a template slot and afterwards adds the sub with template
//since this is a admin command this will force the template to be added
case settings.prefix+"AddSub":
if (command[1] in subs.subs){
msg.reply("Subreddit already exists");
}
else{
templateHandler.addslot(msg,settings).then(result => {
if(result){
subHandler.addsub(subs,command[1],command[2]);
subHandler.writesubs(subs,"./subs.json");
}
else{
msg.reply("Subreddit not added");
}
}
);
}
break;
//removes a sub from the subs array
case settings.prefix+"RemSub":
if (command[1] in subs.subs){
subHandler.remsub(subs,command[1]);
subHandler.writesubs(subs,"./subs.json");
msg.reply("Subreddit removed");
}
}
}
//border to rep commands
if(subs.allreps.includes(msg.author.id)){
switch(command[0]){
case settings.prefix+"updateTemplate":
if(command[1] in subs.subs){
if(subs.subs[command[1]].reps.includes(msg.author.id)){
templateHandler.testTemplate(msg,settings,command[1]).then(result => {
if(result=="false"){
msg.reply("This template would overwrite the current templates **SIZE** or **NUMBER OF ALOCATED PIXELS**, please ask a manager to make this change")
}
else{
templateHandler.updateTemplate(msg,settings,command[1]).then(msg.reply("Template updated!"));
}
});
}
else{
msg.reply("You are not a representative of this subreddit");
}
}
else{
msg.reply("Subreddit not found, please ask a manager to register your subreddit first");
}
break;
case settings.prefix+"OverlayOff":
if(command[1] in subs.subs){
if(subs.subs[command[1]].reps.includes(msg.author.id)){
msg.reply("Disabling "+command[1]+" for user Overlay")
templateHandler.toggleTemplate(msg,settings,command[1]);
}
else{
msg.reply("You are not a representative of this subreddit");
}
}
else{
msg.reply("Subreddit not found, please ask a manager to register your subreddit first");
}
break;
case settings.prefix+"OverlayOn":
if(command[1] in subs.subs){
if(subs.subs[command[1]].reps.includes(msg.author.id)){
msg.reply("Enabling "+command[1]+" for user Overlay")
templateHandler.untoggleTemplate(msg,settings,command[1]);
}
else{
msg.reply("You are not a representative of this subreddit");
}
}
else{
msg.reply("Subreddit not found, please ask a manager to register your subreddit first");
}
break;
case settings.prefix+"BotOff":
if(command[1] in subs.subs){
if(subs.subs[command[1]].reps.includes(msg.author.id)){
msg.reply("Disabling "+command[1]+" for bot Overlay")
templateHandler.togglebot(msg,settings,command[1]);
}
else{
msg.reply("You are not a representative of this subreddit");
}
}
else{
msg.reply("Subreddit not found, please ask a manager to register your subreddit first");
}
break;
case settings.prefix+"BotOn":
if(command[1] in subs.subs){
if(subs.subs[command[1]].reps.includes(msg.author.id)){
msg.reply("Enabling "+command[1]+" for bot Overlay")
templateHandler.untogglebot(msg,settings,command[1]);
}
else{
msg.reply("You are not a representative of this subreddit");
}
}
else{
msg.reply("Subreddit not found, please ask a manager to register your subreddit first");
}
break;
}
}
//general public comands
switch(command[0]){
case settings.prefix+"getToggled":
toggled=fs.readFileSync("./toggled.csv").toString().replace("\n"," ")
toggledbot=fs.readFileSync("./toggledbot.csv").toString().replace("\n"," ")
msg.reply("Currently the following subreddits are being toggled for the overlay: "+toggled +"\n for the bot: "+toggledbot)
break;
case settings.prefix+"getSubs":
var subis=""
for(sub in subs.subs){
subis +=sub+", "
}
msg.reply("Currently the following subreddits are being represented: \n" +subis)
break;
case settings.prefix+"getTemplate":
if(command[1] in subs.subs){
msg.reply("fetching template...")
templateHandler.returnTemplate(msg,command[1]);
}
else{
msg.reply("Subreddit not found, please add a template first");
}
break;
}
});