Skip to content

Commit

Permalink
push this for later
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpleCreativity committed Jan 17, 2025
1 parent 2e73c73 commit 4dcb5c6
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/classes/Signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ export default class Signal<Params extends unknown[]> {
});
});
}

get connectionCount(): number {
return this.connections.length;
}
}
70 changes: 52 additions & 18 deletions src/classes/components/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MessageFlags,
} from "discord.js";
import client from "../../main.js";
import Signal from "../Signal.js";

export type ButtonOptions = {
label: string;
Expand All @@ -16,15 +17,24 @@ export type ButtonOptions = {
customId?: string;
allowedUsers?: string[];

function?(interaction: ButtonInteraction): unknown | Promise<unknown>;
function?(interaction: ButtonInteraction): void | Promise<void>;
};

export default class Button extends ButtonBuilder {
customId: string;

private events: {
pressed: Signal<[ButtonInteraction]>;
};
private buttonInteractionListener: (buttonInteraction: ButtonInteraction) => void;

constructor(options: ButtonOptions) {
super();

this.events = {
pressed: new Signal(),
};

this.setLabel(options.label);
this.setStyle(options.style);
this.setDisabled(options.disabled ?? false);
Expand All @@ -34,23 +44,47 @@ export default class Button extends ButtonBuilder {
this.setCustomId(this.customId);

if (options.function) {
client.on("buttonInteraction", async (interaction: ButtonInteraction) => {
if (!options.function) return;
if (interaction.customId !== this.customId) return;
if (
options.allowedUsers &&
options.allowedUsers.length > 0 &&
!options.allowedUsers.includes(interaction.user.id)
) {
await interaction.reply({
content: "You are not allowed to use this button",
flags: MessageFlags.Ephemeral,
});
return;
}

return await options.function(interaction);
});
this.events.pressed.connect(options.function);
}

this.buttonInteractionListener = async (interaction: ButtonInteraction) => {
console.log(
"total connections",
this.events.pressed.connectionCount,
client.listenerCount("buttonInteraction"),
);
console.log("buttonInteractionListener", new Date().toISOString().replace("T", " ").replace("Z", ""));
console.log("listeners", client.listeners("buttonInteraction"));

if (interaction.customId !== this.customId) return;
if (
options.allowedUsers &&
options.allowedUsers.length > 0 &&
!options.allowedUsers.includes(interaction.user.id)
) {
await interaction.reply({
content: "You are not allowed to use this button",
flags: MessageFlags.Ephemeral,
});
return;
}

this.events.pressed.fire(interaction);
};

client.on("buttonInteraction", this.buttonInteractionListener);
}

onPressed(listener: (interaction: ButtonInteraction) => void | Promise<void>): void {
this.events.pressed.on(listener);
}

oncePressed(listener: (interaction: ButtonInteraction) => void | Promise<void>): void {
this.events.pressed.once(listener);
}

destroy(): void {
this.events.pressed.disconnectAll();
client.off("buttonInteraction", this.buttonInteractionListener);
}
}
6 changes: 4 additions & 2 deletions src/classes/components/PageEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ export default class PageEmbed extends ButtonEmbed {
const lookValue = response.fields.getTextInputValue("field_value");

if (lookName.length === 0 && lookValue.length === 0) {
return await response.editReply({
await response.editReply({
embeds: [
client.Functions.makeErrorEmbed({
title: "An error occured while searching",
description: "You must either pass trough a `field_name` or `field_value`!",
}),
],
});
return;
}

// forEach is the devil
Expand Down Expand Up @@ -192,7 +193,8 @@ export default class PageEmbed extends ButtonEmbed {
}),
);

return await response.editReply(buttonEmbed.getMessageData());
await response.editReply(buttonEmbed.getMessageData());
return;
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/classes/components/QuestionEmbed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
import { AnySelectMenuInteraction, ButtonInteraction, ButtonStyle, ChatInputCommandInteraction } from "discord.js";
import Button from "./Button.js";
import ButtonEmbed from "./ButtonEmbed.js";
import type Embed from "./Embed.js";
import Emojis from "../../../public/Emojis.json" with { type: "json" };
export type QuestionEmbedOptions = {
baseEmbed: Embed;
allowedUsers?: string[];
yesButtonLabel?: string;
noButtonLabel?: string;
};
export enum QuestionEmbedResponse {
Yes = 0,
No = 1,
}
export default class QuestionEmbed extends ButtonEmbed {
yesButton: Button;
noButton: Button;
constructor(options: QuestionEmbedOptions) {
super(options.baseEmbed);
this.yesButton = this.addButton(
new Button({
label: options.yesButtonLabel ?? "Yes",
emoji: Emojis.check2,
style: ButtonStyle.Success,
allowedUsers: options.allowedUsers,
}),
);
this.noButton = this.addButton(
new Button({
label: options.noButtonLabel ?? "No",
emoji: Emojis.close2,
style: ButtonStyle.Success,
allowedUsers: options.allowedUsers,
}),
);
}
async Prompt(
interaction: ChatInputCommandInteraction | ButtonInteraction | AnySelectMenuInteraction,
embed: Embed,
) {
return new Promise((resolve, reject) => {
})
}
}
*/
15 changes: 11 additions & 4 deletions src/classes/components/StringSelectMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "discord.js";
import Emojis from "../../../public/Emojis.json" with { type: "json" };
import client from "../../main.js";
import Signal from "../Signal.js";
import Button from "./Button.js";
import ButtonEmbed from "./ButtonEmbed.js";
import type Embed from "./Embed.js";
Expand All @@ -39,6 +40,9 @@ export default class StringSelectMenu {
options: StringSelectMenuOptionBuilder[];
allowedUsers: string[];

private selectMenuSignal: Signal<[AnySelectMenuInteraction]>;
private buttonSignal: Signal<[ButtonInteraction]>;

constructor(options: StringSelectMenuOptions) {
this.selector = new StringSelectMenuBuilder()
.setPlaceholder(options.placeholder)
Expand All @@ -49,6 +53,9 @@ export default class StringSelectMenu {

this.allowedUsers = options.allowedUsers || [];
this.options = options.options;

this.selectMenuSignal = new Signal();
this.buttonSignal = new Signal();
}

getSelector() {
Expand All @@ -57,7 +64,7 @@ export default class StringSelectMenu {
return new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(this.selector);
}

async prompt(
async Prompt(
interaction: ChatInputCommandInteraction | ButtonInteraction | AnySelectMenuInteraction,
embed: Embed,
) {
Expand Down Expand Up @@ -97,11 +104,11 @@ export default class StringSelectMenu {
// @ts-ignore
for (const row of buttonEmbed.rows) components.push({ type: ComponentType.ActionRow, components: row });

client.on("selectMenu", async (newInteraction: AnySelectMenuInteraction) => {
this.selectMenuSignal.connect(async (newInteraction: AnySelectMenuInteraction) => {
if (!newInteraction.isStringSelectMenu()) return;
if (newInteraction.customId !== this.selector.data.custom_id) return;
if (this.allowedUsers.length > 0 && !this.allowedUsers.includes(newInteraction.user.id)) {
await newInteraction.reply({
newInteraction.reply({
content: "You are not allowed to use this menu",
flags: MessageFlags.Ephemeral,
});
Expand All @@ -119,7 +126,7 @@ export default class StringSelectMenu {
});

return new Promise((resolve, reject) => {
client.on("buttonPress", async (newInteraction: ButtonInteraction) => {
this.buttonSignal.connect(async (newInteraction: ButtonInteraction) => {
if (
newInteraction.customId !== submitButton.customId &&
newInteraction.customId !== cancelButton.customId
Expand Down
19 changes: 17 additions & 2 deletions src/interactables/points/getPoints.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UserAvatarHeadshotImageSize, type UserData } from "bloxwrap";
import {
type APIEmbedField,
type ButtonInteraction,
ButtonStyle,
type ChatInputCommandInteraction,
MessageFlags,
Expand Down Expand Up @@ -47,7 +48,7 @@ const callback = async (
inline: false,
});

buttonEmbed.addButton(
const testbutton = buttonEmbed.addButton(
new Button({
label: "View Notes",
emoji: Emojis.folder_open,
Expand Down Expand Up @@ -95,11 +96,25 @@ const callback = async (
}),
);

return await buttonInteraction.editReply(pageEmbed.getMessageData());
await buttonInteraction.editReply(pageEmbed.getMessageData());
return;
},
}),
);

testbutton.onPressed((buttonInteraction: ButtonInteraction) => {
console.log("Still connected.");
});

setTimeout(() => {
testbutton.destroy();
console.log("destroyed");

console.log("total connections", client.listenerCount("buttonInteraction"));
console.log("buttonInteractionListener", new Date().toISOString().replace("T", " ").replace("Z", ""));
console.log("listeners", client.listeners("buttonInteraction"));
}, 60000);

buttonEmbed.addButton(
new Button({
label: "View Ranklock data",
Expand Down
6 changes: 4 additions & 2 deletions src/interactables/points/pointlogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export default new SlashCommand({
try {
await pointlog.save();

return await interaction.editReply({
await interaction.editReply({
embeds: [
client.Functions.makeSuccessEmbed({
title: "Pointlog created",
Expand All @@ -393,6 +393,7 @@ export default new SlashCommand({
],
components: [],
});
return;
} catch (error) {
const message: string =
error && typeof error === "object" && "message" in error
Expand All @@ -418,7 +419,7 @@ export default new SlashCommand({

const userBuffer = Buffer.from(userText, "utf-8");

return await interaction.editReply({
await interaction.editReply({
embeds: [
client.Functions.makeErrorEmbed({
title: "Pointlog creation failure",
Expand All @@ -428,6 +429,7 @@ export default new SlashCommand({
files: [{ name: `pointlog_${pointlog.id}_fulldata.txt`, attachment: userBuffer }],
components: [],
});
return;
}
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/interactables/user/linkRoblox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default new SlashCommand({

code = generateCode();
buttonEmbed.embed.setDescription(
`Hi, ${interaction.user.username}! To make sure you're the owner of the account, please set your [roblox profile](https://www.roblox.com/users/${robloxUser.id}/profile) description to the following:\n\n\`\`\`${code}\`\`\`\n\nOnce you have done this, click the button below to link your account.`
`Hi, ${interaction.user.username}! To make sure you're the owner of the account, please set your [roblox profile](https://www.roblox.com/users/${robloxUser.id}/profile) description to the following:\n\n\`\`\`${code}\`\`\`\n\nOnce you have done this, click the button below to link your account.`,
);

await buttonInteraction.editReply(buttonEmbed.getMessageData());
Expand Down

0 comments on commit 4dcb5c6

Please sign in to comment.