-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (71 loc) · 2.7 KB
/
Copy pathapp.js
File metadata and controls
80 lines (71 loc) · 2.7 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
const { Client, Intents } = require("discord.js");
const mongoose = require("mongoose");
const Database = require("./db/Database");
const db = require("./db");
const Config = require("./config");
const setupScript = require("./scripts/setupScript");
const activityScript = require("./scripts/activityScript");
const INTENTS = [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGES,
];
const PARTIALS = ["CHANNEL"]; // DM channels are not automatically cached, this is a workaround to read DMs off the bat
mongoose
.connect(Config.database, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("Connected to database.");
// Make sure the owner always has rank 4
db.User.findOne({ disID: Config.owner }, { grank: 1 }, (err, data) => {
if (err) console.log(err);
else {
if (data) {
if (data.grank < 4) {
db.User.updateOne(
{ disID: Config.owner },
{ $set: { grank: 4 } },
console.error
);
console.log(`Owner db entry exists, rank had to be set to 4`);
} else console.log(`Owner db entry exists and rank is good`);
} else {
new db.User({
disID: Config.owner,
grank: 4,
}).save(console.error);
console.log(`Owner db entry had to be created`);
}
}
});
// Clear out the temporary data
db.TempUser.deleteMany().exec((err) => {
if (err) console.error(err);
});
const bot = new Client({ intents: INTENTS, partials: PARTIALS });
bot.login(Config.token);
bot.once("ready", () => {
console.log(
`Connected Jolty to ${bot.guilds.cache.size} servers successfully ...`
);
bot.user.setActivity(`${Config.prefix}help`);
let _settings = Database.get("settings");
_settings.currentDate = new Date();
Database.update("settings", _settings);
});
bot.on("messageCreate", async (msg) => {
let notCommand = await setupScript.try(msg); // Had to make this await because it would return a promise instead of a boolean
if (notCommand && msg.guildId !== null) {
activityScript.check(msg); // If it isn't a command and is not a direct message, run activity script
}
});
bot.on("disconnect", (err) => {
console.log("There was an error with Jolty >>>");
if (err.type == "close" && err.wasClean) {
console.log("Suicided like you told me, sir.");
} else console.log(err);
});
// Handling error event since the bot has previously crashed due to the lack of it
bot.on("error", console.error);
})
.catch(console.error);