gralonium 0.2.0 → 0.4.0

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 (47) hide show
  1. package/lib/classes/Client.d.ts +9 -11
  2. package/lib/classes/Client.js +10 -13
  3. package/lib/classes/Context.d.ts +43 -4
  4. package/lib/classes/Context.js +251 -12
  5. package/lib/classes/Cooldowns.d.ts +3 -3
  6. package/lib/classes/Cooldowns.js +3 -8
  7. package/lib/classes/HelpCommand.d.ts +55 -13
  8. package/lib/classes/HelpCommand.js +319 -1
  9. package/lib/classes/Loader.d.ts +17 -11
  10. package/lib/classes/Loader.js +43 -42
  11. package/lib/classes/Utils.d.ts +4 -5
  12. package/lib/classes/Utils.js +32 -21
  13. package/lib/classes/builders/CommandBuilder.d.ts +10 -0
  14. package/lib/classes/builders/CommandBuilder.js +16 -0
  15. package/lib/classes/builders/ComponentsV2Builder.d.ts +285 -113
  16. package/lib/classes/builders/ComponentsV2Builder.js +1069 -182
  17. package/lib/classes/builders/GroupBuilder.d.ts +9 -0
  18. package/lib/classes/builders/GroupBuilder.js +16 -0
  19. package/lib/classes/builders/InteractionBuilder.d.ts +40 -25
  20. package/lib/classes/builders/InteractionBuilder.js +102 -1
  21. package/lib/classes/builders/components/StyleMapper.d.ts +1 -1
  22. package/lib/classes/builders/components/StyleMapper.js +2 -1
  23. package/lib/classes/builders/components/helpers.d.ts +68 -4
  24. package/lib/classes/builders/components/helpers.js +366 -7
  25. package/lib/classes/builders/components/templates.d.ts +151 -9
  26. package/lib/classes/builders/components/templates.js +376 -17
  27. package/lib/classes/builders/components/text.d.ts +24 -0
  28. package/lib/classes/builders/components/text.js +91 -0
  29. package/lib/classes/experiments/Confirmator.d.ts +45 -50
  30. package/lib/classes/experiments/Confirmator.js +37 -21
  31. package/lib/classes/experiments/Paginator.d.ts +49 -56
  32. package/lib/classes/experiments/Paginator.js +48 -19
  33. package/lib/classes/views/V2Paginator.d.ts +91 -0
  34. package/lib/classes/views/V2Paginator.js +268 -0
  35. package/lib/classes/views/V2View.d.ts +113 -0
  36. package/lib/classes/views/V2View.js +438 -0
  37. package/lib/experiments.d.ts +2 -0
  38. package/lib/experiments.js +9 -1
  39. package/lib/main.d.ts +3 -3
  40. package/lib/main.js +5 -5
  41. package/package.json +6 -15
  42. package/lib/events/interactionCreate.d.ts +0 -4
  43. package/lib/events/interactionCreate.js +0 -17
  44. package/lib/events/messageCreate.d.ts +0 -4
  45. package/lib/events/messageCreate.js +0 -17
  46. package/lib/events/messageUpdate.d.ts +0 -4
  47. package/lib/events/messageUpdate.js +0 -17
@@ -3,13 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Confirmator = void 0;
4
4
 
5
5
  const discord_js_1 = require("discord.js");
6
+ const helpers_js_1 = require("../builders/components/helpers.js");
7
+ const StyleMapper_js_1 = require("../builders/components/StyleMapper.js");
6
8
 
