3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-06-08 00:05:01 +00:00

Merge branch '240811_application_commands_merge_2' into next

This commit is contained in:
Dragory 2024-08-11 22:28:41 +03:00
commit 43b8017985
No known key found for this signature in database
279 changed files with 6192 additions and 3044 deletions

View file

@ -1,4 +1,5 @@
import {
ChatInputCommandInteraction,
Client,
Message,
MessageCreateOptions,
@ -6,9 +7,9 @@ import {
MessageReaction,
PartialMessageReaction,
PartialUser,
TextBasedChannel,
User,
} from "discord.js";
import { sendContextResponse } from "../pluginUtils.js";
import { MINUTES, noop } from "../utils.js";
import { Awaitable } from "./typeUtils.js";
import Timeout = NodeJS.Timeout;
@ -27,14 +28,14 @@ const defaultOpts: PaginateMessageOpts = {
export async function createPaginatedMessage(
client: Client,
channel: TextBasedChannel | User,
context: Message | User | ChatInputCommandInteraction,
totalPages: number,
loadPageFn: LoadPageFn,
opts: Partial<PaginateMessageOpts> = {},
): Promise<Message> {
const fullOpts = { ...defaultOpts, ...opts } as PaginateMessageOpts;
const firstPageContent = await loadPageFn(1);
const message = await channel.send(firstPageContent);
const message = await sendContextResponse(context, firstPageContent);
let page = 1;
let pageLoadId = 0; // Used to avoid race conditions when rapidly switching pages

View file

@ -5,7 +5,7 @@ import { validateNoObjectAliases } from "./validateNoObjectAliases.js";
* Loads a YAML file safely while removing object anchors/aliases (including arrays)
*/
export function loadYamlSafely(yamlStr: string): any {
let loaded = yaml.safeLoad(yamlStr);
let loaded = yaml.load(yamlStr);
if (loaded == null || typeof loaded !== "object") {
loaded = {};
}

View file

@ -0,0 +1,20 @@
import { AttachmentSlashCommandOption, slashOptions } from "knub";
type AttachmentSlashOptions = Omit<AttachmentSlashCommandOption, "type" | "resolveValue" | "getExtraAPIProps">;
export function generateAttachmentSlashOptions(amount: number, options: AttachmentSlashOptions) {
return new Array(amount).fill(0).map((_, i) => {
return slashOptions.attachment({
name: amount > 1 ? `${options.name}${i + 1}` : options.name,
description: options.description,
required: options.required ?? false,
});
});
}
export function retrieveMultipleOptions(amount: number, options: any, name: string) {
return new Array(amount)
.fill(0)
.map((_, i) => options[amount > 1 ? `${name}${i + 1}` : name])
.filter((a) => a);
}

View file

@ -2,22 +2,26 @@ import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
GuildTextBasedChannel,
ChatInputCommandInteraction,
Message,
MessageActionRowComponentBuilder,
MessageComponentInteraction,
MessageCreateOptions,
User,
} from "discord.js";
import moment from "moment";
import { v4 as uuidv4 } from "uuid";
import { isContextInteraction } from "../pluginUtils.js";
import { noop } from "../utils.js";
export async function waitForButtonConfirm(
channel: GuildTextBasedChannel,
toPost: MessageCreateOptions,
context: Message | User | ChatInputCommandInteraction,
toPost: Omit<MessageCreateOptions, "flags">,
options?: WaitForOptions,
): Promise<boolean> {
return new Promise(async (resolve) => {
const idMod = `${channel.guild.id}-${moment.utc().valueOf()}`;
const contextIsInteraction = isContextInteraction(context);
const idMod = `${context.id}-${moment.utc().valueOf()}`;
const row = new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents([
new ButtonBuilder()
.setStyle(ButtonStyle.Success)
@ -29,7 +33,15 @@ export async function waitForButtonConfirm(
.setLabel(options?.cancelText || "Cancel")
.setCustomId(`cancelButton:${idMod}:${uuidv4()}`),
]);
const message = await channel.send({ ...toPost, components: [row] });
const sendMethod = () => {
if (contextIsInteraction) {
return context.replied ? context.editReply.bind(context) : context.reply.bind(context);
} else {
return "send" in context ? context.send.bind(context) : context.channel.send.bind(context.channel);
}
};
const extraParameters = contextIsInteraction ? { fetchReply: true, ephemeral: true } : {};
const message = (await sendMethod()({ ...toPost, components: [row], ...extraParameters })) as Message;
const collector = message.createMessageComponentCollector({ time: 10000 });
@ -41,16 +53,16 @@ export async function waitForButtonConfirm(
.catch((err) => console.trace(err.message));
} else {
if (interaction.customId.startsWith(`confirmButton:${idMod}:`)) {
message.delete();
if (!contextIsInteraction) message.delete();
resolve(true);
} else if (interaction.customId.startsWith(`cancelButton:${idMod}:`)) {
message.delete();
if (!contextIsInteraction) message.delete();
resolve(false);
}
}
});
collector.on("end", () => {
if (message.deletable) message.delete().catch(noop);
if (!contextIsInteraction && message.deletable) message.delete().catch(noop);
resolve(false);
});
});