-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
88 lines (76 loc) · 2.61 KB
/
data.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
const { MongoClient } = require("mongodb");
const { mongodb } = require("./config.json");
const client = new MongoClient(mongodb);
const db = client.db("lmsdb");
const configCollection = db.collection("settings");
const classesCollection = db.collection("classes");
const assignmentsCollection = db.collection("assignments");
let getConfig = async (guildId) => {
return await configCollection.findOne({ _id: guildId });
};
let addToConfig = async (guildId, type, config, value) => {
const res = await configCollection.updateOne(
{ _id: guildId },
{ $set: { [`${type}.${config}`]: value } },
{ upsert: true }
);
};
let hasPerm = async (interaction, perms) => {
try {
// if the person who ran the command is the server owner allow
if (interaction.guild.ownerId == interaction.user.id) {
return true;
}
// check if owner is in required perms. if not check if the user have required perms
if (!("owner" in perms)) {
const conf = await configCollection.findOne({ _id: interaction.guild.id }, { roles: 1, _id: 0 });
const roles = conf["roles"];
const memberRoles = interaction.member.roles.cache;
// loops through permissions and checks if user has them
for (perm of perms) {
if (memberRoles.has(roles[perm])) {
return true;
}
}
}
return false;
} catch (e) {
throw e;
}
};
let createClass = async (guildId, categoryId, voiceId, textId, roleId) => {
await classesCollection.insertOne({ _id: categoryId, guild: guildId, voice: voiceId, text: textId, role: roleId });
};
let getClass = async (categoryId, projection) => {
return await classesCollection.findOne({ _id: categoryId }, projection);
};
let deleteClass = async (categoryId) => {
return await classesCollection.deleteMany({ _id: categoryId });
};
let createAssignment = async (assignmentId) => {
return await assignmentsCollection.insertOne({ _id: assignmentId });
};
let deleteAssignment = async (assignmentId) => {
return await assignmentsCollection.deleteOne({ _id: assignmentId });
};
let doesAssignmentExists = async (assignmentId) => {
let assignment = await assignmentsCollection.findOne({ _id: assignmentId });
try {
if (assignment._id === assignmentId) {
return true;
}
} catch {
return false;
}
};
module.exports = {
addToConfig,
getConfig,
hasPerm,
createClass,
getClass,
deleteClass,
createAssignment,
deleteAssignment,
doesAssignmentExists,
};