-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
346 lines (325 loc) · 13 KB
/
main.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
const Discord = require("discord.js")
const client = new Discord.Client()
const fs = require('fs');
const fsPromises = fs.promises
const eventEmitter = require('events');
const { version } = require('./package.json');
// CONFIG DETERMINATION (reads different files with 'live' or 'dev' command line param)
var config;
if (process.argv.length > 2) {
switch (process.argv[2]) {
case ("live"): {config = JSON.parse(fs.readFileSync("./cfg_live.json")); break};
case ("dev"): {config = JSON.parse(fs.readFileSync("./cfg_dev.json")); break};
default: config = JSON.parse(fs.readFileSync("./cfg.json"));
}
} else config = JSON.parse(fs.readFileSync("./cfg.json"));
// Extra config stuff that I want synced across git
//IRA HANDLER
const ira = new eventEmitter()
//local VARIABLES
var limitusers = []
var defaultGuild
var logChannel
//GLOBAL VARIABLES
global.queue = []
global.votes = 0
global.voteusers = []
global.pollobject = JSON.parse(fs.readFileSync("./poll.json"))
global.streamoptions = {volume:0.5, bitrate:192000}
global.defaultVolume = 0.5
global.blacklist = [];
global.config = config;
global.config.version = version;
global.client = client;
global.usermanager = require("./events/usermanager.js")
global.cardmanager = require("./events/cardmanager.js")
global.dropmanager = require("./events/dropmanager.js")
global.defautlGuild;
global.logChannel;
//modules
var general = require("./general/index.js")
var utils = require("./utils/index.js")
var shitpost = require("./shitpost/index.js")
var mod = require("./mod/index.js")
var music = require("./music/index.js")
var dj = require("./dj/index.js")
var botmanage = require("./botmanage/index.js")
var logs = require("./events/logs/index.js")
var poll = require("./events/poll.js")
//RELOAD FUNC
function shutdown(message) {
message.channel.send({embed:utils.embed("happy", "Good night!")}).then( function() {
fs.writeFileSync("./shutdownstatus.json", `{"shutdown":true}`)
client.destroy()
process.exit()
});
}
function reload(arg, message) {
try {
if(config.owners.includes(message.author.id)) {
arg = message.content.split(" ")[1]
switch (arg) {
case "general":
general.refresh()
delete require.cache[require.resolve('./general/index.js')];
general = require('./general/index.js');
delete require.cache[require.resolve('./events/usermanager.js')]
global.usermanager = require("./events/usermanager.js")
delete require.cache[require.resolve('./events/cardmanager.js')]
global.cardmanager = require("./events/cardmanager.js")
delete require.cache[require.resolve('./events/dropmanager.js')]
global.dropmanager = require("./events/dropmanager.js")
embed = utils.embed("happy", `Reloaded module ${arg}`)
message.channel.send({embed})
break;
case "shitpost":
shitpost.refresh()
delete require.cache[require.resolve('./shitpost/index.js')]
shitpost = require('./shitpost/index.js')
embed = utils.embed("happy", `Reloaded module ${arg}`)
message.channel.send({embed})
break;
case "mod":
mod.refresh()
delete require.cache[require.resolve('./mod/index.js')]
mod = require('./mod/index.js')
embed = utils.embed("happy", `Reloaded module ${arg}`)
message.channel.send({embed})
break;
case "botmanage":
botmanage.refresh()
delete require.cache[require.resolve('./botmanage/index.js')]
botmanage = require('./botmanage/index.js')
embed = utils.embed("happy", `Reloaded module ${arg}`)
message.channel.send({embed})
break;
case "dj":
botmanage.refresh()
delete require.cache[require.resolve('./dj/index.js')]
mod = require('./dj/index.js')
embed = utils.embed("happy", `Reloaded module ${arg}`)
message.channel.send({embed})
break;
case "music":
music.refresh(message)
delete require.cache[require.resolve('./music/index.js')]
music = require('./music/index.js')
embed = utils.embed("happy", `Reloaded module ${arg}`)
message.channel.send({embed})
break;
case "logs":
logs.refresh()
delete require.cache[require.resolve('./events/logs/index.js')]
logs = require('./events/logs/index.js')
message.channel.send({embed:utils.embed("happy", `Reloaded module ${arg}`)})
break;
case "poll":
delete require.cache[require.resolve('./events/logs/index.js')]
poll = null
poll = require('./events/poll.js')
break;
case "utils":
utils.refresh()
delete require.cache[require.resolve('./utils/index.js')]
utils = require('./utils/index.js')
message.channel.send({embed:utils.embed("happy", `Reloaded module ${arg}`)})
break;
default:
message.channel.send({embed:utils.embed("malfunction", "`Invalid Option`")})
}
}
else {message.channel.send({embed:utils.embed("malfunction", "`You dont have permission for this command`")})}
} catch (err) {
message.channel.send({embed:utils.embed(`malfunction`,`OH THAT'S NOT GOOD \`\`\`${err}\`\`\``, "RED")})
}
}
//EVAL FUNC
function evalcmd(message) {
let content = message.content.substr(message.content.split(" ")[0].length + 1)
let evalresponse
try {
evalresponse = eval(content)
} catch(err) {
return message.channel.send("An Error Occured with the input " + content + "\n ```" + err.stack + "```")
}
if(evalresponse === undefined) {return message.channel.send("```undefined```")}
else if (evalresponse === null) {message.channel.send("```null```")}
else {
message.channel.send({embed:utils.embed("happy", `Eval response:\`\`\`${evalresponse.toString()}\`\`\``)})
}
}
//READY EVENT
client.on("ready", async function() {
defaultGuild = await client.guilds.resolve(config.guildid)
global.defaultGuild = defaultGuild
logChannel = undefined
if (defaultGuild) {
logChannel = defaultGuild.channels.resolve(config.logs)
global.logChannel = logChannel
if (!logChannel) {
console.log("log channel missing")
}
} else {
console.log("default guild missing")
}
let shutdownstatus = await fsPromises.readFile("./shutdownstatus.json");
if (JSON.parse(shutdownstatus).shutdown == false) {
if (logChannel) {
logChannel.send(null,{embed:utils.embed("malfunction", "OKAY I DON'T THINK I SHUT DOWN PROPERLY THERE", undefined, `Version ${version}`)})
}
} else {
logChannel.send(null,{embed:utils.embed("happy", `HEY IDIOTS I'M RUNNIN' ON VERSION ${version} TODAY`)})
}
fsPromises.writeFile("./shutdownstatus.json", `{"shutdown":false}`, (err) => {})
global.pollobject = JSON.parse(fs.readFileSync("./poll.json"))
global.dropmanager.catchUp();
if (defaultGuild) {
if (global.pollobject.pollmessage != null) {
pollChan = await defaultGuild.channels.resolve(global.pollobject["pollchan"])
pollChan.messages.resolve(global.pollobject.pollmessage)
}
if (defaultGuild.me.voice.channel) {
defaultGuild.me.voice.kick("Cleaning up crashed voice connection")
}
}
})
// COMMAND HANDLING PT 1
// lookupCommand(message, command): searches command lists and returns the corresponding function if it exists.
function lookupCommand(message, command) {
if(general.commandList.includes(command)) {
return general[command].func
}
else if(shitpost.commandList.includes(command)) {
return shitpost[command].func
}
else if(music.commandList.includes(command)) {
if (!message.guild) return;
return music[command].func
}
else if (dj.commandList.includes(command)) { // these commands must check for permissions. currently these are hard-coded and do not respond to the perms specified within the module index.js
if (!message.guild) return;
if (message.member.roles.cache.has(config.djrole) || message.member.roles.cache.has(config.modrole) || config.owners.includes(message.author.id))
return dj[command].func;
else return;
}
else if(mod.commandList.includes(command)) {
if (!message.guild) return;
if(config.owners.includes(message.author.id) || message.member.roles.cache.has(config.modrole)) {
return mod[command].func;
} else return;
}
else if(botmanage.commandList.includes(command)) {
if (!message.guild && !config.owners.includes(message.author.id)) return;
if(config.owners.includes(message.author.id) || message.member.roles.cache.has(config.modrole) ) {
return botmanage[command].func;
} else return;
}
return;
}
//COMMAND HANDLER
client.on("message", message => {
try {
if(client.user.id == message.author.id) return
// if bot is mentioned, forward it to owner
if(message.mentions.has(client.user, options={"ignoreRoles":true, "ignoreEveryone":true})) {
logChannel.send("I've been messaged: "+message.url);
}
// basic input parsing
if(!message.content.startsWith(config.prefix)) return
if(message.content == config.prefix) return
if(global.blacklist.includes(message.author.id)) return
let command = message.content.split(config.prefix)[1]
.split(" ")[0]
.replace(" ", "")
.toLowerCase();
if(!command) return;
if(command === "reload") return reload(message.content.split(" ")[1], message)
if(command === "eval" && config.owners.includes(message.author.id)) return evalcmd(message)
if(command === "shutdown" && config.owners.includes(message.author.id)) return shutdown(message)
var func = lookupCommand(message, command);
if (func) {
if(limitusers.includes(message.author.id)) return message.channel.send({embed:utils.embed("angry", `SLOW DOWN I'M GONNA TRIP A BREAKER!`)}).then((message) => {
setTimeout(function() {
message.delete()
}, config.ratelimitmessage)
})
try {
func.call(client, message)
ira.emit("ratelimit", message.author)
}catch(err) {
var embed = utils.embed(`malfunction`,`OH THAT'S NOT GOOD \`\`\`${err}\`\`\``, "RED")
message.channel.send({embed})
}
}
} catch (err) { // Catch an error not in a promise
utils.messageOwner.func({embed:utils.embed(
`malfunction`,
`Unhandled error due to \`${message.content}\`! \`\`\`${err}\`\`\` ${message.url}`,
"RED",
)});
}
})
client.on('error', (error) => {
console.log("Unhandled Error!")
console.log(error.error)
})
//BOT HANDLER EVENTS
ira.on("ratelimit", (user) => {
// limitusers.push(user.id)
// setTimeout(function() {
// limitusers.splice(limitusers.indexOf(user.id))
// }, config.ratelimit)
})
//events and things
//Poll events
//I DONT FUCKING KNOW WHY IT WORKS LIKE THIS BUT NOT MODULAR I WAS SLEEP DEPRIVED WHEN I WROTE IT AND IT ONLY SEEMS TO WORK IN THESE EXACT CONDITIONS PLEASE HELP
//This is the only bit of code ive written that confuses me just cause it only works here but no where else even though there are no specific dependecies to the client and passing a user object should let me get the client object i honestly have no idea why it only works in this context it should work in modular form but guess what it doesnt even though it should i dont understand it SEND HELP
client.on("messageReactionAdd", (reaction, user) =>{
global.pollobject = JSON.parse(fs.readFileSync("./poll.json"))
if(user == client.user) return
if(global.pollobject.pollmessage == null) return
if(reaction.message.id === global.pollobject.pollmessage) {
reaction.message.reactions.forEach(function(r) {
r.fetchUsers().then(users => {
if(reaction.emoji.name === r.emoji.name) return
if(users.has(user.id)) {
r.users.remove(user)
}
})
})
}
})
client.on("messageReactionAdd", (reaction, user) => {
if(user == client.user) return
global.dropmanager.getDrop(reaction.message).then(doc => {
if (reaction.emoji.name == doc.reaction) {
if (doc.endTime < new Date().getTime()) {
global.dropmanager.endDrop(doc)
} else {
global.dropmanager.claim(undefined, user, doc)
}
}
}).catch(err => {})
})
//LOGS OVER HERE
//log error function
function logerr(err) {
logChannel.send({embed:utils.embed("malfunction", `OH BOY ERROR IN MY LOGS \`\`\`${err}\`\`\``)})
}
//Log events
client.on("messageDelete", (message) => {
try {logs.mdelete(message)}catch(err){logerr(err)}
})
client.on("guildMemberRemove", member => {
try {logs.leave(member)}catch(err){logerr(err)}
})
client.on("guildBanAdd", (guild, user) => {
try {logs.ban(user)}catch(err){logerr(err)}
})
client.on("guildBanRemove", (guild, user) => {
try{logs.unbanned(user)}catch(err){logerr(err)}
})
client.on("messageUpdate", (OldMessage, NewMessage) => {
try{logs.edit(OldMessage, NewMessage)}catch(err){logerr(err)}
})
client.login(config.token)