7
9
  class Confirmator {
8
10
  _author;
9
11
  _continue = async () => null;
10
- _cancel = async (interaction) => interaction.reply({ content: "Cancelled process.", ephemeral: true });
11
- _filter = async (interaction) => interaction.reply({ content: "This interaction isn't for you!", ephemeral: true });
12
+ _cancel = async (interaction) => interaction.reply({ content: "Cancelled process.", flags: discord_js_1.MessageFlags.Ephemeral });
13
+ _filter = async (interaction) =>
14
+ interaction.reply({ content: "This interaction isn't for you!", flags: discord_js_1.MessageFlags.Ephemeral });
12
15
  filter = async (interaction) => interaction.user.id !== this._author.id;
16
+ _continueId = "continueButton";
17
+ _cancelId = "cancelButton";
18
+ _disableOnEnd = true;
13
19
  timeout;
14
20
  message;
15
21
  bot;
@@ -17,6 +23,10 @@ class Confirmator {
17
23
  constructor(bot, options) {
18
24
  this._author = options.author;
19
25
  this.bot = bot;
26
+ if (options.filter) this.filter = options.filter;
27
+ if (options.continueId) this._continueId = options.continueId;
28
+ if (options.cancelId) this._cancelId = options.cancelId;
29
+ if (options.disableOnEnd !== undefined) this._disableOnEnd = Boolean(options.disableOnEnd);
20
30
  this.timeout = options.timeout ?? 60;
21
31
  }
22
32
 
@@ -46,31 +56,35 @@ class Confirmator {
46
56
  }
47
57
 
48
58
  static Buttons(options) {
59
+ const continueStyle =
60
+ options?.continue?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.continue.style) : discord_js_1.ButtonStyle.Danger;
61
+ const cancelStyle =
62
+ options?.cancel?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.cancel.style) : discord_js_1.ButtonStyle.Success;
63
+
49
64
  const continueButton = new discord_js_1.ButtonBuilder()
50
65
  .setLabel(options?.continue?.label || "Continue")
51
- .setStyle(discord_js_1.ButtonStyle.Danger)
52
- .setCustomId("continueButton");
66
+ .setStyle(continueStyle)
67
+ .setCustomId(options?.continue?.customId || "continueButton");
68
+ if (options?.continue?.emoji) {
69
+ const parsed = helpers_js_1.parseEmoji(options.continue.emoji);
70
+ if (parsed) continueButton.setEmoji(parsed);
71
+ }
53
72
 
54
73
  const cancelButton = new discord_js_1.ButtonBuilder()
55
74
  .setLabel(options?.cancel?.label || "Cancel")
56
- .setStyle(discord_js_1.ButtonStyle.Success)
57
- .setCustomId("cancelButton");
75
+ .setStyle(cancelStyle)
76
+ .setCustomId(options?.cancel?.customId || "cancelButton");
77
+ if (options?.cancel?.emoji) {
78
+ const parsed = helpers_js_1.parseEmoji(options.cancel.emoji);
79
+ if (parsed) cancelButton.setEmoji(parsed);
80
+ }
58
81
 
59
82
  return new discord_js_1.ActionRowBuilder().addComponents(continueButton, cancelButton).toJSON();
60
83
  }
61
84
 
62
85
  async #disableMessageComponents() {
63
- if (!this.message?.editable || !this.message.components?.length) return;
64
-
65
- const components = this.message.components.map((row) => {
66
- const cloned = discord_js_1.ActionRowBuilder.from(row);
67
- for (const component of cloned.components) {
68
- component.setDisabled(true);
69
- }
70
- return cloned;
71
- });
72
-
73
- await this.message.edit({ components }).catch(() => null);
86
+ if (!this._disableOnEnd) return;
87
+ await helpers_js_1.disableMessageComponents(this.message);
74
88
  }
75
89
 
76
90
  start() {
@@ -84,7 +98,7 @@ class Confirmator {
84
98
  });
85
99
 
