@duque.edits/sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.gitattributes +2 -0
  2. package/LICENSE +21 -0
  3. package/README.md +16 -0
  4. package/package.json +46 -0
  5. package/src/index.ts +30 -0
  6. package/src/managers/bet/GuildBetManager.ts +117 -0
  7. package/src/managers/betuser/GuildBetUserManager.ts +103 -0
  8. package/src/managers/channel/ChannelManager.ts +168 -0
  9. package/src/managers/groupedchannel/GroupedChannelManager.ts +117 -0
  10. package/src/managers/guild/GuildManager.ts +84 -0
  11. package/src/managers/match/GuildMatchManager.ts +115 -0
  12. package/src/managers/mediator/GuildMediatorManager.ts +111 -0
  13. package/src/managers/messages/MessagesManager.ts +95 -0
  14. package/src/managers/permission/GuildPermissionManager.ts +88 -0
  15. package/src/managers/product/GuildProductManager.ts +115 -0
  16. package/src/managers/ticket/GuildTicketManager.ts +112 -0
  17. package/src/managers/user/GuildUserManager.ts +116 -0
  18. package/src/rest/APIEndpoints.ts +11 -0
  19. package/src/rest/REST.ts +126 -0
  20. package/src/rest/Routes.ts +143 -0
  21. package/src/structures/Collection.ts +85 -0
  22. package/src/structures/bet/GuildBet.ts +332 -0
  23. package/src/structures/betuser/GuildBetUser.ts +247 -0
  24. package/src/structures/channel/Channel.ts +78 -0
  25. package/src/structures/groupedchannel/GroupedChannel.ts +165 -0
  26. package/src/structures/guild/Guild.ts +302 -0
  27. package/src/structures/match/GuildMatch.ts +385 -0
  28. package/src/structures/mediator/GuildMediator.ts +175 -0
  29. package/src/structures/product/GuildProduct.ts +217 -0
  30. package/src/structures/shop/GuildShop.ts +98 -0
  31. package/src/structures/ticket/GuildTicket.ts +227 -0
  32. package/src/structures/user/GuildUser.ts +248 -0
  33. package/src/types/api/APIBaseChannel.ts +13 -0
  34. package/src/types/api/APIBetChannel.ts +13 -0
  35. package/src/types/api/APIBetMessage.ts +13 -0
  36. package/src/types/api/APIGuild.ts +123 -0
  37. package/src/types/api/APIGuildBet.ts +69 -0
  38. package/src/types/api/APIGuildBetUser.ts +46 -0
  39. package/src/types/api/APIGuildChannel.ts +13 -0
  40. package/src/types/api/APIGuildEmoji.ts +16 -0
  41. package/src/types/api/APIGuildGroupedChannel.ts +13 -0
  42. package/src/types/api/APIGuildMatch.ts +67 -0
  43. package/src/types/api/APIGuildMediator.ts +18 -0
  44. package/src/types/api/APIGuildMessage.ts +13 -0
  45. package/src/types/api/APIGuildPermissions.ts +7 -0
  46. package/src/types/api/APIGuildRole.ts +13 -0
  47. package/src/types/api/APIGuildShop.ts +15 -0
  48. package/src/types/api/APIGuildTicket.ts +36 -0
  49. package/src/types/api/APIGuildUser.ts +45 -0
  50. package/src/types/api/APIMessage.ts +16 -0
  51. package/src/types/api/APIPlayer.ts +13 -0
  52. package/src/types/api/APIProduct.ts +27 -0
  53. package/src/types/api/index.ts +180 -0
  54. package/src/types/index.ts +24 -0
  55. package/src/utils/Assertion.ts +56 -0
  56. package/tests/index.ts +86 -0
  57. package/tsconfig.json +15 -0
