@elara-services/packages 6.1.0 → 6.1.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 (37) hide show
  1. package/dist/package.json +3 -2
  2. package/dist/src/lib/misc.d.ts +1 -1
  3. package/dist/src/lib/misc.js +15 -3
  4. package/dist/src/lib/tasks.d.ts +12 -5
  5. package/dist/src/lib/tasks.js +37 -12
  6. package/docs/assets/search.js +1 -1
  7. package/docs/classes/AES.html +7 -6
  8. package/docs/classes/Minesweeper.html +13 -12
  9. package/docs/functions/fetch.html +18 -4
  10. package/docs/functions/randomNumber.html +3 -2
  11. package/docs/functions/randomWeight.html +3 -2
  12. package/docs/functions/randomWords.html +3 -2
  13. package/docs/index.html +2 -1
  14. package/docs/interfaces/ButtonOptions.html +12 -11
  15. package/docs/interfaces/ModalOptions.html +8 -7
  16. package/docs/interfaces/RandomNumberOptions.html +6 -5
  17. package/docs/interfaces/RandomWord.html +11 -10
  18. package/docs/interfaces/SelectOptions.html +14 -13
  19. package/docs/interfaces/Slash.html +10 -9
  20. package/docs/interfaces/SlashOptions.html +16 -15
  21. package/docs/interfaces/TaskCreate.html +6 -5
  22. package/docs/interfaces/TextInputOptions.html +17 -16
  23. package/docs/modules.html +4 -2
  24. package/docs/types/ButtonNumberStyles.html +3 -2
  25. package/docs/types/ButtonStyles.html +3 -2
  26. package/docs/types/ChannelTypes.html +3 -2
  27. package/docs/types/Lang.html +3 -2
  28. package/docs/types/LangName.html +3 -2
  29. package/docs/types/TaskCreateRun.html +81 -0
  30. package/docs/variables/ButtonStyle.html +3 -2
  31. package/docs/variables/Duration.html +3 -2
  32. package/docs/variables/Interactions.html +3 -2
  33. package/docs/variables/Languages.html +4 -3
  34. package/docs/variables/SlashBuilder.html +3 -2
  35. package/docs/variables/Tasks.html +158 -0
  36. package/package.json +3 -2
  37. package/docs/classes/Tasks.html +0 -158
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elara-services/packages",
3
- "version": "6.1.0",
3
+ "version": "6.1.1",
4
4
  "description": "",
5
5
  "main": "./dist/src/index.js",
6
6
  "author": "SUPERCHIEFYT (Elara-Discord-Bots, Elara-Services)",
@@ -37,8 +37,9 @@
37
37
  "@elara-services/fetch": "2.0.1"
38
38
  },
39
39
  "dependencies": {
40
- "@elara-services/utils": "^1.2.31",
40
+ "@elara-services/utils": "^2.0.18",
41
41
  "discord-api-types": "0.37.48",
42
+ "eventemitter3": "^5.0.1",
42
43
  "node-schedule": "^2.1.1"
43
44
  }
44
45
  }
@@ -1 +1 @@
1
- export declare function fetch<B extends object, T>(url: string, key?: string, body?: B | undefined, postRequest?: boolean, returnRaw?: boolean, addHeaders?: Record<string, string | number>): Promise<T | null>;
1
+ export declare function fetch<B extends object, T>(url: string, key?: string, body?: B | undefined, postRequest?: boolean, returnRaw?: boolean, addHeaders?: Record<string, string | number>, errors?: (e: Error) => Promise<unknown> | unknown): Promise<T | null>;
@@ -34,7 +34,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.fetch = void 0;
36
36
  const utils_1 = require("@elara-services/utils");