86
100
  collector.on("collect", async (interaction) => {
87
- if (interaction.customId !== "continueButton" && interaction.customId !== "cancelButton") {
101
+ if (interaction.customId !== this._continueId && interaction.customId !== this._cancelId) {
88
102
  return;
89
103
  }
90
104
 
@@ -93,21 +107,23 @@ class Confirmator {
93
107
  return;
94
108
  }
95
109
 
96
- if (interaction.customId === "continueButton") {
110
+ if (interaction.customId === this._continueId) {
97
111
  await interaction.deferUpdate();
98
- await collector.stop("confirmed");
112
+ collector.stop("confirmed");
99
113
  await this._continue(interaction);
100
114
  return;
101
115
  }
102
116
 
103
117
  await interaction.deferUpdate();
104
- await collector.stop("cancelled");
118
+ collector.stop("cancelled");
105
119
  await this._cancel(interaction);
106
120
  });
107
121
 
108
122
  collector.on("end", async () => {
109
123
  await this.#disableMessageComponents();
110
124
  });
125
+
126
+ return collector;
111
127
  }
112
128
  }
113
129
 
@@ -1,65 +1,58 @@
1
- import { User, Message, ButtonStyle, CollectedInteraction } from "discord.js";
1
+ import { ButtonStyle, CollectedInteraction, InteractionCollector, Message, User } from "discord.js";
2
+ import type { ButtonStyleResolvable } from "../builders/ComponentsV2Builder.js";
3
+ import type { EmojiInput } from "../builders/components/helpers.js";
2
4
  import { AsyncFunction } from "../Loader.js";
3
- import { Erine } from "../Client.js";
5
+ import { Gralonium } from "../Client.js";
6
+
4
7
  export type ButtonSchema = {
5
- style?: ButtonStyle;
6
- label?: string;
8
+ style?: ButtonStyle | ButtonStyleResolvable;
9
+ label?: string;
10
+ emoji?: EmojiInput;
11
+ customId?: string;
12
+ disabled?: boolean;
7
13
  };
8
- export type PaginatorProcess = AsyncFunction<CollectedInteraction, boolean>;
14
+
15
+ export type PaginatorProcess = AsyncFunction<CollectedInteraction, any>;
16
+
9
17
  export type PaginatorOptions<P = any> = {
10
- pages: P[];
11
- author: User;
12
- filter?: PaginatorProcess;
13
- timeout?: number;
18
+ pages: P[];
19
+ author: User;
20
+ filter?: PaginatorProcess;
21
+ timeout?: number;
22
+ previousId?: string;
23
+ nextId?: string;
24
+ disableOnEnd?: boolean;
14
25
  };
26
+
15
27
  export type PaginatorButtonsOptions = {
16
- next?: ButtonSchema;
17
- previous?: ButtonSchema;
28
+ next?: ButtonSchema;
29
+ previous?: ButtonSchema;
18
30
  };