@@ -0,0 +1,385 @@
1
+ import { REST } from "../../rest/REST";
2
+ import { Routes } from "../../rest/Routes";
3
+ import { Guild } from "../guild/Guild";
4
+ import { APIGuildMatch } from "../../types/api/APIGuildMatch";
5
+ import { APIPlayer } from "../../types/api/APIPlayer";
6
+ import { APIBetChannel } from "../../types/api/APIBetChannel";
7
+ import { BaseMatchModes, BaseMatchStatus, Confirm, Optional } from "../../types/api";
8
+ import { Assertion } from "../../utils/Assertion";
9
+ import { ChannelManager } from "../../managers/channel/ChannelManager";
10
+ import { APIGuildMessage } from "../../types/api/APIGuildMessage";
11
+ import { GuildMatchManager } from "../../managers/match/GuildMatchManager";
12
+ import { MessagesManager } from "../../managers/messages/MessagesManager";
13
+
14
+ export class GuildMatch {
15
+ /** Match's type */
16
+ type: BaseMatchModes;
17
+
18
+ /** Match's status */
19
+ status: BaseMatchStatus;
20
+
21
+ /** Match's challenge */
22
+ challenge: boolean;
23
+
24
+ /** Match's players */
25
+ players: APIPlayer[];
26
+
27
+ /** Match's winners */
28
+ winners: APIPlayer[];
29
+
30
+ /** Match's losers */
31
+ losers: APIPlayer[];
32
+
33
+ /** Match;s messages */
34
+ messages: MessagesManager;
35
+
36
+ /** Matches channels */
37
+ channels: ChannelManager<GuildMatch>;
38
+
39
+ /** Match's maximum size */
40
+ maximumSize: number;
41
+
42
+ /** Match's kicked out */
43
+ kickedOut: APIPlayer[];
44
+
45
+ /** Match's team a */
46
+ teamA: APIPlayer[];
47
+
48
+ /** Match's team b */
49
+ teamB: APIPlayer[];
50
+
51
+ /** Match's confirmed */
52
+ confirmed: Confirm[];
53
+
54
+ /** Match's leaders */
55
+ leaders: APIPlayer[];
56
+
57
+ /** Match's mvp */
58
+ mvpId: string;
59
+
60
+ /** Match's creator id */
61
+ creatorId: string;
62
+
63
+ /** Match's room creator id */
64
+ roomCreatorId: string;
65
+
66
+ /** Creation Date */
67
+ createdAt: Date;
68
+
69
+ /** Updated Date */
70
+ updatedAt: Date;
71
+
72
+ /** Match's id */
73
+ _id: string;
74
+
75
+ manager: GuildMatchManager;
76
+
77
+ /** The given guild */
78
+ readonly guild: Guild;
79
+
80
+ /** The rest client */
81
+ readonly rest: REST;
82
+ readonly key: string;
83
+
84
+ /**
85
+ * GuildMatch match
86
+ * @param data The match's data
87
+ * @param guild The guild
88
+ * @param rest The rest client
89
+ */
90
+
91
+ constructor(data: APIGuildMatch, guild: Guild, manager: GuildMatchManager, rest: REST) {
92
+ this._id = data?._id;
93
+ this.manager = manager;
94
+
95
+ this.challenge = data?.challenge;
96
+ this.kickedOut = data?.kickedOut;
97
+ this.leaders = data?.leaders;
98
+ this.type = data?.type;
99
+ this.status = data?.status;
100
+ this.maximumSize = data?.maximumSize;
101
+ this.players = data?.players;
102
+ this.teamA = data?.teamA;
103
+ this.teamB = data?.teamB;
104
+ this.winners = data?.winners;
105
+ this.losers = data?.losers;
106
+ this.creatorId = data?.creatorId;
107
+ this.roomCreatorId = data?.roomCreatorId;
108
+ this.confirmed = data?.confirmed;
109
+ this.mvpId = data?.mvpId;
110
+
111
+ this.key = "matches";
112
+ this.channels = new ChannelManager<GuildMatch>(guild, this, rest);
113
+ this.messages = new MessagesManager(guild, this?._id, rest);
114
+
115
+ this.createdAt = data?.createdAt ? new Date(data?.createdAt) : new Date();
116
+ this.updatedAt = data?.updatedAt ? new Date(data?.updatedAt) : new Date();
117
+
118
+ this.guild = guild;
119
+ this.rest = rest;
120
+
121
+ this.channels.setAll(data?.channels);
122
+ this.messages.setAll(data?.messages);
123
+ }
124
+
125
+ /**
126
+ * Fetches the match
127
+ * @returns New Instance of the match
128
+ */
129
+ async fetch(): Promise<GuildMatch> {
130
+ const route = Routes.guilds.matches.get(this.guild.id, this._id);
131
+ const response = await this.rest.request<APIGuildMatch, {}>({
132
+ method: "get",
133
+ url: route,
134
+ });
135
+
136
+ const match = new GuildMatch(response, this.guild, this.manager, this.rest);
137
+ this.manager.cache.set(match._id, match);
138
+ return match;
139
+ }
140
+ async addMessage(id: string, type: string, content?: string) {
141
+ const response = await this.messages.create({
142
+ userId: id,
143
+ type: type as "img",
144
+ content,
145
+ });
146
+
147
+ this.manager.cache.set(this._id, this);
148
+ this.rest.matches.set(this._id, this);
149
+ return response;
150
+ }
151
+ async addConfirmed(type: string, id: string): Promise<Confirm> {
152
+ Assertion.assertString(type);
153
+ Assertion.assertString(id);
154
+
155
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "confirmed");
156
+ const payload = { type, id };
157
+
158
+ const response = await this.rest.request<Confirm[], typeof payload>({
159
+ method: "PATCH",
160
+ url: route,
161
+ payload,
162
+ });
163
+
164
+ this.rest.emit("matchUpdate", this);
165
+
166
+ this.updatedAt = new Date();
167
+ this.confirmed = response;
168
+ this.rest.matches.set(this._id, this);
169
+ this.manager.cache.set(this._id, this);
170
+ return this.confirmed.find((c) => c.type === type);
171
+ }
172
+ async setConfirmed(set: Confirm[]): Promise<GuildMatch> {
173
+ Assertion.assertObject(set);
174
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "confirmed");
175
+
176
+ const response = await this.rest.request<Confirm[], typeof set>({
177
+ method: "PATCH",
178
+ url: route,
179
+ payload: set,
180
+ });
181
+
182
+ this.confirmed = response;
183
+ this.rest.matches.set(this._id, this);
184
+ this.manager.cache.set(this._id, this);
185
+ return this;
186
+ }
187
+
188
+ async setStatus(status: BaseMatchStatus): Promise<GuildMatch> {
189
+ Assertion.assertString(status);
190
+
191
+ const payload = { set: status.toLowerCase() };
192
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "status");
193
+ const response = await this.rest.request<APIGuildMatch, typeof payload>({
194
+ method: "PATCH",
195
+ url: route,
196
+ payload,
197
+ });
198
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
199
+ this.status = response.status;
200
+ this.rest.matches.set(this._id, this);
201
+ this.manager.cache.set(this._id, this);
202
+ return this;
203
+ }
204
+ async setWinners(players: Optional<APIPlayer>[] | Optional<APIPlayer>): Promise<GuildMatch> {
205
+ if (!Array.isArray(players)) players = [players];
206
+
207
+ const payload = { winners: players };
208
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "winners");
209
+ const response = await this.rest.request<APIGuildMatch, typeof payload>({
210
+ method: "PATCH",
211
+ url: route,
212
+ payload,
213
+ });
214
+
215
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
216
+ this.winners = response.winners;
217
+ this.rest.matches.set(this._id, this);
218
+ this.manager.cache.set(this._id, this);
219
+ return this;
220
+ }
221
+ async setLoser(players: Optional<APIPlayer>[] | Optional<APIPlayer>): Promise<GuildMatch> {
222
+ if (!Array.isArray(players)) players = [players];
223
+
224
+ const payload = { losers: players };
225
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "losers");
226
+ const response = await this.rest.request<APIGuildMatch, {}>({
227
+ method: "PATCH",
228
+ url: route,
229
+ payload,
230
+ });
231
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
232
+ this.losers = response.losers;
233
+ this.rest.matches.set(this._id, this);
234
+ this.manager.cache.set(this._id, this);
235
+ return this;
236
+ }
237
+ async setMvp(userId: string): Promise<GuildMatch> {
238
+ Assertion.assertString(userId);
239
+
240
+ const payload = { id: userId };
241
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "mvp");
242
+ const response = await this.rest.request<APIGuildMatch, {}>({
243
+ method: "PATCH",
244
+ url: route,
245
+ payload,
246
+ });
247
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
248
+ this.mvpId = response.mvpId;
249
+ this.rest.matches.set(this._id, this);
250
+ this.manager.cache.set(this._id, this);
251
+ return this;
252
+ }
253
+ async setRoomCreatorId(userId: string): Promise<GuildMatch> {
254
+ Assertion.assertString(userId);
255
+
256
+ const payload = { id: userId };
257
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "roomCreatorId");
258
+ const response = await this.rest.request<APIGuildMatch, {}>({
259
+ method: "PATCH",
260
+ url: route,
261
+ payload,
262
+ });
263
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
264
+ this.roomCreatorId = response.roomCreatorId;
265
+ this.rest.matches.set(this._id, this);
266
+ this.manager.cache.set(this._id, this);
267
+ return this;
268
+ }
269
+ async kick(player: APIPlayer) {
270
+ const payload = { ...player };
271
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "kickout");
272
+ const response = await this.rest.request<APIGuildMatch, typeof payload>({
273
+ method: "PATCH",
274
+ url: route,
275
+ payload,
276
+ });
277
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
278
+ this.kickedOut = response.kickedOut;
279
+ this.rest.matches.set(this._id, this);
280
+ this.manager.cache.set(this._id, this);
281
+ return this;
282
+ }
283
+ async addChannel(id: string, type: string): Promise<GuildMatch> {
284
+ await this.channels.create(id, type);
285
+ this.rest.matches.set(this._id, this);
286
+ this.manager.cache.set(this._id, this);
287
+ return this;
288
+ }
289
+
290
+ async setChannels(channels: APIBetChannel[]): Promise<GuildMatch> {
291
+ Assertion.assertObject(channels);
292
+
293
+ const payload = { set: channels };
294
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "channels");
295
+ const response = await this.rest.request<APIGuildMatch, {}>({
296
+ method: "PATCH",
297
+ url: route,
298
+ payload,
299
+ });
300
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
301
+ this.channels.setAll(response.channels);
302
+ this.rest.matches.set(this._id, this);
303
+ this.manager.cache.set(this._id, this);
304
+ return this;
305
+ }
306
+
307
+ async addPlayer(id: string, name: string): Promise<GuildMatch> {
308
+ Assertion.assertString(id);
309
+ Assertion.assertString(name);
310
+
311
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "players");
312
+ const payload = { id, name };
313
+ const response = await this.rest.request<APIGuildMatch, typeof payload>({
314
+ method: "POST",
315
+ url: route,
316
+ payload,
317
+ });
318
+ console.log({ response });
319
+
320
+ this.players = response.players;
321
+ this.rest.matches.set(this._id, this);
322
+ this.manager.cache.set(this._id, this);
323
+ return this;
324
+ }
325
+ async removePlayer(id: string, name: string): Promise<GuildMatch> {
326
+ Assertion.assertString(id);
327
+ Assertion.assertString(name);
328
+
329
+ const route = Routes.guilds.matches.resource(this.guild.id, this._id, "players", id);
330
+ const payload = { id, name };
331
+ const response = await this.rest.request<APIGuildMatch, typeof payload>({
332
+ method: "DELETE",
333
+ url: route,
334
+ payload,
335
+ });
336
+
337
+ this.players = response.players;
338
+ this.rest.matches.set(this?._id, this);
339
+ this.manager.cache.set(this?._id, this);
340
+ return this;
341
+ }
342
+
343
+ async update(data: Optional<APIGuildMatch>): Promise<GuildMatch> {
344
+ const route = Routes.guilds.matches.get(this.guild.id, this._id);
345
+ const payload = data;
346
+
347
+ const response = await this.rest.request<APIGuildMatch, typeof payload>({
348
+ method: "patch",
349
+ url: route,
350
+ payload,
351
+ });
352
+ this.rest.emit("matchUpdate", this, new GuildMatch(response, this.guild, this.manager, this.rest));
353
+ for (const k in response) {
354
+ if (k === "id" || k == "createdAt") continue;
355
+ if (Object.hasOwn(this, k)) {
356
+ if (k === "channels") {
357
+ console.log({ chs: response["channels"] });
358
+ this.channels.setAll(response["channels"]);
359
+ console.log({ chs2: this.channels.cache });
360
+ continue;
361
+ }
362
+ if (k === "messages") {
363
+ this.messages.setAll(response["messages"]);
364
+ continue;
365
+ }
366
+
367
+ (this as any)[k] = response[k as keyof APIGuildMatch];
368
+ }
369
+ }
370
+
371
+ this.updatedAt = new Date();
372
+ this.manager.cache.set(this._id, this);
373
+ this.rest.matches.set(this._id, this);
374
+ return this;
375
+ }
376
+ toJSON() {
377
+ const json: Record<string, unknown> = {};
378
+ for (const [key, value] of Object.entries(this)) {
379
+ if (typeof value !== "function") {
380
+ json[key] = value;
381
+ }
382
+ }
383
+ return json;
384
+ }
385
+ }
@@ -0,0 +1,175 @@
1
+ import { REST } from "../../rest/REST";
2
+ import { Routes } from "../../rest/Routes";
3
+ import { Guild } from "../guild/Guild";
4
+ import { APIGuildMediator } from "../../types/api/APIGuildMediator";
5
+ import { Optional } from "../../types/api";
6
+ import { Assertion } from "../../utils/Assertion";
7
+ import { GuildMediatorManager } from "../../managers/mediator/GuildMediatorManager";
8
+
9
+ export class GuildMediator {
10
+ /** Mediator's id */
11
+ id: string;
12
+
13
+ /** Mediator's name */
14
+ name: string;
15
+
16
+ /** Mediator's links */
17
+ paymentLinks: string[];
18
+
19
+ /** Creation Date */
20
+ createdAt: Date;
21
+
22
+ /** Updated Date */
23
+ updatedAt: Date;
24
+
25
+ /** The given guild */
26
+ readonly guild: Guild;
27
+
28
+ /** The rest client */
29
+ readonly rest: REST;
30
+ readonly manager: GuildMediatorManager;
31
+
32
+ /**
33
+ * GuildMediator mediator
34
+ * @param data The mediator's data
35
+ * @param guild The guild
36
+ * @param rest The rest client
37
+ */
38
+ constructor(
39
+ data: APIGuildMediator,
40
+ guild: Guild,
41
+ manager: GuildMediatorManager,
42
+ rest: REST
43
+ ) {
44
+ this.id = data.id;
45
+ this.name = data.name;
46
+ this.paymentLinks = data.paymentLinks;
47
+
48
+ this.createdAt = data?.createdAt ? new Date(data?.createdAt) : new Date();
49
+ this.updatedAt = data?.updatedAt ? new Date(data?.updatedAt) : new Date();
50
+
51
+ this.guild = guild;
52
+ this.rest = rest;
53
+ this.manager = manager;
54
+ }
55
+ /** String representation of this mediator */
56
+ toString() {
57
+ return `<@${this.id}>`;
58
+ }
59
+ /**
60
+ * Fetches the mediator
61
+ * @returns New Instance of the mediator
62
+ */
63
+ async fetch() {
64
+ const route = Routes.guilds.mediators.get(this.guild.id, this.id);
65
+ const response = await this.rest.request<APIGuildMediator, {}>({
66
+ method: "get",
67
+ url: route,
68
+ });
69
+
70
+ const med = new GuildMediator(
71
+ response,
72
+ this.guild,
73
+ this.manager,
74
+ this.rest
75
+ );
76
+ this.manager.cache.set(med.id, med);
77
+ this.rest.mediators.set(med.id, med);
78
+ return med;
79
+ }
80
+
81
+ async update(data: Optional<APIGuildMediator>) {
82
+ const route = Routes.guilds.mediators.get(this.guild.id, this.id);
83
+ const payload = data;
84
+
85
+ const response = await this.rest.request<APIGuildMediator, typeof payload>({
86
+ method: "patch",
87
+ url: route,
88
+ payload,
89
+ });
90
+ const med = new GuildMediator(
91
+ response,
92
+ this.guild,
93
+ this.manager,
94
+ this.rest
95
+ );
96
+ this.rest.emit("mediatorUpdate", this);
97
+ for (const k in response) {
98
+ if (k === "id") continue;
99
+
100
+ if (Object.hasOwn(this, k)) {
101
+ (this as any)[k] = response[k as keyof APIGuildMediator];
102
+ }
103
+ }
104
+ this.manager.cache.set(med.id, med);
105
+ this.rest.emit("mediatorUpdate", this, med);
106
+ return this;
107
+ }
108
+
109
+ async delete() {
110
+ const route = Routes.guilds.mediators.delete(this.guild.id, this.id);
111
+ const response = await this.rest.request<boolean, {}>({
112
+ method: "DELETE",
113
+ url: route,
114
+ });
115
+
116
+ this.manager.cache.delete(this.id);
117
+ this.rest.mediators.delete(this.id);
118
+ this.rest.emit("mediatorUpdate", this);
119
+ return response;
120
+ }
121
+ async setLinks(link: string) {
122
+ Assertion.assertString(link);
123
+
124
+ const route = Routes.guilds.mediators.resource(
125
+ this.guild.id,
126
+ this.id,
127
+ "links"
128
+ );
129
+ const payload = { paymentLinks: [link], set: true };
130
+ const response = await this.rest.request<APIGuildMediator, {}>({
131
+ method: "PATCH",
132
+ url: route,
133
+ payload,
134
+ });
135
+
136
+ this.paymentLinks = response.paymentLinks;
137
+ this.manager.cache.set(this.id, this);
138
+ this.rest.mediators.set(this.id, this);
139
+ this.rest.emit("mediatorUpdate", this);
140
+ return this.paymentLinks;
141
+ }
142
+ async removeLink(link: string) {
143
+ Assertion.assertString(link);
144
+
145
+ const route = Routes.guilds.mediators.resource(
146
+ this.guild.id,
147
+ this.id,
148
+ "links"
149
+ );
150
+
151
+ const payload = {
152
+ paymentLinks: this.paymentLinks.filter((lm) => lm !== link),
153
+ };
154
+ const response = await this.rest.request<APIGuildMediator, {}>({
155
+ method: "PATCH",
156
+ url: route,
157
+ payload,
158
+ });
159
+
160
+ this.paymentLinks = response.paymentLinks;
161
+ this.manager.cache.set(this.id, this);
162
+ this.rest.mediators.set(this.id, this);
163
+ this.rest.emit("mediatorUpdate", this);
164
+ return this.paymentLinks;
165
+ }
166
+ toJSON() {
167
+ const json: Record<string, unknown> = {};
168
+ for (const [key, value] of Object.entries(this)) {
169
+ if (typeof value !== "function") {
170
+ json[key] = value;
171
+ }
172
+ }
173
+ return json;
174
+ }
175
+ }