-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
155 lines (139 loc) · 5.06 KB
/
main.js
File metadata and controls
155 lines (139 loc) · 5.06 KB
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
const { Client, Collection } = require("discord.js");
const botSettings = require("./botSettings.json");
const fs = require("fs");
const functions = require("./functions.js");
const mongoose = require("mongoose");
const client = new Client({partials:['REACTION', 'MESSAGE']});
const shell = require('shelljs');
var schedule = require('node-schedule');
// daily backup
const fileLocation = '~';
const rule = new schedule.RecurrenceRule();
rule.hour = 22;
rule.minute = 0;
var j = schedule.scheduleJob(rule, () => {
const date = new Date();
const fileName = date.getMonth()+1 + "" + date.getDate() + "" + date.getFullYear();
const command = "mongodump --db=dimensionsDB --archive=" + fileName + " --gzip";
shell.cd(fileLocation);
shell.exec(command);
});
client.commands = new Collection();
client.indicators = {
teleporting: [],
usingCommand: []
}
// cache and related;
// ITS SUPER IMPORTANT THAT THESE NAMES RESEMBLE THE COLLECTION NAMES (you use schema.collection.name) in recache
client.cache = {
dimensions: new Collection(),
// member cache is for dimension teleport cooldown
members: new Collection(),
rrmessages: new Collection()
}
client.couldNotCache = {
dimensions: false,
members: false,
rrmessages: false
}
// models
client.models = {
dimension: require("./models/dimension.js"),
member: require("./models/member.js"),
rrmessage: require("./models/rrmessage.js")
}
mongoose.connect('mongodb://localhost:27017/dimensionsDB', { useNewUrlParser: true, useUnifiedTopology: true })
.then(console.log("\nSUCCESSFULLY CONNECTED TO MONGO DB"))
.catch(err => {
if(err) {
console.log("\nCONNECTION TO MONGO DB FAILED: \n" + err)
}
});
// DISABLE ALL DB RELATED FUNCTIONS, OR FIND A WAY TO RESTART USING CODE (client.isConnected = false, if(!client.isConnected) return;)
client.models.dimension.find({}, async (err, docs) => {
if(err) {
console.log("Could not initially (before client ready) cache dimension data.");
client.couldNotCache.dimensions = true;
return;
}
if(docs) {
await docs.forEach(doc => {
client.cache.dimensions.set(doc["_id"], doc);
})
console.log("Initially cached the \'dimensions\' collection!")
}
})
// no reason to cache member data
// client.models.member.find({}, async (err, docs) => {
// if(err) {
// console.log("Could not initially (before client ready) cache members data.");
// client.couldNotCache.members = true;
// return;
// }
// if(docs) {
// await docs.forEach(doc => {
// // for the teleport cooldown feature
// doc.lastTeleport = new Date();
// client.cache.members.set(doc["_id"], doc);
// })
// console.log("Initially cached the \'members\' collection!")
// }
// })
client.models.rrmessage.find({}, async (err, docs) => {
if(err) {
console.log("Could not initially (before client ready) cache rrmessage data.");
client.couldNotCache.rrmessages = true;
return;
}
if(docs) {
await docs.forEach(doc => {
client.cache.rrmessages.set(doc["_id"], doc);
})
console.log("Initially cached the \'rrmessages\' collection!");
}
})
// recent: add client.connect and client.models in the ready thing so if it restarts, the db is recached
// DO SOMETHING WITH THE couldNotCache boolean (restrict functions);
client.on("ready", async () => {
console.log("\n================== READY START ==================")
// logged in
console.log(`Logged in as ${client.user.username}!`);
// generate invite
var invite = await client.generateInvite(["ADMINISTRATOR"]);
console.log(invite);
// set bot user activity
await client.user.setActivity(botSettings.activity.description, {type: botSettings.activity.type})
console.log(`Set activity to \"${botSettings.activity.type} ${botSettings.activity.description}\"`)
console.log("=================== READY END ===================")
// refresh portals
functions.processes.refreshPortals(client);
})
fs.readdir("./commands/", (err, files) => {
if(err) {
console.log("ERROR READING ./commands/ PATH");
return;
};
// console.log(files);
console.log(`Found ${files.length} file(s)!`);
files.forEach((file) => {
if(!file.endsWith(".js")) return;
var cmd = require(`./commands/${file}`);
var cmdName = cmd.help.name;
client.commands.set(cmdName, cmd);
console.log(`Loaded the \'${cmdName}\' command!`)
})
})
fs.readdir("./events/", (err, files) => {
if(err) {
console.log("ERROR READING ./events/ PATH");
return;
};
console.log(`\nFound ${files.length} event(s)!`)
files.forEach((file) => {
if(!file.endsWith(".js")) return;
var event = require(`./events/${file}`);
client.on(event.help.name, event.run.bind(null, client));
console.log(`Setup response for the \'${event.help.name}\' event!`)
})
})
client.login(botSettings.token);