@duque.edits/sdk 0.0.2 → 0.0.3
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/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duque.edits/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
|
+
"typings": "./dist/index.d.ts",
|
|
6
7
|
"type": "commonjs",
|
|
7
8
|
"description": "An SDK that facilitates interaction with my API.",
|
|
8
9
|
"keywords": [
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { REST } from "../../../rest/REST";
|
|
2
|
+
import { 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 { APIBaseChannel, Optional } from "../../../types";
|
|
9
|
+
import { Assertion } from "../../../utils/Assertion";
|
|
10
|
+
|
|
11
|
+
type Channels = Optional<APIBaseChannel>;
|
|
12
|
+
|
|
13
|
+
export class ChannelManager<Structure extends GuildBet | GuildMatch> {
|
|
14
|
+
/** A cache of bet channels */
|
|
15
|
+
cache: Collection<string, Channel<Structure>>;
|
|
16
|
+
|
|
17
|
+
/** The rest client */
|
|
18
|
+
rest: REST;
|
|
19
|
+
|
|
20
|
+
/** GuildBet user guild */
|
|
21
|
+
guild: Guild;
|
|
22
|
+
|
|
23
|
+
/** The bet */
|
|
24
|
+
structure: Structure;
|
|
25
|
+
|
|
26
|
+
/** Base Url */
|
|
27
|
+
baseUrl: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Manage channels of a bet
|
|
31
|
+
* @param guild The guild at hand
|
|
32
|
+
* @param bet The bet at hand
|
|
33
|
+
* @param rest The rest client
|
|
34
|
+
*/
|
|
35
|
+
constructor(guild: Guild, structure: Structure, rest: REST) {
|
|
36
|
+
this.structure = structure;
|
|
37
|
+
this.rest = rest;
|
|
38
|
+
this.guild = guild;
|
|
39
|
+
this.cache = new Collection<string, Channel<Structure>>("channels");
|
|
40
|
+
this.baseUrl = Routes.guilds[structure.key as "bets"].resource(guild.id, structure._id, "channels");
|
|
41
|
+
}
|
|
42
|
+
async create(id: string, type: string): Promise<Channel<Structure>> {
|
|
43
|
+
Assertion.assertString(id);
|
|
44
|
+
Assertion.assertString(type);
|
|
45
|
+
|
|
46
|
+
const route = this.baseUrl;
|
|
47
|
+
const payload = { id, type };
|
|
48
|
+
const response = await this.rest.request<APIBaseChannel, typeof payload>({
|
|
49
|
+
method: "POST",
|
|
50
|
+
url: route,
|
|
51
|
+
payload,
|
|
52
|
+
});
|
|
53
|
+
const channel = this.set(response);
|
|
54
|
+
|
|
55
|
+
this.rest.emit("channelCreate", channel);
|
|
56
|
+
return channel;
|
|
57
|
+
}
|
|
58
|
+
async createMany(...channels: Channels[]) {
|
|
59
|
+
Assertion.assertArray(channels);
|
|
60
|
+
const route = Routes.fields(this.baseUrl, "bulk");
|
|
61
|
+
const payload = { channels };
|
|
62
|
+
const response = await this.rest.request<APIBaseChannel[], typeof payload>({
|
|
63
|
+
method: "POST",
|
|
64
|
+
url: route,
|
|
65
|
+
payload,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
this.rest.emit("channelBulkCreate", response);
|
|
69
|
+
return this.setAll(response);
|
|
70
|
+
}
|
|
71
|
+
async deleteMany(...channels: Optional<APIBaseChannel>[]) {
|
|
72
|
+
Assertion.assertArray(channels);
|
|
73
|
+
|
|
74
|
+
const route = Routes.fields(this.baseUrl, "bulk");
|
|
75
|
+
const payload = { channels };
|
|
76
|
+
const response = await this.rest.request<APIBaseChannel[], typeof payload>({
|
|
77
|
+
method: "DELETE",
|
|
78
|
+
url: route,
|
|
79
|
+
payload,
|
|
80
|
+
});
|
|
81
|
+
const channel = this.setAll(response);
|
|
82
|
+
|
|
83
|
+
this.rest.emit("channelBulkDelete", channel);
|
|
84
|
+
return channel;
|
|
85
|
+
}
|
|
86
|
+
setAll(data: APIBaseChannel[]): Collection<string, Channel<Structure>> {
|
|
87
|
+
if (!data) return this.cache;
|
|
88
|
+
for (let channelData of data) this.set(channelData);
|
|
89
|
+
return this.cache;
|
|
90
|
+
}
|
|
91
|
+
set(data: APIBaseChannel): Channel<Structure> {
|
|
92
|
+
if (!data.type) return;
|
|
93
|
+
const channel = new Channel(
|
|
94
|
+
{
|
|
95
|
+
baseUrl: Routes.guilds[this.structure.key as "bets"].resource(this.guild.id, this.structure._id, "channels"),
|
|
96
|
+
data: data,
|
|
97
|
+
guild: this.guild,
|
|
98
|
+
manager: this,
|
|
99
|
+
},
|
|
100
|
+
this.rest
|
|
101
|
+
);
|
|
102
|
+
this.cache.set(data.type, channel);
|
|
103
|
+
return channel;
|
|
104
|
+
}
|
|
105
|
+
async update(type: string, payload: Optional<APIBaseChannel>): Promise<Channel<Structure>> {
|
|
106
|
+
Assertion.assertString(type);
|
|
107
|
+
Assertion.assertObject(payload);
|
|
108
|
+
|
|
109
|
+
const route = Routes.fields(this.baseUrl, type);
|
|
110
|
+
const response = await this.rest.request<APIBaseChannel, {}>({
|
|
111
|
+
method: "PATCH",
|
|
112
|
+
url: route,
|
|
113
|
+
payload,
|
|
114
|
+
});
|
|
115
|
+
const channel = this.set(response);
|
|
116
|
+
this.rest.emit("betUpdate", this.structure, this.structure);
|
|
117
|
+
return channel;
|
|
118
|
+
}
|
|
119
|
+
async fetch(type: string): Promise<Channel<Structure>> {
|
|
120
|
+
Assertion.assertString(type);
|
|
121
|
+
|
|
122
|
+
const route = Routes.fields(this.baseUrl, type);
|
|
123
|
+
const response = await this.rest.request<APIBaseChannel, {}>({
|
|
124
|
+
method: "GET",
|
|
125
|
+
url: route,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const channel = this.set(response);
|
|
129
|
+
this.cache.set(channel.type, channel);
|
|
130
|
+
|
|
131
|
+
return channel;
|
|
132
|
+
}
|
|
133
|
+
async fetchAll() {
|
|
134
|
+
const response = await this.rest.request<APIBaseChannel[], {}>({
|
|
135
|
+
method: "GET",
|
|
136
|
+
url: this.baseUrl,
|
|
137
|
+
});
|
|
138
|
+
if (Array.isArray(response) && response.length === 0) {
|
|
139
|
+
this.cache.clear();
|
|
140
|
+
return this.cache;
|
|
141
|
+
}
|
|
142
|
+
for (let channelData of response) this.set(channelData);
|
|
143
|
+
return this.cache;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async delete(type: string): Promise<Collection<string, Channel<Structure>>> {
|
|
147
|
+
Assertion.assertString(type);
|
|
148
|
+
|
|
149
|
+
const route = this.baseUrl;
|
|
150
|
+
const response = await this.rest.request<APIBaseChannel[], {}>({
|
|
151
|
+
method: "DELETE",
|
|
152
|
+
url: Routes.fields(route, type),
|
|
153
|
+
});
|
|
154
|
+
this.cache.delete(type);
|
|
155
|
+
return this.cache;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async deleteAll(): Promise<boolean> {
|
|
159
|
+
const route = this.baseUrl;
|
|
160
|
+
const value = await this.rest.request<boolean, {}>({
|
|
161
|
+
method: "DELETE",
|
|
162
|
+
url: route,
|
|
163
|
+
});
|
|
164
|
+
this.cache.clear();
|
|
165
|
+
return value;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { REST } from "../../../rest/REST";
|
|
2
|
+
import { Routes } from "../../../rest/Routes";
|
|
3
|
+
import { Collection } from "../../../structures/Collection";
|
|
4
|
+
import { Guild } from "../../../structures/guild/Guild";
|
|
5
|
+
import { GuildMatch } from "../../../structures/match/GuildMatch";
|
|
6
|
+
import { APIGuildMatch, Optional } from "../../../types";
|
|
7
|
+
import { Assertion } from "../../../utils/Assertion";
|
|
8
|
+
|
|
9
|
+
export class GuildMatchManager {
|
|
10
|
+
/** A cache of users */
|
|
11
|
+
cache: Collection<string, GuildMatch>;
|
|
12
|
+
|
|
13
|
+
/** The rest client */
|
|
14
|
+
rest: REST;
|
|
15
|
+
|
|
16
|
+
/** GuildMatch match 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
|
+
|
|
26
|
+
this.cache = new Collection<string, GuildMatch>("matches");
|
|
27
|
+
this.rest = rest;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Fetch a match
|
|
32
|
+
* @param id Id of the match to fetch
|
|
33
|
+
* @returns APIBetUser
|
|
34
|
+
*/
|
|
35
|
+
async fetch(id: string) {
|
|
36
|
+
const route = Routes.guilds.matches.get(this.guild.id, id);
|
|
37
|
+
const response = await this.rest.request<APIGuildMatch, {}>({
|
|
38
|
+
method: "get",
|
|
39
|
+
url: route,
|
|
40
|
+
});
|
|
41
|
+
const match = new GuildMatch(response, this.guild, this, this.rest);
|
|
42
|
+
this.cache.set(match._id, match);
|
|
43
|
+
this.rest.matches.set(match._id, match);
|
|
44
|
+
|
|
45
|
+
return match;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async fetchAll() {
|
|
49
|
+
const route = Routes.guilds.matches.getAll(this.guild.id);
|
|
50
|
+
const response = await this.rest.request<APIGuildMatch[], {}>({
|
|
51
|
+
method: "get",
|
|
52
|
+
url: route,
|
|
53
|
+
});
|
|
54
|
+
if (Array.isArray(response) && response.length === 0) {
|
|
55
|
+
this.cache.clear();
|
|
56
|
+
return this.cache;
|
|
57
|
+
}
|
|
58
|
+
this.setAll(response);
|
|
59
|
+
return this.cache;
|
|
60
|
+
}
|
|
61
|
+
set(data: APIGuildMatch): GuildMatch {
|
|
62
|
+
if (!data?._id) return;
|
|
63
|
+
const match = new GuildMatch(data, this?.guild, this, this.rest);
|
|
64
|
+
|
|
65
|
+
this.cache.set(data?._id, match);
|
|
66
|
+
this.rest.matches.set(data?._id, match);
|
|
67
|
+
return match;
|
|
68
|
+
}
|
|
69
|
+
setAll(data: APIGuildMatch[]) {
|
|
70
|
+
if (!data) return this.cache;
|
|
71
|
+
for (let match of data || []) this.set(match);
|
|
72
|
+
return this.cache;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async create(payload: Optional<APIGuildMatch>): Promise<GuildMatch> {
|
|
76
|
+
Assertion.assertObject(payload);
|
|
77
|
+
|
|
78
|
+
const route = Routes.guilds.matches.create(this.guild.id);
|
|
79
|
+
const response = await this.rest.request<APIGuildMatch, typeof payload>({
|
|
80
|
+
method: "POST",
|
|
81
|
+
url: route,
|
|
82
|
+
payload,
|
|
83
|
+
});
|
|
84
|
+
const match = this.set(response);
|
|
85
|
+
return match;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async delete(id: string) {
|
|
89
|
+
Assertion.assertString(id);
|
|
90
|
+
|
|
91
|
+
const route = Routes.guilds.matches.delete(id, this.guild.id);
|
|
92
|
+
const match = this.cache.get(id);
|
|
93
|
+
this.rest.emit("matchDelete", match);
|
|
94
|
+
|
|
95
|
+
await this.rest.request<boolean, {}>({
|
|
96
|
+
method: "DELETE",
|
|
97
|
+
url: route,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
this.cache.delete(id);
|
|
101
|
+
return this.cache;
|
|
102
|
+
}
|
|
103
|
+
async deleteAll() {
|
|
104
|
+
const route = Routes.guilds.matches.deleteAll(this.guild.id);
|
|
105
|
+
this.rest.emit("matchesDelete", this.cache);
|
|
106
|
+
|
|
107
|
+
const value = await this.rest.request<boolean, {}>({
|
|
108
|
+
method: "DELETE",
|
|
109
|
+
url: route,
|
|
110
|
+
});
|
|
111
|
+
this.cache.clear();
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
}
|