@open-discord-bots/framework 0.3.5 → 0.3.6
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/dist/api/main.js +1 -1
- package/dist/api/modules/state.d.ts +13 -11
- package/dist/api/modules/state.js +4 -4
- package/package.json +1 -1
- package/src/api/main.ts +1 -1
- package/src/api/modules/state.ts +19 -15
package/dist/api/main.js
CHANGED
|
@@ -44,7 +44,7 @@ export class ODMain {
|
|
|
44
44
|
constructor(managers, project) {
|
|
45
45
|
this.project = project;
|
|
46
46
|
this.versions = managers.versions;
|
|
47
|
-
this.versions.add(ODVersion.fromString("opendiscord:api", "v0.3.
|
|
47
|
+
this.versions.add(ODVersion.fromString("opendiscord:api", "v0.3.6"));
|
|
48
48
|
this.versions.add(ODVersion.fromString("opendiscord:livestatus", "v2.0.0"));
|
|
49
49
|
this.debugfile = managers.debugfile;
|
|
50
50
|
this.console = managers.console;
|
|
@@ -6,16 +6,18 @@ import * as discord from "discord.js";
|
|
|
6
6
|
/**## ODStateKey `type`
|
|
7
7
|
* The key template for message states.
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
10
|
-
/**A valid discord server/guild ID or instance. */
|
|
11
|
-
guild?: discord.Guild | string | null;
|
|
9
|
+
export type ODStateKey<WithGuildKey extends boolean, WithUserKey extends boolean> = {
|
|
12
10
|
/**A valid discord channel ID or instance. */
|
|
13
11
|
channel: discord.Channel | string;
|
|
14
12
|
/**A valid discord message ID or instance. */
|
|
15
13
|
message: discord.Message | string;
|
|
14
|
+
} & (WithGuildKey extends true ? {
|
|
15
|
+
/**A valid discord server/guild ID or instance. */
|
|
16
|
+
guild: discord.Guild | string;
|
|
17
|
+
} : {}) & (WithUserKey extends true ? {
|
|
16
18
|
/**A valid discord user ID or instance. */
|
|
17
|
-
user
|
|
18
|
-
}
|
|
19
|
+
user: discord.User | discord.GuildMember | string;
|
|
20
|
+
} : {});
|
|
19
21
|
/**## ODStateData `type`
|
|
20
22
|
* The raw data template for message states used for storing in the database.
|
|
21
23
|
*/
|
|
@@ -65,7 +67,7 @@ export interface ODStateSettings {
|
|
|
65
67
|
*
|
|
66
68
|
* Features automatic garbage collection to clear expired states.
|
|
67
69
|
*/
|
|
68
|
-
export declare class ODState<StateData extends any> extends ODManagerData {
|
|
70
|
+
export declare class ODState<StateData extends any, WithGuildKey extends boolean = true, WithUserKey extends boolean = true> extends ODManagerData {
|
|
69
71
|
/**Alias to Open Discord message states database. */
|
|
70
72
|
protected database: ODDatabase<ODDatabaseIdConstraint>;
|
|
71
73
|
/**Alias to Open Discord client manager. */
|
|
@@ -82,17 +84,17 @@ export declare class ODState<StateData extends any> extends ODManagerData {
|
|
|
82
84
|
/**Purge all expired message states that reached a timeout or ephemeral. */
|
|
83
85
|
protected purgeExpiredStates(): Promise<void>;
|
|
84
86
|
/**Transform the object-based message state key contents to a string. */
|
|
85
|
-
protected transformKey(key: ODStateKey): string;
|
|
87
|
+
protected transformKey(key: ODStateKey<WithGuildKey, WithUserKey>): string;
|
|
86
88
|
/**Transform the message state data contents for storage in the database. */
|
|
87
|
-
protected transformData(key: ODStateKey, data: StateData, isEphemeral: boolean, keepCreatedDate?: number): ODStateData<StateData>;
|
|
89
|
+
protected transformData(key: ODStateKey<WithGuildKey, WithUserKey>, data: StateData, isEphemeral: boolean, keepCreatedDate?: number): ODStateData<StateData>;
|
|
88
90
|
/**Calculate milliseconds from a time + unit. */
|
|
89
91
|
protected timeoutToUnixTime(): number | null;
|
|
90
92
|
/**Set a message state using guild, channel & message id as key. Returns `true` when overwritten. */
|
|
91
|
-
setMsgState(key: ODStateKey, data: StateData, isEphemeral: boolean): Promise<boolean>;
|
|
93
|
+
setMsgState(key: ODStateKey<WithGuildKey, WithUserKey>, data: StateData, isEphemeral: boolean): Promise<boolean>;
|
|
92
94
|
/**Get a message state using guild, channel & message id as key. */
|
|
93
|
-
getMsgState(key: ODStateKey): Promise<ODStateData<StateData> | null>;
|
|
95
|
+
getMsgState(key: ODStateKey<WithGuildKey, WithUserKey>): Promise<ODStateData<StateData> | null>;
|
|
94
96
|
/**Delete a message state using guild, channel & message id as key. Returns `true` when deleted. */
|
|
95
|
-
deleteMsgState(key: ODStateKey): Promise<boolean>;
|
|
97
|
+
deleteMsgState(key: ODStateKey<WithGuildKey, WithUserKey>): Promise<boolean>;
|
|
96
98
|
/**List all message states of this `ODState`. */
|
|
97
99
|
listMsgStates(): Promise<{
|
|
98
100
|
key: string;
|
|
@@ -120,18 +120,18 @@ export class ODState extends ODManagerData {
|
|
|
120
120
|
}
|
|
121
121
|
/**Transform the object-based message state key contents to a string. */
|
|
122
122
|
transformKey(key) {
|
|
123
|
-
const newGuild = (!key.guild) ? "NULL" : (typeof key.guild === "string" ? key.guild : key.guild.id);
|
|
123
|
+
const newGuild = (!("guild" in key) || !key.guild) ? "NULL" : (typeof key.guild === "string" ? key.guild : key.guild.id);
|
|
124
124
|
const newChannel = (!key.channel) ? "NULL" : (typeof key.channel === "string" ? key.channel : key.channel.id);
|
|
125
125
|
const newMessage = (!key.message) ? "NULL" : (typeof key.message === "string" ? key.message : key.message.id);
|
|
126
|
-
const newUser = (!key.user) ? "NULL" : (typeof key.user === "string" ? key.user : key.user.id);
|
|
126
|
+
const newUser = (!("user" in key) || !key.user) ? "NULL" : (typeof key.user === "string" ? key.user : key.user.id);
|
|
127
127
|
return `G:${newGuild},C:${newChannel},M:${newMessage},U:${newUser}`;
|
|
128
128
|
}
|
|
129
129
|
/**Transform the message state data contents for storage in the database. */
|
|
130
130
|
transformData(key, data, isEphemeral, keepCreatedDate) {
|
|
131
|
-
const guildId = (!key.guild) ? null : (typeof key.guild === "string" ? key.guild : key.guild.id);
|
|
131
|
+
const guildId = (!("guild" in key) || !key.guild) ? null : (typeof key.guild === "string" ? key.guild : key.guild.id);
|
|
132
132
|
const channelId = (typeof key.channel === "string" ? key.channel : key.channel.id);
|
|
133
133
|
const messageId = (typeof key.message === "string" ? key.message : key.message.id);
|
|
134
|
-
const userId = (!key.user) ? null : (typeof key.user === "string" ? key.user : key.user.id);
|
|
134
|
+
const userId = (!("user" in key) || !key.user) ? null : (typeof key.user === "string" ? key.user : key.user.id);
|
|
135
135
|
const createdDate = keepCreatedDate ?? Date.now();
|
|
136
136
|
const modifiedDate = Date.now();
|
|
137
137
|
const unmodifiedTimeoutDate = this.timeoutToUnixTime();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-discord-bots/framework",
|
|
3
3
|
"author": "DJj123dj",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.6",
|
|
5
5
|
"description": "The core framework of the popular open-source discord bots: Open Ticket & Open Moderation.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
package/src/api/main.ts
CHANGED
|
@@ -154,7 +154,7 @@ export abstract class ODMain implements ODMainManagers {
|
|
|
154
154
|
constructor(managers:ODMainManagers,project:ODProjectType){
|
|
155
155
|
this.project = project
|
|
156
156
|
this.versions = managers.versions
|
|
157
|
-
this.versions.add(ODVersion.fromString("opendiscord:api","v0.3.
|
|
157
|
+
this.versions.add(ODVersion.fromString("opendiscord:api","v0.3.6"))
|
|
158
158
|
this.versions.add(ODVersion.fromString("opendiscord:livestatus","v2.0.0"))
|
|
159
159
|
|
|
160
160
|
this.debugfile = managers.debugfile
|
package/src/api/modules/state.ts
CHANGED
|
@@ -10,16 +10,20 @@ import * as discord from "discord.js"
|
|
|
10
10
|
/**## ODStateKey `type`
|
|
11
11
|
* The key template for message states.
|
|
12
12
|
*/
|
|
13
|
-
export
|
|
14
|
-
/**A valid discord server/guild ID or instance. */
|
|
15
|
-
guild?:discord.Guild|string|null,
|
|
13
|
+
export type ODStateKey<WithGuildKey extends boolean,WithUserKey extends boolean> = {
|
|
16
14
|
/**A valid discord channel ID or instance. */
|
|
17
15
|
channel:discord.Channel|string,
|
|
18
16
|
/**A valid discord message ID or instance. */
|
|
19
17
|
message:discord.Message|string,
|
|
20
|
-
/**A valid discord user ID or instance. */
|
|
21
|
-
user?:discord.User|discord.GuildMember|string|null
|
|
22
18
|
}
|
|
19
|
+
& (WithGuildKey extends true ? {
|
|
20
|
+
/**A valid discord server/guild ID or instance. */
|
|
21
|
+
guild:discord.Guild|string,
|
|
22
|
+
} : {})
|
|
23
|
+
& (WithUserKey extends true ? {
|
|
24
|
+
/**A valid discord user ID or instance. */
|
|
25
|
+
user:discord.User|discord.GuildMember|string
|
|
26
|
+
} : {})
|
|
23
27
|
|
|
24
28
|
/**## ODStateData `type`
|
|
25
29
|
* The raw data template for message states used for storing in the database.
|
|
@@ -69,7 +73,7 @@ export interface ODStateSettings {
|
|
|
69
73
|
*
|
|
70
74
|
* Features automatic garbage collection to clear expired states.
|
|
71
75
|
*/
|
|
72
|
-
export class ODState<StateData extends any> extends ODManagerData {
|
|
76
|
+
export class ODState<StateData extends any,WithGuildKey extends boolean = true,WithUserKey extends boolean = true> extends ODManagerData {
|
|
73
77
|
/**Alias to Open Discord message states database. */
|
|
74
78
|
protected database: ODDatabase<ODDatabaseIdConstraint>
|
|
75
79
|
/**Alias to Open Discord client manager. */
|
|
@@ -175,20 +179,20 @@ export class ODState<StateData extends any> extends ODManagerData {
|
|
|
175
179
|
}
|
|
176
180
|
}
|
|
177
181
|
/**Transform the object-based message state key contents to a string. */
|
|
178
|
-
protected transformKey(key:ODStateKey){
|
|
179
|
-
const newGuild = (!key.guild) ? "NULL" : (typeof key.guild === "string" ? key.guild : key.guild.id)
|
|
182
|
+
protected transformKey(key:ODStateKey<WithGuildKey,WithUserKey>){
|
|
183
|
+
const newGuild = (!("guild" in key) || !key.guild) ? "NULL" : (typeof key.guild === "string" ? key.guild : key.guild.id)
|
|
180
184
|
const newChannel = (!key.channel) ? "NULL" : (typeof key.channel === "string" ? key.channel : key.channel.id)
|
|
181
185
|
const newMessage = (!key.message) ? "NULL" : (typeof key.message === "string" ? key.message : key.message.id)
|
|
182
|
-
const newUser = (!key.user) ? "NULL" : (typeof key.user === "string" ? key.user : key.user.id)
|
|
186
|
+
const newUser = (!("user" in key) || !key.user) ? "NULL" : (typeof key.user === "string" ? key.user : key.user.id)
|
|
183
187
|
|
|
184
188
|
return `G:${newGuild},C:${newChannel},M:${newMessage},U:${newUser}`
|
|
185
189
|
}
|
|
186
190
|
/**Transform the message state data contents for storage in the database. */
|
|
187
|
-
protected transformData(key:ODStateKey,data:StateData,isEphemeral:boolean,keepCreatedDate?:number): ODStateData<StateData> {
|
|
188
|
-
const guildId = (!key.guild) ? null : (typeof key.guild === "string" ? key.guild : key.guild.id)
|
|
191
|
+
protected transformData(key:ODStateKey<WithGuildKey,WithUserKey>,data:StateData,isEphemeral:boolean,keepCreatedDate?:number): ODStateData<StateData> {
|
|
192
|
+
const guildId = (!("guild" in key) || !key.guild) ? null : (typeof key.guild === "string" ? key.guild : key.guild.id)
|
|
189
193
|
const channelId = (typeof key.channel === "string" ? key.channel : key.channel.id)
|
|
190
194
|
const messageId = (typeof key.message === "string" ? key.message : key.message.id)
|
|
191
|
-
const userId = (!key.user) ? null : (typeof key.user === "string" ? key.user : key.user.id)
|
|
195
|
+
const userId = (!("user" in key) || !key.user) ? null : (typeof key.user === "string" ? key.user : key.user.id)
|
|
192
196
|
const createdDate = keepCreatedDate ?? Date.now()
|
|
193
197
|
const modifiedDate = Date.now()
|
|
194
198
|
|
|
@@ -209,7 +213,7 @@ export class ODState<StateData extends any> extends ODManagerData {
|
|
|
209
213
|
else return null
|
|
210
214
|
}
|
|
211
215
|
/**Set a message state using guild, channel & message id as key. Returns `true` when overwritten. */
|
|
212
|
-
async setMsgState(key:ODStateKey,data:StateData,isEphemeral:boolean): Promise<boolean> {
|
|
216
|
+
async setMsgState(key:ODStateKey<WithGuildKey,WithUserKey>,data:StateData,isEphemeral:boolean): Promise<boolean> {
|
|
213
217
|
const rawKey = this.transformKey(key)
|
|
214
218
|
|
|
215
219
|
const existingData = await this.getMsgState(key)
|
|
@@ -217,14 +221,14 @@ export class ODState<StateData extends any> extends ODManagerData {
|
|
|
217
221
|
return await this.database.set(this.id.value,rawKey,contents)
|
|
218
222
|
}
|
|
219
223
|
/**Get a message state using guild, channel & message id as key. */
|
|
220
|
-
async getMsgState(key:ODStateKey): Promise<ODStateData<StateData>|null> {
|
|
224
|
+
async getMsgState(key:ODStateKey<WithGuildKey,WithUserKey>): Promise<ODStateData<StateData>|null> {
|
|
221
225
|
const rawKey = this.transformKey(key)
|
|
222
226
|
const rawData = await this.database.get(this.id.value,rawKey)
|
|
223
227
|
if (typeof rawData !== "object") return null
|
|
224
228
|
else return rawData as ODStateData<StateData>
|
|
225
229
|
}
|
|
226
230
|
/**Delete a message state using guild, channel & message id as key. Returns `true` when deleted. */
|
|
227
|
-
async deleteMsgState(key:ODStateKey): Promise<boolean> {
|
|
231
|
+
async deleteMsgState(key:ODStateKey<WithGuildKey,WithUserKey>): Promise<boolean> {
|
|
228
232
|
const rawKey = this.transformKey(key)
|
|
229
233
|
return await this.database.delete(this.id.value,rawKey)
|
|
230
234
|
}
|