37
- function fetch(url, key = "", body = undefined, postRequest = false, returnRaw = false, addHeaders) {
37
+ function fetch(url, key = "", body = undefined, postRequest = false, returnRaw = false, addHeaders, errors) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
39
  try {
40
40
  const client = yield Promise.resolve().then(() => __importStar(require("@elara-services/fetch"))).catch(() => { });
@@ -60,8 +60,17 @@ function fetch(url, key = "", body = undefined, postRequest = false, returnRaw =
60
60
  .header(headers)
61
61
  .body(body, "json")
62
62
  .send()
63
- .catch(() => ({ statusCode: 500, body: "", json: () => "" }));
63
+ .catch((e) => new Error(e));
64
+ if (res instanceof Error) {
65
+ if (errors) {
66
+ errors(res);
67
+ }
68
+ return null;
69
+ }
64
70
  if (res.statusCode !== 200) {
71
+ if (errors) {
72
+ errors(new Error(`Got (${res.statusCode}) status while trying to fetch ${url}`));
73
+ }
65
74
  return null;
66
75
  }
67
76
  if (returnRaw) {
@@ -69,7 +78,10 @@ function fetch(url, key = "", body = undefined, postRequest = false, returnRaw =
69
78
  }
70
79
  return res.json();
71
80
  }
72
- catch (_a) {
81
+ catch (err) {
82
+ if (errors) {
83
+ errors(err instanceof Error ? err : new Error(err));
84
+ }
73
85
  return null;
74
86
  }
75
87
  });
@@ -1,8 +1,15 @@
1
- export declare class Tasks extends null {
2
- static create({ id, time, shouldCancel }: TaskCreate | undefined, run: (...args: unknown[]) => Promise<unknown> | unknown): string | import("node-schedule").Job;
3
- static delete(id: string): boolean;
4
- static has(id: string): boolean;
5
- }
1
+ import { Collection } from "@elara-services/utils";
2
+ import { type Job } from "node-schedule";
3
+ export type TaskCreateRun = (...args: unknown[]) => Promise<unknown> | unknown;
4
+ export declare const Tasks: {
5
+ create: ({ id, time, shouldCancel }: TaskCreate | undefined, run: TaskCreateRun) => string | Job;
6
+ has: (id: string) => boolean;
7
+ delete: (id: string) => boolean;
8
+ cancelAll: () => void;
9
+ list: () => Collection<string, Job>;
10
+ dateFrom: (ms: number) => string;
11
+ repeat: (id: string, ms: number, run: TaskCreateRun) => void;
12
+ };
6
13
  export interface TaskCreate {
7
14
  id: string;
8
15
  time: string;
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Tasks = void 0;
4
4
  const utils_1 = require("@elara-services/utils");
5
5
  const node_schedule_1 = require("node-schedule");
6
- class Tasks extends null {
7
- static create({ id, time, shouldCancel } = {
6
+ exports.Tasks = {
7
+ create: ({ id, time, shouldCancel } = {
8
8
  id: "",
9
9
  time: "",
10
10
  shouldCancel: true,
11
- }, run) {
11
+ }, run) => {
12
12
  if (!utils_1.is.string(id) || !utils_1.is.string(time) || !utils_1.is.boolean(shouldCancel)) {
13
13
  return `You failed to provide one of the following options: 'id', 'time' or 'shouldCancel'`;
14
14
  }
@@ -21,12 +21,37 @@ class Tasks extends null {
21
21
  return void (0, node_schedule_1.cancelJob)(id);
22
22
  }
23
23
  });
24
- }
25
- static delete(id) {
26
- return (0, node_schedule_1.cancelJob)(id);
27
- }
28
- static has(id) {
29
- return node_schedule_1.scheduledJobs[id] ? true : false;
30
- }
31
- }
32
- exports.Tasks = Tasks;
24
+ },
25
+ has: (id) => node_schedule_1.scheduledJobs[id] ? true : false,
26
+ delete: (id) => (0, node_schedule_1.cancelJob)(id),
27
+ cancelAll: () => {
28
+ for (const v of Object.values(node_schedule_1.scheduledJobs)) {
29
+ v.cancel();
30
+ }
31
+ },
32
+ list: () => {
33
+ const list = new utils_1.Collection();
34
+ for (const v of Object.keys(node_schedule_1.scheduledJobs)) {
35
+ if (!node_schedule_1.scheduledJobs[v]) {
36
+ continue;
37
+ }
38
+ list.set(v, node_schedule_1.scheduledJobs[v]);
39
+ }
40
+ return list;
41
+ },
42
+ dateFrom: (ms) => new Date(Date.now() + ms).toISOString(),
43
+ repeat: (id, ms, run) => {
44
+ if (!id.startsWith("repeat-")) {
45
+ id = `repeat-${id}-${utils_1.snowflakes.generate()}`;
46
+ }
47
+ const schedule = () => exports.Tasks.create({
48
+ id,
49
+ time: exports.Tasks.dateFrom(ms),
50
+ shouldCancel: true,
51
+ }, () => {
52
+ exports.Tasks.repeat(`${id.split("-").slice(0, 2).join("-")}-${utils_1.snowflakes.generate()}`, ms, run);
53
+ run();
54
+ });
55
+ schedule();
56
+ },
57
+ };