31
+
19
32
  export declare class Paginator<P = any> {
20
- pages: P[];
21
- filter: PaginatorProcess;
22
- bot: Erine;
23
- message?: Message;
24
- timeout: number;
25
- private _author;
26
- private _page;
27
- private _filter;
28
- private _update;
29
- constructor(bot: Erine, options: PaginatorOptions);
30
- /**
31
- * Get the page content (provided in the constructor).
32
- */
33
- get page(): P;
34
- /**
35
- * Get the current page number.
36
- */
37
- get pageNumber(): number;
38
- /**
39
- * Get the page footer.
40
- * @example "3/10"
41
- */
42
- get pagesFooter(): string;
43
- /**
44
- * @param predicate The function to call when someone clicks the cancel button.
45
- */
46
- setUpdateProcess(predicate: AsyncFunction<P, any>): this;
47
- /**
48
- * @param predicate The function to call when the filter process returns false
49
- */
50
- setFilterProcess(predicate: PaginatorProcess): this;
51
- /**
52
- * @param predicate The function to call and check if is true, if not it will throw an error (setted with .setFilterErrorProcess())
53
- */
54
- setFilterCheckProcess(predicate: PaginatorProcess): this;
55
- /**
56
- * You have to use this after you send the message with the buttons, this is required.
57
- * @param message The message that will be used in the process
58
- */
59
- setMessage(message: Message): this;
60
- /**
61
- * @param options Optional options to customize your buttons
62
- */
63
- static Buttons(options?: PaginatorButtonsOptions): import("discord.js").APIActionRowComponent<import("discord.js").APIStringSelectComponent | import("discord.js").APIChannelSelectComponent | import("discord.js").APIMentionableSelectComponent | import("discord.js").APIUserSelectComponent | import("discord.js").APIRoleSelectComponent | import("discord.js").APIButtonComponent | import("discord.js").APITextInputComponent>;
64
- start(): void;
33
+ pages: P[];
34
+ filter: PaginatorProcess;
35
+ bot: Gralonium;
36
+ message?: Message;
37
+ timeout: number;
38
+ constructor(bot: Gralonium, options: PaginatorOptions<P>);
39
+ get page(): P;
40
+ get pageNumber(): number;
41
+ get pagesFooter(): string;
42
+ setUpdateProcess(predicate: AsyncFunction<P, any>): this;
43
+ setFilterProcess(predicate: PaginatorProcess): this;
44
+ setFilterCheckProcess(predicate: PaginatorProcess): this;
45
+ setMessage(message: Message): this;
46
+ static Buttons(
47
+ options?: PaginatorButtonsOptions
48
+ ): import("discord.js").APIActionRowComponent<
49
+ | import("discord.js").APIStringSelectComponent
50
+ | import("discord.js").APIChannelSelectComponent
51
+ | import("discord.js").APIMentionableSelectComponent
52
+ | import("discord.js").APIUserSelectComponent
53
+ | import("discord.js").APIRoleSelectComponent
54
+ | import("discord.js").APIButtonComponent
55
+ | import("discord.js").APITextInputComponent
56
+ >;
57
+ start(): InteractionCollector<any>;
65
58
  }
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Paginator = void 0;
4
4
 
5
5
  const discord_js_1 = require("discord.js");
6
+ const helpers_js_1 = require("../builders/components/helpers.js");
7
+ const StyleMapper_js_1 = require("../builders/components/StyleMapper.js");
8
+ const ComponentsV2Builder_js_1 = require("../builders/ComponentsV2Builder.js");
6
9
 
7
10
  class Paginator {
8
11
  pages;
@@ -12,14 +15,29 @@ class Paginator {
12
15
  timeout;
13
16
  _author;
14
17
  _page = 0;
15
- _filter = async (interaction) => interaction.reply({ content: "This interaction isn't for you!", ephemeral: true });
16
- _update = async (content) => this.message.edit({ content: content.toString() });
18
+ _previousId = "previousButton";
19
+ _nextId = "continueButton";
20
+ _disableOnEnd = true;
21
+ _filter = async (interaction) =>
22
+ interaction.reply({ content: "This interaction isn't for you!", flags: discord_js_1.MessageFlags.Ephemeral });
23
+ _update = async (content) => {
24
+ if (ComponentsV2Builder_js_1.ComponentsV2Builder.isBuilder(content)) {
25
+ return this.message.edit(content.toPayload());
26
+ }
27
+ if (content && typeof content === "object" && (content.components || content.embeds || content.content !== undefined)) {
28
+ return this.message.edit(content);
29
+ }
30
+ return this.message.edit({ content: content.toString() });
31
+ };
17
32
 
18
33
  constructor(bot, options) {
19
34
  this.bot = bot;
20
35
  this.pages = options.pages;
21
36
  this._author = options.author;
22
37
  if (options.filter) this.filter = options.filter;
38
+ if (options.previousId) this._previousId = options.previousId;
39
+ if (options.nextId) this._nextId = options.nextId;
40
+ if (options.disableOnEnd !== undefined) this._disableOnEnd = Boolean(options.disableOnEnd);
23
41
  this.timeout = options.timeout ?? 60;
24
42
  }
25
43
 
@@ -56,31 +74,40 @@ class Paginator {
56
74
  }
57
75
 
58
76
  static Buttons(options) {
77
+ const nextStyle = options?.next?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.next.style) : discord_js_1.ButtonStyle.Primary;
78
+ const prevStyle =
79
+ options?.previous?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.previous.style) : discord_js_1.ButtonStyle.Primary;
80
+
59
81
  const nextButton = new discord_js_1.ButtonBuilder()
60
- .setCustomId("continueButton")
61
- .setStyle(options?.previous?.style || discord_js_1.ButtonStyle.Primary)
82
+ .setCustomId(options?.next?.customId || "continueButton")
83
+ .setStyle(nextStyle)
62
84
  .setLabel(options?.next?.label || "Next");
85
+ if (options?.next?.emoji) {
86
+ const parsed = helpers_js_1.parseEmoji(options.next.emoji);
87
+ if (parsed) nextButton.setEmoji(parsed);
88
+ }
89
+ if (options?.next?.disabled !== undefined) {
90
+ nextButton.setDisabled(Boolean(options.next.disabled));
91
+ }
63
92
 
64
93
  const previousButton = new discord_js_1.ButtonBuilder()
65
- .setCustomId("previousButton")
66
- .setStyle(options?.previous?.style || discord_js_1.ButtonStyle.Primary)
94
+ .setCustomId(options?.previous?.customId || "previousButton")
95
+ .setStyle(prevStyle)
67
96
  .setLabel(options?.previous?.label || "Previous");
97
+ if (options?.previous?.emoji) {
98
+ const parsed = helpers_js_1.parseEmoji(options.previous.emoji);
99
+ if (parsed) previousButton.setEmoji(parsed);
100
+ }
101
+ if (options?.previous?.disabled !== undefined) {
102
+ previousButton.setDisabled(Boolean(options.previous.disabled));
103
+ }
68
104
 
69
105
  return new discord_js_1.ActionRowBuilder().addComponents(previousButton, nextButton).toJSON();
70
106
  }
