-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.ts
204 lines (183 loc) · 5.68 KB
/
db.ts
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
import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
import { MongoClient, ObjectId } from "https://deno.land/x/[email protected]/mod.ts";
await load({
export: true,
});
export const mongoClient = new MongoClient();
await mongoClient.connect(Deno.env.get("DB_URL")!);
const db = mongoClient.database("one_bbn");
export async function findUser(discordId: string) {
const user = await db.collection("users").findOne({
authentication: {
$elemMatch: {
id: discordId,
type: "oauth",
provider: "discord"
}
},
"profile.verified.email": true
})
if (!user) return null;
return user._id;
}
export async function getCoins(discordId: string) {
const user = await findUser(discordId);
if (!user) return null;
const access = await db.collection("@bbn/hosting/access").findOne({
owner: user
});
if (!access) return null;
return access.coins;
}
export async function setCoins(discordId: string, coins: number) {
// check if user exists
const user = await findUser(discordId);
if (!user) return null;
// update user
return await db.collection("@bbn/hosting/access").updateOne({
owner: user
}, {
$set: {
coins
}
});
}
export async function addCoins(discordId: string, coins: number) {
// check if user exists
const user = await findUser(discordId);
if (!user) return null;
// update user
return await db.collection("@bbn/hosting/access").updateOne({
owner: user
}, {
$inc: {
coins
}
});
}
export async function removeCoins(discordId: string, coins: number) {
// check if user exists
const user = await findUser(discordId);
if (!user) return null;
// update user
return await db.collection("@bbn/hosting/access").updateOne({
owner: user
}, {
$inc: {
coins: -coins
}
});
}
export async function getLastDaily(discordId: string) {
// check if user exists
const user = await findUser(discordId);
if (!user) return null;
const access = await db.collection("@bbn/hosting/access").findOne({
owner: user
});
if (!access) return null;
return access.lastDaily;
}
export async function setLastDaily(discordId: string, lastDaily: number) {
const user = await findUser(discordId);
if (!user) return null;
return await db.collection("@bbn/hosting/access").updateOne({
owner: user
}, {
$set: {
lastDaily
}
});
}
export async function getServerURLs(discordId: string) {
const user = await findUser(discordId);
if (!user) return null;
const servers = await db.collection("@bbn/hosting/servers").find({
user
}).toArray();
return servers.map(server => server.identifier ? `https://panel.bbn.one/server/${server.identifier}` : `https://bbn.one/hosting?path=servers/${server._id}/`);
}
export async function lastLogin(discordId: string) {
const user = await findUser(discordId);
if (!user) return null;
const userevent = await db.collection("user-events").findOne({
type: "auth",
userId: user
}, {
sort: {
_id: -1
}
});
if (!userevent) return null;
const location = await fetch(`https://ipinfo.io/${userevent.ip}/json`).then(res => res.json());
return [ {
platform: userevent.source.platform,
platformVersion: userevent.source.platformVersion,
legacyUserAgent: userevent.source.legacyUserAgent,
}, location.bogon ? undefined : `${String.fromCodePoint(...(location.country as string).toUpperCase().split('').map(char => 127397 + char.charCodeAt(0)))} ${location.city} (${location.timezone})`, location.timezone ];
}
export async function saveTranscript(transcript: any) {
await db.collection("@bbn/bot/transcripts").insertOne(transcript);
}
export async function addPartner(member: ObjectId, cpu: number, memory: number, disk: number, slots: number, invite: string) {
await db.collection("@bbn/bot/partners").insertOne({
owner: member,
cpu,
memory,
disk,
slots,
invite,
lastinvite: Date.now()
});
await db.collection("@bbn/hosting/access").updateOne({
owner: member
}, {
$inc: {
"limits.memory": memory,
"limits.disk": disk,
"limits.cpu": cpu,
"limits.slots": slots
}
});
}
export async function removePartner(member: ObjectId) {
const partner = await db.collection("@bbn/bot/partners").findOne({
owner: member
});
if (!partner) return null;
db.collection("@bbn/hosting/access").updateOne({
owner: member
}, {
$inc: {
"limits.memory": -partner.memory,
"limits.disk": -partner.disk,
"limits.cpu": -partner.cpu,
"limits.slots": -partner.slots
}
});
db.collection("@bbn/bot/partners").deleteOne({
owner: member
});
}
export function getPartnerFromInvite(invite: string) {
return db.collection("@bbn/bot/partners").findOne({
invite
});
}
export function getMemberFromBBNId(bbnid: ObjectId) {
return db.collection("users").findOne({
_id: bbnid
}).then(user => user?.authentication.find((auth: any) => auth.type === "oauth" && auth.provider === "discord")?.id);
}
export function updateLastInvite(member: ObjectId) {
db.collection("@bbn/bot/partners").updateOne({
owner: member
}, {
$set: {
lastinvite: Date.now()
}
})
}
export function getPartners() {
return db.collection("@bbn/bot/partners").find().toArray();
}