@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.
- package/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +16 -0
- package/package.json +46 -0
- package/src/index.ts +30 -0
- package/src/managers/bet/GuildBetManager.ts +117 -0
- package/src/managers/betuser/GuildBetUserManager.ts +103 -0
- package/src/managers/channel/ChannelManager.ts +168 -0
- package/src/managers/groupedchannel/GroupedChannelManager.ts +117 -0
- package/src/managers/guild/GuildManager.ts +84 -0
- package/src/managers/match/GuildMatchManager.ts +115 -0
- package/src/managers/mediator/GuildMediatorManager.ts +111 -0
- package/src/managers/messages/MessagesManager.ts +95 -0
- package/src/managers/permission/GuildPermissionManager.ts +88 -0
- package/src/managers/product/GuildProductManager.ts +115 -0
- package/src/managers/ticket/GuildTicketManager.ts +112 -0
- package/src/managers/user/GuildUserManager.ts +116 -0
- package/src/rest/APIEndpoints.ts +11 -0
- package/src/rest/REST.ts +126 -0
- package/src/rest/Routes.ts +143 -0
- package/src/structures/Collection.ts +85 -0
- package/src/structures/bet/GuildBet.ts +332 -0
- package/src/structures/betuser/GuildBetUser.ts +247 -0
- package/src/structures/channel/Channel.ts +78 -0
- package/src/structures/groupedchannel/GroupedChannel.ts +165 -0
- package/src/structures/guild/Guild.ts +302 -0
- package/src/structures/match/GuildMatch.ts +385 -0
- package/src/structures/mediator/GuildMediator.ts +175 -0
- package/src/structures/product/GuildProduct.ts +217 -0
- package/src/structures/shop/GuildShop.ts +98 -0
- package/src/structures/ticket/GuildTicket.ts +227 -0
- package/src/structures/user/GuildUser.ts +248 -0
- package/src/types/api/APIBaseChannel.ts +13 -0
- package/src/types/api/APIBetChannel.ts +13 -0
- package/src/types/api/APIBetMessage.ts +13 -0
- package/src/types/api/APIGuild.ts +123 -0
- package/src/types/api/APIGuildBet.ts +69 -0
- package/src/types/api/APIGuildBetUser.ts +46 -0
- package/src/types/api/APIGuildChannel.ts +13 -0
- package/src/types/api/APIGuildEmoji.ts +16 -0
- package/src/types/api/APIGuildGroupedChannel.ts +13 -0
- package/src/types/api/APIGuildMatch.ts +67 -0
- package/src/types/api/APIGuildMediator.ts +18 -0
- package/src/types/api/APIGuildMessage.ts +13 -0
- package/src/types/api/APIGuildPermissions.ts +7 -0
- package/src/types/api/APIGuildRole.ts +13 -0
- package/src/types/api/APIGuildShop.ts +15 -0
- package/src/types/api/APIGuildTicket.ts +36 -0
- package/src/types/api/APIGuildUser.ts +45 -0
- package/src/types/api/APIMessage.ts +16 -0
- package/src/types/api/APIPlayer.ts +13 -0
- package/src/types/api/APIProduct.ts +27 -0
- package/src/types/api/index.ts +180 -0
- package/src/types/index.ts +24 -0
- package/src/utils/Assertion.ts +56 -0
- package/tests/index.ts +86 -0
- package/tsconfig.json +15 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 duque das edits
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# A SDK for my API
|
|
2
|
+
|
|
3
|
+
> You can use this package as base for your own sdk
|
|
4
|
+
|
|
5
|
+
> __Version__: [2.5.3](https://www.npmjs.com/package/@duque.edits/rest)
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
- Rest Client
|
|
9
|
+
- Guild Manager
|
|
10
|
+
|
|
11
|
+
## To-Do
|
|
12
|
+
- Bet User Manager
|
|
13
|
+
- User Manager
|
|
14
|
+
- Bet Manager
|
|
15
|
+
- Match Manager
|
|
16
|
+
- Ticket Manager
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@duque.edits/sdk",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"description": "An SDK that facilitates interaction with my API.",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"REST",
|
|
10
|
+
"API",
|
|
11
|
+
"SDK",
|
|
12
|
+
"DuqueEdits"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "ts-node tests/index.ts"
|
|
16
|
+
},
|
|
17
|
+
"author": "Duque Edits jedits077@gmail.com",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/duque7x/sdk-api.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/duque7x/sdk-api/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/duque7x/sdk-api#readme",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"undici": "^7.10.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@eslint/js": "^9.29.0",
|
|
32
|
+
"@types/node": "^24.0.14",
|
|
33
|
+
"chalk": "^4.1.2",
|
|
34
|
+
"dotenv": "^16.6.1",
|
|
35
|
+
"eslint": "^9.29.0",
|
|
36
|
+
"globals": "^16.2.0",
|
|
37
|
+
"ts-node": "^10.9.2",
|
|
38
|
+
"typescript": "^5.8.3"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=14"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export * from "./managers/bet/GuildBetManager";
|
|
2
|
+
export * from "./managers/betuser/GuildBetUserManager";
|
|
3
|
+
export * from "./managers/channel/ChannelManager";
|
|
4
|
+
export * from "./managers/groupedchannel/GroupedChannelManager";
|
|
5
|
+
export * from "./managers/guild/GuildManager";
|
|
6
|
+
export * from "./managers/match/GuildMatchManager";
|
|
7
|
+
export * from "./managers/mediator/GuildMediatorManager";
|
|
8
|
+
export * from "./managers/messages/MessagesManager";
|
|
9
|
+
export * from "./managers/permission/GuildPermissionManager";
|
|
10
|
+
export * from "./managers/product/GuildProductManager";
|
|
11
|
+
export * from "./managers/ticket/GuildTicketManager";
|
|
12
|
+
export * from "./managers/user/GuildUserManager";
|
|
13
|
+
|
|
14
|
+
export * from "./structures/bet/GuildBet";
|
|
15
|
+
export * from "./structures/betuser/GuildBetUser";
|
|
16
|
+
export * from "./structures/channel/Channel";
|
|
17
|
+
export * from "./structures/groupedchannel/GroupedChannel";
|
|
18
|
+
export * from "./structures/guild/Guild";
|
|
19
|
+
export * from "./structures/match/GuildMatch";
|
|
20
|
+
export * from "./structures/mediator/GuildMediator";
|
|
21
|
+
export * from "./structures/product/GuildProduct";
|
|
22
|
+
export * from "./structures/shop/GuildShop";
|
|
23
|
+
export * from "./structures/ticket/GuildTicket";
|
|
24
|
+
export * from "./structures/user/GuildUser";
|
|
25
|
+
export * from "./structures/Collection";
|
|
26
|
+
|
|
27
|
+
export * from "./rest/REST";
|
|
28
|
+
export * from "./rest/Routes";
|
|
29
|
+
|
|
30
|
+
export * from "./types/index";
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { REST } from "../../rest/REST";
|
|
2
|
+
import { Routes } from "../../rest/Routes";
|
|
3
|
+
import { GuildBet } from "../../structures/bet/GuildBet";
|
|
4
|
+
import { Collection } from "../../structures/Collection";
|
|
5
|
+
import { Guild } from "../../structures/guild/Guild";
|
|
6
|
+
import { Optional } from "../../types/api";
|
|
7
|
+
import { APIGuildBet } from "../../types/api/APIGuildBet";
|
|
8
|
+
import { Assertion } from "../../utils/Assertion";
|
|
9
|
+
|
|
10
|
+
export class GuildBetManager {
|
|
11
|
+
/** A cache of users */
|
|
12
|
+
cache: Collection<string, GuildBet>;
|
|
13
|
+
|
|
14
|
+
/** The rest client */
|
|
15
|
+
rest: REST;
|
|
16
|
+
|
|
17
|
+
/** GuildBet bet guild */
|
|
18
|
+
guild: Guild;
|
|
19
|
+
/**
|
|
20
|
+
* Manage users with the given client
|
|
21
|
+
* @param users An array of users
|
|
22
|
+
* @param rest The rest client
|
|
23
|
+
*/
|
|
24
|
+
constructor(guild: Guild, rest: REST) {
|
|
25
|
+
this.guild = guild;
|
|
26
|
+
this.rest = rest;
|
|
27
|
+
|
|
28
|
+
this.cache = new Collection<string, GuildBet>("bets");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async create(payload: Optional<APIGuildBet>): Promise<GuildBet> {
|
|
32
|
+
Assertion.assertObject(payload);
|
|
33
|
+
|
|
34
|
+
const route = Routes.guilds.bets.create(this.guild.id);
|
|
35
|
+
const response = await this.rest.request<APIGuildBet, typeof payload>({
|
|
36
|
+
method: "POST",
|
|
37
|
+
url: route,
|
|
38
|
+
});
|
|
39
|
+
const bet = this.set(response);
|
|
40
|
+
return bet;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Fetch a bet
|
|
45
|
+
* @param id Id of the bet to fetch
|
|
46
|
+
* @returns APIBetUser
|
|
47
|
+
*/
|
|
48
|
+
async fetch(id: string) {
|
|
49
|
+
const route = Routes.guilds.bets.get(this.guild.id, id);
|
|
50
|
+
const response = await this.rest.request<APIGuildBet, {}>({
|
|
51
|
+
method: "get",
|
|
52
|
+
url: route,
|
|
53
|
+
});
|
|
54
|
+
const bet = new GuildBet(response, this.guild, this, this.rest);
|
|
55
|
+
this.cache.set(bet._id, bet);
|
|
56
|
+
this.rest.bets.set(bet._id, bet);
|
|
57
|
+
|
|
58
|
+
return bet;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async fetchAll() {
|
|
62
|
+
const route = Routes.guilds.bets.getAll(this.guild.id);
|
|
63
|
+
const response = await this.rest.request<APIGuildBet[], {}>({
|
|
64
|
+
method: "get",
|
|
65
|
+
url: route,
|
|
66
|
+
});
|
|
67
|
+
if (Array.isArray(response) && response.length === 0) {
|
|
68
|
+
this.cache.clear();
|
|
69
|
+
return this.cache;
|
|
70
|
+
}
|
|
71
|
+
for (let betData of response) {
|
|
72
|
+
const bet = new GuildBet(betData, this.guild, this, this.rest);
|
|
73
|
+
this.cache.set(bet._id, bet);
|
|
74
|
+
}
|
|
75
|
+
return this.cache;
|
|
76
|
+
}
|
|
77
|
+
set(data: APIGuildBet): GuildBet {
|
|
78
|
+
if (!data._id) return;
|
|
79
|
+
const bet = new GuildBet(data, this.guild, this, this.rest);
|
|
80
|
+
this.cache.set(bet._id, bet);
|
|
81
|
+
this.rest.bets.set(bet._id, bet);
|
|
82
|
+
|
|
83
|
+
return bet;
|
|
84
|
+
}
|
|
85
|
+
setAll(data: APIGuildBet[]) {
|
|
86
|
+
if (!data) return this.cache;
|
|
87
|
+
for (let bet of data) this.set(bet);
|
|
88
|
+
return this.cache;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
async delete(id: string) {
|
|
92
|
+
Assertion.assertString(id);
|
|
93
|
+
|
|
94
|
+
const route = Routes.guilds.bets.delete(id, this.guild.id);
|
|
95
|
+
const bet = this.cache.get(id);
|
|
96
|
+
this.rest.emit("betDelete", bet);
|
|
97
|
+
|
|
98
|
+
await this.rest.request<boolean, {}>({
|
|
99
|
+
method: "DELETE",
|
|
100
|
+
url: route,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
this.cache.delete(id);
|
|
104
|
+
return this.cache;
|
|
105
|
+
}
|
|
106
|
+
async deleteAll() {
|
|
107
|
+
const route = Routes.guilds.bets.deleteAll(this.guild.id);
|
|
108
|
+
this.rest.emit("betsDelete", this.cache);
|
|
109
|
+
|
|
110
|
+
const value = await this.rest.request<boolean, {}>({
|
|
111
|
+
method: "DELETE",
|
|
112
|
+
url: route,
|
|
113
|
+
});
|
|
114
|
+
this.cache.clear();
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { REST } from "../../rest/REST";
|
|
2
|
+
import { Routes } from "../../rest/Routes";
|
|
3
|
+
import { GuildBetUser } from "../../structures/betuser/GuildBetUser";
|
|
4
|
+
import { Collection } from "../../structures/Collection";
|
|
5
|
+
import { Guild } from "../../structures/guild/Guild";
|
|
6
|
+
import { APIGuildBetUser } from "../../types/api/APIGuildBetUser";
|
|
7
|
+
import { Assertion } from "../../utils/Assertion";
|
|
8
|
+
|
|
9
|
+
export class GuildBetUserManager {
|
|
10
|
+
/** A cache of users */
|
|
11
|
+
cache: Collection<string, GuildBetUser>;
|
|
12
|
+
|
|
13
|
+
/** The rest client */
|
|
14
|
+
rest: REST;
|
|
15
|
+
|
|
16
|
+
/** Bet user guild */
|
|
17
|
+
guild: Guild;
|
|
18
|
+
/**
|
|
19
|
+
* Manage users with the given client
|
|
20
|
+
* @param users An array of users
|
|
21
|
+
* @param rest The rest client
|
|
22
|
+
*/
|
|
23
|
+
constructor(guild: Guild, rest: REST) {
|
|
24
|
+
this.guild = guild;
|
|
25
|
+
this.rest = rest;
|
|
26
|
+
|
|
27
|
+
this.cache = new Collection<string, GuildBetUser>("betUsers");
|
|
28
|
+
}
|
|
29
|
+
set(data: APIGuildBetUser): Collection<string, GuildBetUser> {
|
|
30
|
+
if (!data.id) return;
|
|
31
|
+
const user = new GuildBetUser(data, this, this.rest);
|
|
32
|
+
this.rest.betUsers.set(user.id, user);
|
|
33
|
+
return this.cache.set(user.id, user);
|
|
34
|
+
}
|
|
35
|
+
setAll(data: APIGuildBetUser[]): Collection<string, GuildBetUser> {
|
|
36
|
+
if (!data) return this.cache;
|
|
37
|
+
for (let user of data) this.set(user);
|
|
38
|
+
return this.cache;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Fetch a user
|
|
42
|
+
* @param id Id of the user to fetch
|
|
43
|
+
* @returns GuildBetUser
|
|
44
|
+
*/
|
|
45
|
+
async fetch(id: string, name: string): Promise<GuildBetUser> {
|
|
46
|
+
const route = Routes.guilds.betUsers.get(this.guild.id, id);
|
|
47
|
+
const response = await this.rest.request<APIGuildBetUser, { name: string }>(
|
|
48
|
+
{
|
|
49
|
+
method: "get",
|
|
50
|
+
url: route,
|
|
51
|
+
payload: { name },
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
if (!response) return this.cache.get(id);
|
|
55
|
+
|
|
56
|
+
const user = new GuildBetUser(response, this, this.rest);
|
|
57
|
+
this.cache.set(user?.id, user);
|
|
58
|
+
this.rest.betUsers.set(user?.id, user);
|
|
59
|
+
|
|
60
|
+
return user;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async fetchAll(): Promise<Collection<string, GuildBetUser>> {
|
|
64
|
+
const route = Routes.guilds.betUsers.getAll(this.guild.id);
|
|
65
|
+
const response = await this.rest.request<APIGuildBetUser[], {}>({
|
|
66
|
+
method: "get",
|
|
67
|
+
url: route,
|
|
68
|
+
});
|
|
69
|
+
if (Array.isArray(response) && response.length === 0) {
|
|
70
|
+
this.cache.clear();
|
|
71
|
+
return this.cache;
|
|
72
|
+
}
|
|
73
|
+
this.setAll(response);
|
|
74
|
+
return this.cache;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async delete(id: string) {
|
|
78
|
+
Assertion.assertString(id);
|
|
79
|
+
|
|
80
|
+
const route = Routes.guilds.betUsers.delete(id, this.guild.id);
|
|
81
|
+
const betUser = this.cache.get(id);
|
|
82
|
+
this.rest.emit("betDelete", betUser);
|
|
83
|
+
|
|
84
|
+
await this.rest.request<boolean, {}>({
|
|
85
|
+
method: "DELETE",
|
|
86
|
+
url: route,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
this.cache.delete(id);
|
|
90
|
+
return this.cache;
|
|
91
|
+
}
|
|
92
|
+
async deleteAll() {
|
|
93
|
+
const route = Routes.guilds.betUsers.deleteAll(this.guild.id);
|
|
94
|
+
this.rest.emit("betsDelete", this.cache);
|
|
95
|
+
|
|
96
|
+
const value = await this.rest.request<boolean, {}>({
|
|
97
|
+
method: "DELETE",
|
|
98
|
+
url: route,
|
|
99
|
+
});
|
|
100
|
+
this.cache.clear();
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { REST } from "../../rest/REST";
|
|
2
|
+
import { Route, Routes } from "../../rest/Routes";
|
|
3
|
+
import { GuildBet } from "../../structures/bet/GuildBet";
|
|
4
|
+
import { Channel } from "../../structures/channel/Channel";
|
|
5
|
+
import { Collection } from "../../structures/Collection";
|
|
6
|
+
import { Guild } from "../../structures/guild/Guild";
|
|
7
|
+
import { GuildMatch } from "../../structures/match/GuildMatch";
|
|
8
|
+
import { Optional } from "../../types/api";
|
|
9
|
+
import { APIBaseChannel } from "../../types/api/APIBaseChannel";
|
|
10
|
+
|
|
11
|
+
import { Assertion } from "../../utils/Assertion";
|
|
12
|
+
type Channels = Optional<APIBaseChannel>;
|
|
13
|
+
|
|
14
|
+
export class ChannelManager<Structure extends GuildBet | GuildMatch> {
|
|
15
|
+
/** A cache of bet channels */
|
|
16
|
+
cache: Collection<string, Channel<Structure>>;
|
|
17
|
+
|
|
18
|
+
/** The rest client */
|
|
19
|
+
rest: REST;
|
|
20
|
+
|
|
21
|
+
/** GuildBet user guild */
|
|
22
|
+
guild: Guild;
|
|
23
|
+
|
|
24
|
+
/** The bet */
|
|
25
|
+
structure: Structure;
|
|
26
|
+
|
|
27
|
+
/** Base Url */
|
|
28
|
+
baseUrl: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Manage channels of a bet
|
|
32
|
+
* @param guild The guild at hand
|
|
33
|
+
* @param bet The bet at hand
|
|
34
|
+
* @param rest The rest client
|
|
35
|
+
*/
|
|
36
|
+
constructor(guild: Guild, structure: Structure, rest: REST) {
|
|
37
|
+
this.structure = structure;
|
|
38
|
+
this.rest = rest;
|
|
39
|
+
this.guild = guild;
|
|
40
|
+
this.cache = new Collection<string, Channel<Structure>>("channels");
|
|
41
|
+
this.baseUrl = Routes.guilds[structure.key as "bets"].resource(guild.id, structure._id, "channels");
|
|
42
|
+
}
|
|
43
|
+
async create(id: string, type: string): Promise<Channel<Structure>> {
|
|
44
|
+
Assertion.assertString(id);
|
|
45
|
+
Assertion.assertString(type);
|
|
46
|
+
|
|
47
|
+
const route = this.baseUrl;
|
|
48
|
+
const payload = { id, type };
|
|
49
|
+
const response = await this.rest.request<APIBaseChannel, typeof payload>({
|
|
50
|
+
method: "POST",
|
|
51
|
+
url: route,
|
|
52
|
+
payload,
|
|
53
|
+
});
|
|
54
|
+
const channel = this.set(response);
|
|
55
|
+
|
|
56
|
+
this.rest.emit("channelCreate", channel);
|
|
57
|
+
return channel;
|
|
58
|
+
}
|
|
59
|
+
async createMany(...channels: Channels[]) {
|
|
60
|
+
Assertion.assertArray(channels);
|
|
61
|
+
const route = Routes.fields(this.baseUrl, "bulk");
|
|
62
|
+
const payload = { channels };
|
|
63
|
+
const response = await this.rest.request<APIBaseChannel[], typeof payload>({
|
|
64
|
+
method: "POST",
|
|
65
|
+
url: route,
|
|
66
|
+
payload,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.rest.emit("channelBulkCreate", response);
|
|
70
|
+
return this.setAll(response);
|
|
71
|
+
}
|
|
72
|
+
async deleteMany(...channels: Optional<APIBaseChannel>[]) {
|
|
73
|
+
Assertion.assertArray(channels);
|
|
74
|
+
|
|
75
|
+
const route = Routes.fields(this.baseUrl, "bulk");
|
|
76
|
+
const payload = { channels };
|
|
77
|
+
const response = await this.rest.request<APIBaseChannel[], typeof payload>({
|
|
78
|
+
method: "DELETE",
|
|
79
|
+
url: route,
|
|
80
|
+
payload,
|
|
81
|
+
});
|
|
82
|
+
const channel = this.setAll(response);
|
|
83
|
+
|
|
84
|
+
this.rest.emit("channelBulkDelete", channel);
|
|
85
|
+
return channel;
|
|
86
|
+
}
|
|
87
|
+
setAll(data: APIBaseChannel[]): Collection<string, Channel<Structure>> {
|
|
88
|
+
if (!data) return this.cache;
|
|
89
|
+
for (let channelData of data) this.set(channelData);
|
|
90
|
+
return this.cache;
|
|
91
|
+
}
|
|
92
|
+
set(data: APIBaseChannel): Channel<Structure> {
|
|
93
|
+
if (!data.type) return;
|
|
94
|
+
const channel = new Channel(
|
|
95
|
+
{
|
|
96
|
+
baseUrl: Routes.guilds[this.structure.key as "bets"].resource(this.guild.id, this.structure._id, "channels"),
|
|
97
|
+
data: data,
|
|
98
|
+
guild: this.guild,
|
|
99
|
+
manager: this,
|
|
100
|
+
},
|
|
101
|
+
this.rest
|
|
102
|
+
);
|
|
103
|
+
this.cache.set(data.type, channel);
|
|
104
|
+
return channel;
|
|
105
|
+
}
|
|
106
|
+
async update(type: string, payload: Optional<APIBaseChannel>): Promise<Channel<Structure>> {
|
|
107
|
+
Assertion.assertString(type);
|
|
108
|
+
Assertion.assertObject(payload);
|
|
109
|
+
|
|
110
|
+
const route = Routes.fields(this.baseUrl, type);
|
|
111
|
+
const response = await this.rest.request<APIBaseChannel, {}>({
|
|
112
|
+
method: "PATCH",
|
|
113
|
+
url: route,
|
|
114
|
+
payload,
|
|
115
|
+
});
|
|
116
|
+
const channel = this.set(response);
|
|
117
|
+
this.rest.emit("betUpdate", this.structure, this.structure);
|
|
118
|
+
return channel;
|
|
119
|
+
}
|
|
120
|
+
async fetch(type: string): Promise<Channel<Structure>> {
|
|
121
|
+
Assertion.assertString(type);
|
|
122
|
+
|
|
123
|
+
const route = Routes.fields(this.baseUrl, type);
|
|
124
|
+
const response = await this.rest.request<APIBaseChannel, {}>({
|
|
125
|
+
method: "GET",
|
|
126
|
+
url: route,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const channel = this.set(response);
|
|
130
|
+
this.cache.set(channel.type, channel);
|
|
131
|
+
|
|
132
|
+
return channel;
|
|
133
|
+
}
|
|
134
|
+
async fetchAll() {
|
|
135
|
+
const response = await this.rest.request<APIBaseChannel[], {}>({
|
|
136
|
+
method: "GET",
|
|
137
|
+
url: this.baseUrl,
|
|
138
|
+
});
|
|
139
|
+
if (Array.isArray(response) && response.length === 0) {
|
|
140
|
+
this.cache.clear();
|
|
141
|
+
return this.cache;
|
|
142
|
+
}
|
|
143
|
+
for (let channelData of response) this.set(channelData);
|
|
144
|
+
return this.cache;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async delete(type: string): Promise<Collection<string, Channel<Structure>>> {
|
|
148
|
+
Assertion.assertString(type);
|
|
149
|
+
|
|
150
|
+
const route = this.baseUrl;
|
|
151
|
+
const response = await this.rest.request<APIBaseChannel[], {}>({
|
|
152
|
+
method: "DELETE",
|
|
153
|
+
url: Routes.fields(route, type),
|
|
154
|
+
});
|
|
155
|
+
this.cache.delete(type);
|
|
156
|
+
return this.cache;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async deleteAll(): Promise<boolean> {
|
|
160
|
+
const route = this.baseUrl;
|
|
161
|
+
const value = await this.rest.request<boolean, {}>({
|
|
162
|
+
method: "DELETE",
|
|
163
|
+
url: route,
|
|
164
|
+
});
|
|
165
|
+
this.cache.clear();
|
|
166
|
+
return value;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { REST } from "../../rest/REST";
|
|
2
|
+
import { Routes } from "../../rest/Routes";
|
|
3
|
+
import { GroupedChannel } from "../../structures/groupedchannel/GroupedChannel";
|
|
4
|
+
import { Collection } from "../../structures/Collection";
|
|
5
|
+
import { Guild } from "../../structures/guild/Guild";
|
|
6
|
+
import { Optional } from "../../types/api";
|
|
7
|
+
import { APIGuildGroupedChannel } from "../../types/api/APIGuildGroupedChannel";
|
|
8
|
+
import { Assertion } from "../../utils/Assertion";
|
|
9
|
+
|
|
10
|
+
export class GroupedChannelManager {
|
|
11
|
+
/** A cache of channels */
|
|
12
|
+
cache: Collection<string, GroupedChannel>;
|
|
13
|
+
|
|
14
|
+
/** Key */
|
|
15
|
+
key: string;
|
|
16
|
+
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
|
|
19
|
+
/** The rest client */
|
|
20
|
+
rest: REST;
|
|
21
|
+
|
|
22
|
+
/** GroupedChannel groupedChannel guild */
|
|
23
|
+
guild: Guild;
|
|
24
|
+
/**
|
|
25
|
+
* Manage channels with the given client
|
|
26
|
+
* @param channels An array of channels
|
|
27
|
+
* @param rest The rest client
|
|
28
|
+
*/
|
|
29
|
+
constructor(guild: Guild, key: string, rest: REST) {
|
|
30
|
+
this.key = key;
|
|
31
|
+
this.baseUrl = Routes.fields(Routes.guilds.get(guild?.id), key);
|
|
32
|
+
|
|
33
|
+
this.cache = new Collection<string, GroupedChannel>("groupedChannels");
|
|
34
|
+
|
|
35
|
+
this.guild = guild;
|
|
36
|
+
this.rest = rest;
|
|
37
|
+
}
|
|
38
|
+
async delete(type: string) {
|
|
39
|
+
const route = Routes.fields(this.baseUrl, type);
|
|
40
|
+
const response = await this.rest.request<boolean, {}>({
|
|
41
|
+
method: "delete",
|
|
42
|
+
url: route,
|
|
43
|
+
});
|
|
44
|
+
this.cache.delete(type);
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
47
|
+
async create(
|
|
48
|
+
payload: Optional<APIGuildGroupedChannel>
|
|
49
|
+
): Promise<GroupedChannel> {
|
|
50
|
+
Assertion.assertObject(payload);
|
|
51
|
+
|
|
52
|
+
const route = this.baseUrl;
|
|
53
|
+
const response = await this.rest.request<
|
|
54
|
+
APIGuildGroupedChannel,
|
|
55
|
+
typeof payload
|
|
56
|
+
>({
|
|
57
|
+
method: "POST",
|
|
58
|
+
url: route,
|
|
59
|
+
payload,
|
|
60
|
+
});
|
|
61
|
+
const groupedChannel = this.set(response);
|
|
62
|
+
return groupedChannel;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Fetch a groupedChannel
|
|
67
|
+
* @param id Id of the groupedChannel to fetch
|
|
68
|
+
* @returns APIgroupedChannelUser
|
|
69
|
+
*/
|
|
70
|
+
async fetch(type: string) {
|
|
71
|
+
const route = Routes.fields(this.baseUrl, type);
|
|
72
|
+
const response = await this.rest.request<APIGuildGroupedChannel, {}>({
|
|
73
|
+
method: "get",
|
|
74
|
+
url: route,
|
|
75
|
+
});
|
|
76
|
+
const channel = this.set(response);
|
|
77
|
+
this.cache.set(channel.type, channel);
|
|
78
|
+
|
|
79
|
+
return channel;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async fetchAll() {
|
|
83
|
+
const route = this.baseUrl;
|
|
84
|
+
const response = await this.rest.request<APIGuildGroupedChannel[], {}>({
|
|
85
|
+
method: "get",
|
|
86
|
+
url: route,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
for (let groupedChannelData of response) {
|
|
90
|
+
const groupedChannel = new GroupedChannel(
|
|
91
|
+
groupedChannelData,
|
|
92
|
+
this.guild,
|
|
93
|
+
this,
|
|
94
|
+
this.rest
|
|
95
|
+
);
|
|
96
|
+
this.cache.set(groupedChannel.type, groupedChannel);
|
|
97
|
+
}
|
|
98
|
+
return this.cache;
|
|
99
|
+
}
|
|
100
|
+
setAll(data: APIGuildGroupedChannel[]) {
|
|
101
|
+
if (!data) return this.cache;
|
|
102
|
+
for (let groupedChannel of data) this.set(groupedChannel);
|
|
103
|
+
return this.cache;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
set(data: APIGuildGroupedChannel): GroupedChannel {
|
|
107
|
+
if (!data?.type) return;
|
|
108
|
+
const groupedChannel = new GroupedChannel(
|
|
109
|
+
data,
|
|
110
|
+
this.guild,
|
|
111
|
+
this,
|
|
112
|
+
this.rest
|
|
113
|
+
);
|
|
114
|
+
this.cache.set(data.type, groupedChannel);
|
|
115
|
+
return groupedChannel;
|
|
116
|
+
}
|
|
117
|
+
}
|