71
107
 
72
108
  async #disableMessageComponents() {
73
- if (!this.message?.editable || !this.message.components?.length) return;
74
-
75
- const components = this.message.components.map((row) => {
76
- const cloned = discord_js_1.ActionRowBuilder.from(row);
77
- for (const component of cloned.components) {
78
- component.setDisabled(true);
79
- }
80
- return cloned;
81
- });
82
-
83
- await this.message.edit({ components }).catch(() => null);
109
+ if (!this._disableOnEnd) return;
110
+ await helpers_js_1.disableMessageComponents(this.message);
84
111
  }
85
112
 
86
113
  start() {
@@ -94,7 +121,7 @@ class Paginator {
94
121
  });
95
122
 
96
123
  collector.on("collect", async (interaction) => {
97
- if (interaction.customId !== "previousButton" && interaction.customId !== "continueButton") {
124
+ if (interaction.customId !== this._previousId && interaction.customId !== this._nextId) {
98
125
  return;
99
126
  }
100
127
 
@@ -103,7 +130,7 @@ class Paginator {
103
130
  return;
104
131
  }
105
132
 
106
- if (interaction.customId === "previousButton") {
133
+ if (interaction.customId === this._previousId) {
107
134
  await interaction.deferUpdate();
108
135
  this._page = this._page <= 0 ? this.pages.length - 1 : this._page - 1;
109
136
  await this._update(this.pages[this._page]);
@@ -118,6 +145,8 @@ class Paginator {
118
145
  collector.on("end", async () => {
119
146
  await this.#disableMessageComponents();
120
147
  });
148
+
149
+ return collector;
121
150
  }
122
151
  }
123
152
 
@@ -0,0 +1,91 @@
1
+ import { InteractionCollector, Message, TextBasedChannel, User } from "discord.js";
2
+ import type { ColorResolvable, ComponentsV2Builder, V2BuiltPayload, V2ButtonObjectOptions } from "../builders/ComponentsV2Builder.js";
3
+ import type { EmojiInput } from "../builders/components/helpers.js";
4
+ import type { Context } from "../Context.js";
5
+ import type { V2View, V2ViewAttachOptions, V2ViewEndMode, V2ViewSendOptions, V2ViewThirdPartyMode } from "./V2View.js";
6
+
7
+ export interface V2PaginatorButtonConfig {
8
+ label?: string;
9
+ emoji?: EmojiInput;
10
+ style?: V2ButtonObjectOptions["style"];
11
+ }
12
+
13
+ export interface V2PaginatorPageContext<T = any> {
14
+ page: T;
15
+ pageIndex: number;
16
+ pageNumber: number;
17
+ totalPages: number;
18
+ totalItems: number;
19
+ startIndex: number;
20
+ items?: T[];
21
+ controls: V2ButtonObjectOptions[];
22
+ view: V2View<{ pageIndex: number }>;
23
+ state: { pageIndex: number };
24
+ isEnded: boolean;
25
+ isEphemeral: boolean;
26
+ prevId: string;
27
+ nextId: string;
28
+ closeId: string;
29
+ }
30
+
31
+ export interface V2PaginatorOptions<T = any> {
32
+ pages?: T[];
33
+ items?: T[];
34
+ perPage?: number;
35
+ totalItems?: number;
36
+ initialPage?: number;
37
+ wrap?: boolean;
38
+ idPrefix?: string;
39
+ author?: User;
40
+ authorId?: string;
41
+ ownerId?: string;
42
+ timeout?: number;
43
+ time?: number;
44
+ timeoutSeconds?: number;
45
+ idle?: number;
46
+ thirdPartyMode?: V2ViewThirdPartyMode;
47
+ thirdPartyMessage?: string;
48
+ onEnd?: V2ViewEndMode;
49
+ onEndHandler?: (info: { view: V2View<{ pageIndex: number }>; message: Message | null; reason: string }) => unknown;
50
+ isEphemeral?: boolean;
51
+ color?: ColorResolvable;
52
+ accentColor?: ColorResolvable;
53
+ title?: string;
54
+ subtitle?: string;
55
+ thumbnail?: string | null;
56
+ numbered?: boolean;
57
+ emptyText?: string;
58
+ footer?: string;
59
+ renderItem?: (item: T, index: number) => string;
60
+ renderPage?: (ctx: V2PaginatorPageContext<T>) => ComponentsV2Builder | V2BuiltPayload;
61
+ appendControlsToPrebuilt?: boolean;
62
+ alwaysShowControls?: boolean;
63
+ outsideControls?: boolean;
64
+ showPageIndicator?: boolean;
65
+ showFirstLast?: boolean;
66
+ pageIndicatorFormatter?: (page: number, totalPages: number) => string;
67
+ prevButton?: V2PaginatorButtonConfig;
68
+ nextButton?: V2PaginatorButtonConfig;
69
+ firstButton?: V2PaginatorButtonConfig;
70
+ lastButton?: V2PaginatorButtonConfig;
71
+ closeButton?: boolean | V2PaginatorButtonConfig;
72
+ closeReason?: string;
73
+ }
74
+
75
+ export declare class V2Paginator<T = any> {
76
+ constructor(options: V2PaginatorOptions<T>);
77
+
78
+ readonly view: V2View<{ pageIndex: number }>;
79
+ readonly pageIndex: number;
80
+ readonly totalPages: number;
81
+ readonly collector: InteractionCollector<any> | null;
82
+ readonly message: Message | null;
83
+
84
+ toPayload(options?: { ephemeral?: boolean }): V2BuiltPayload;
85
+ attach(message: Message | any, collectorOptions?: V2ViewAttachOptions): InteractionCollector<any>;
86
+ stop(reason?: string): V2View<{ pageIndex: number }>;
87
+ send(
88
+ target: Context | TextBasedChannel,
89
+ sendOptions?: V2ViewSendOptions
90
+ ): Promise<{ view: V2View<{ pageIndex: number }>; message: Message | null; collector: InteractionCollector<any> | null }>;
91
+ }