This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (146 loc) · 4.58 KB
/
index.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
const mongoose = require("mongoose");
const permDB = require("./models/permissions.js");
var mongoURL;
class DiscordPermissions {
constructor(props = {}) {
if (!props.database) throw new TypeError("please specify a database")
this.setmongoURL(props.database)
}
/**
* @param {string} [url] - Your mongodb database url
*/
static async setmongoURL(url) {
if (!url) throw new TypeError("No database URL was specified.");
mongoURL = url;
return mongoose.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
}
/**
* @param {string} [userId] - ID of the discord user.
* @param {string} [guildID] - Discord guild id.
*/
static async createUser(userID, guildID) {
if (!userID) throw new TypeError("No user id was specified.");
if (!guildID) throw new TypeError("No guild id was specified.");
const isUser = await permDB.findOne({
userID: userID,
guildID: guildID
});
if (isUser) return false;
const newUser = new permDB({
userID: userID,
guildID: guildID
});
await newUser.save().catch(e => console.log(`Error creating a user in the database: ${e}`));
return newUser;
}
/**
* @param {string} [userID] - ID of the discord user.
* @param {string} [guildID] - Discord guild id.
*/
static async deleteUser(userID, guildID) {
if (!userID) throw new TypeError("No user id was specified.");
if (!guildID) throw new TypeError("No guild id was specified.");
const user = await permDB.findOne({
userID: userID,
guildID: guildID
});
if (!user) return false;
await permDB.findOneAndDelete({
userID: userID,
guildID: guildID
}).catch(e => console.log(`Error deleting a user in the database: ${e}`));
return user;
}
/**
* @param {string} [userID] - Discord user id.
* @param {string} [guildID] - Discord guild id.
* @param {string} [permission] - The permission which you want add
*/
static async addPermission(userID, guildID, permission) {
if (!userID) throw new TypeError("No user id was specified.");
if (!guildID) throw new TypeError("No guild id was specified.");
if (!permission) throw new TypeError("No permission was specified.");
const user = await permDB.findOne({
userID: userID,
guildID: guildID
});
if (!user) {
const permArray = user.permissions || [];
permArray.push(permission)
const newUser = new permDB({
userID: userId,
guildID: guildId,
permission: permArray
});
await newUser.save().catch(e => console.log(`Error at saving a user in the database: ${e}`));
return newUser;
};
const permArray2 = user.permissions || [];
permArray2.push(permission)
await user.save().catch(e => console.log(` ${e}`));
return user.permissions;
}
/**
* @param {string} [userID] - Discord user id.
* @param {string} [guildID] - Discord guild id.
* @param {string} [permission] - The permission which you want remove
*/
static async removePermission(userID, guildID, permission) {
if (!userID) throw new TypeError("No user id was specified.");
if (!guildID) throw new TypeError("No guild id was specified.");
if (!permission) throw new TypeError("No permission was specified.");
return await permDB.updateOne({
userID,
guildID
}, {
$pull: {
'permissions': permission
}
}, {
upsert: true
}).catch(e => console.log(`${e}`));
}
/**
* @param {string} [userID] - Discord user id.
* @param {string} [guildID] - Discord guild id.
* @param {string} [permission] - The permission which you want remove
*/
static async hasPermission(userID, guildID, permission) {
if (!userID) throw new TypeError("No user id was specified.");
if (!guildID) throw new TypeError("No guild id was specified.");
if (!permission) throw new TypeError("No permission was specified.");
const user = await permDB.findOne({
userID: userID,
guildID: guildID
});
const permArray = user.permissions || [];
if (!user) {
const newUser = new permDB({
userID: userId,
guildID: guildId,
permission: permArray
});
await newUser.save().catch(e => console.log(`Error at saving a user in the database: ${e}`));
return newUser;
};
return await permArray.includes(permission);
}
/**
* @param {string} [userId] - Discord user id.
* @param {string} [guildId] - Discord guild id.
*/
static async fetchGuildUser(userId, guildId) {
if (!userId) throw new TypeError("An user id was not provided.");
if (!guildId) throw new TypeError("A guild id was not provided.");
const user = await permDB.findOne({
userID: userId,
guildID: guildId
});
if (!user) return false;
return user;
}
}
module.exports = DiscordPermissions;