Skip to content

Commit d21ed6d

Browse files
TheArmaganErdemGKSL
andcommitted
💦 POG | Tam Button Ve Menü Desteği
Tam Button Ve Menü Desteği Erdem'e Teşekkürler! Co-Authored-By: Erdem <[email protected]>
1 parent 9f37459 commit d21ed6d

File tree

9 files changed

+121
-42
lines changed

9 files changed

+121
-42
lines changed

interactions/butonYolla.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
const { MessageButton, MessageActionRow, MessageSelectMenu } = require("discord.js");
2-
31
module.exports = new Underline.SlashCommand({
42
name: ["buton-yolla"],
53
description: "Buton yollar",
64
async onInteraction(inter, other) {
7-
let button = new MessageButton().setLabel("hi").setStyle("PRIMARY").setCustomId("ornek");
8-
let menu = new MessageSelectMenu().setCustomId("ornek").setMinValues(1).setMaxValues(1).setOptions([
9-
{
10-
label: "hi",
11-
value: "sa"
12-
},
13-
{
14-
label: "hello",
15-
value: "as"
16-
},
17-
])
18-
let row = new MessageActionRow().addComponents(button)
19-
let row2 = new MessageActionRow().addComponents(menu)
205
inter.reply({
21-
content: "Kebabu",
6+
content: "Düğme!",
227
components: [
23-
row
24-
]
25-
})
26-
if(!inter.channel) await inter.user.createDM();
27-
inter.channel.send({
28-
content: "bö",
29-
components: [
30-
row2
8+
{
9+
type: "ACTION_ROW",
10+
components: [
11+
Underline.interactions.get("basBana").toJSON()
12+
]
13+
}
3114
]
3215
})
3316
},

interactions/buttons/basBana.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = new Underline.Button({
2+
id: "basBana",
3+
name: "basBana",
4+
description: "...",
5+
onInteraction(inter, other) {
6+
inter.reply("Evet")
7+
inter.message.edit({
8+
components: [
9+
{
10+
type: "ACTION_ROW",
11+
components: [
12+
{
13+
...this.toJSON(),
14+
label: `${Math.floor(1000 + Math.random() * 999)}`
15+
}
16+
]
17+
}
18+
]
19+
})
20+
},
21+
options: {
22+
style: "DANGER",
23+
label: "Rastgele Sayı!",
24+
emoji: "💥"
25+
},
26+
guildOnly: true,
27+
developerOnly: false
28+
});

types/Button.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
const { MessageButton } = require("discord.js");
12
const Interaction = require("./Interaction");
23

34
class Button extends Interaction {
4-
55
/** @param {Interaction.TOmittedInteraction & Interaction.Button} arg */
66
constructor (arg = { }) {
77
super({
@@ -10,6 +10,18 @@ class Button extends Interaction {
1010
...arg
1111
})
1212
}
13+
isButton() { return true; }
14+
toJSON() {
15+
let button = new MessageButton().setCustomId(this.name[0]).setStyle(this.options.style)
16+
if (this.options.emoji) button.setEmoji(this.options.emoji);
17+
if (this.options.label) button.setLabel(this.options.label);
18+
if (this.options.url) button.setURL(this.options.url);
19+
return button;
20+
}
21+
isSelectMenu() { return false; }
22+
isChatActionCommand() { return false; }
23+
isUserActionCommand() { return false; }
24+
isMessageActionCommand() { return false; }
1325
}
1426

1527
module.exports = Button;

types/Interaction.d.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,41 @@ import {
1313
PermissionString,
1414
SelectMenuInteraction,
1515
MessageSelectOptionData,
16-
ButtonInteraction
16+
ButtonInteraction,
17+
MessageSelectMenu,
18+
MessageButton,
19+
MessageButtonStyleResolvable,
20+
EmojiResolvable
1721
} from "discord.js";
1822

23+
interface CustomSelectMenuOptions {
24+
min?: number;
25+
max?: number;
26+
choices: MessageSelectOptionData[];
27+
placeholder?: string;
28+
onComplete?(interaction: SelectMenuInteraction): MessageSelectOptionData[]
29+
}
30+
31+
interface CustomButtonOptions {
32+
emoji?: EmojiResolvable;
33+
label?: string;
34+
style: MessageButtonStyleResolvable;
35+
url?: string;
36+
}
37+
1938
export type CustomApplicationCommandOptionData = (
2039
ApplicationCommandNonOptionsData
2140
| ApplicationCommandChannelOptionData
2241
| ApplicationCommandChoicesData
23-
) & { onComplete(interaction: AutocompleteInteraction, value: string|number): Promise<ApplicationCommandOptionChoice[]> }
42+
) & { onComplete(interaction: AutocompleteInteraction, value: string | number): ApplicationCommandOptionChoice[] }
2443

2544
export class BaseInteraction {
2645
private _type: string;
2746
name: string[];
2847
id?: string;
2948
perms?: { bot: PermissionString[], user: PermissionString[] };
30-
onInteraction: (interaction: CommandInteraction | ContextMenuInteraction, other: IOther ) => void;
49+
onInteraction(interaction: CommandInteraction | ContextMenuInteraction, other: IOther ): void;
50+
toJSON(): MessageButton | MessageSelectMenu | undefined;
3151
onLoad?(client: Client): void;
3252
coolDowns: Map<string, number>;
3353
description!: string;
@@ -38,12 +58,17 @@ export class BaseInteraction {
3858
guildOnly?: boolean;
3959
options?: CustomApplicationCommandOptionData[];
4060
defaultPermission?: boolean;
41-
actionType?: ApplicationCommandType;
42-
constructor(arg: TInteractionConstructor)
61+
actionType?: ApplicationCommandType | "SELECT_MENU" | "BUTTON";
62+
isSelectMenu(): this is import("./SelectMenu");
63+
isButton(): this is import("./Button");
64+
isChatActionCommand(): this is import("./SlashCommand");
65+
isUserActionCommand(): this is import("./UserAction");
66+
isMessageActionCommand(): this is import("./MessageAction");
67+
constructor(arg: TInteractionConstructor);
4368
}
4469

45-
export type TOmittedInteraction = Omit<BaseInteraction, "_type" | "coolDowns" | "name" | "onInteraction" | "actionType" | "options">;
46-
export type TInteractionConstructor = TOmittedInteraction & ((ActionChatCommand | ActionRightClickCommand) & SelectMenu);
70+
export type TOmittedInteraction = Omit<BaseInteraction, "_type" | "coolDowns" | "name" | "onInteraction" | "actionType" | "options" | "toJSON">;
71+
export type TInteractionConstructor = TOmittedInteraction & ((ActionChatCommand | ActionRightClickCommand | SelectMenu | Button));
4772

4873
export interface IOther {
4974
setCoolDown(durations: number): void,
@@ -53,29 +78,33 @@ export interface IOther {
5378
export interface ActionChatCommand {
5479
name: string[];
5580
actionType: "CHAT_INPUT";
56-
onInteraction: (interaction: CommandInteraction, other: IOther) => void;
81+
onInteraction(interaction: CommandInteraction, other: IOther): void;
5782
options: CustomApplicationCommandOptionData[];
83+
toJSON(): undefined;
5884
}
5985

6086
export interface ActionRightClickCommand {
6187
name: string;
6288
actionType: "MESSAGE" | "USER";
63-
onInteraction: (interaction: ContextMenuInteraction, other: IOther) => void;
89+
onInteraction(interaction: ContextMenuInteraction, other: IOther): void;
6490
options: undefined;
91+
toJSON(): undefined;
6592
}
6693

6794
export interface SelectMenu {
6895
name: string;
6996
actionType: "SELECT_MENU";
70-
onInteraction: (interaction: SelectMenuInteraction, other: IOther) => void;
71-
options?: MessageSelectOptionData[]
97+
onInteraction(interaction: SelectMenuInteraction, other: IOther): void;
98+
options?: CustomSelectMenuOptions;
99+
toJSON(): MessageSelectMenu;
72100
}
73101

74102
export interface Button {
75103
name: string;
76104
actionType: "BUTTON";
77-
onInteraction: (interaction: ButtonInteraction, other: IOther) => void;
78-
options?: undefined;
105+
onInteraction(interaction: ButtonInteraction, other: IOther): void;
106+
options?: CustomButtonOptions;
107+
toJSON(): MessageButton;
79108
}
80109

81110
export = BaseInteraction;

types/Interaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Interaction {
5353
this.developerOnly = Boolean(arg.developerOnly ?? Underline.config.interactionDefaults.developerOnly);
5454
this.other = defaultify(typeof arg.other == "object" ? arg.other : {}, Underline.config.interactionDefaults.other);
5555
this.coolDown = typeof arg.coolDown == "number" ? arg.coolDown : Underline.config.interactionDefaults.coolDown;
56-
this.options = Array.isArray(arg.options) ? arg.options : Underline.config.interactionDefaults.options;
56+
this.options = arg.options;
5757
this.defaultPermission = Boolean(arg.defaultPermission ?? Underline.config.interactionDefaults.defaultPermission);
5858
}
5959
}

types/MessageAction.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ class MessageAction extends Interaction {
88
super({
99
type: "COMMAND",
1010
actionType: "MESSAGE",
11-
name: [arg.name],
1211
...arg
1312
})
1413
}
14+
isMessageActionCommand() { return true; }
15+
isSelectMenu() { return false; }
16+
isButton() { return false; }
17+
isChatActionCommand() { return false; }
18+
isUserActionCommand() { return false; }
19+
toJSON() {}
1520
}
1621

1722
module.exports = MessageAction;

types/SelectMenu.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
const { MessageSelectMenu } = require("discord.js");
12
const Interaction = require("./Interaction");
23

34
class SelectMenu extends Interaction {
4-
55
/** @param {Interaction.TOmittedInteraction & Interaction.SelectMenu} arg */
66
constructor (arg = {}) {
77
super({
@@ -10,6 +10,17 @@ class SelectMenu extends Interaction {
1010
...arg
1111
})
1212
}
13+
isSelectMenu() { return true; }
14+
isButton() { return false; }
15+
isChatActionCommand() { return false; }
16+
isUserActionCommand() { return false; }
17+
isMessageActionCommand() { return false; }
18+
toJSON() {
19+
let menu = new MessageSelectMenu().addOptions( this.options?.onComplete ? this.options.onComplete() : (this.options?.choices ?? []) )
20+
.setMinValues(this.options.min ?? 1).setMaxValues(this.options.max ?? this.options.choices.length).setCustomId(this.name)
21+
if(this.options.placeholder) menu.setPlaceholder(this.options.placeholder)
22+
return menu;
23+
}
1324
}
1425

1526
module.exports = SelectMenu;

types/SlashCommand.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class SlashCommand extends Interaction {
1010
...arg
1111
})
1212
}
13+
isChatActionCommand() { return true; }
14+
isSelectMenu() { return false; }
15+
isButton() { return false; }
16+
isUserActionCommand() { return false; }
17+
isMessageActionCommand() { return false; }
18+
toJSON() {}
1319
}
1420

1521
module.exports = SlashCommand;

types/UserAction.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ class MessageAction extends Interaction {
88
super({
99
type: "COMMAND",
1010
actionType: "USER",
11-
name: [arg.name],
1211
...arg
1312
})
1413
}
14+
isUserActionCommand() { return true; }
15+
isSelectMenu() { return false; }
16+
isButton() { return false; }
17+
isChatActionCommand() { return false; }
18+
isMessageActionCommand() { return false; }
19+
toJSON() {}
1520
}
1621

1722
module.exports = MessageAction;

0 commit comments

Comments
 (0)