From c62cfa243882564e56f20508a4fa2056154134af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Mon, 18 Mar 2024 15:59:16 +0100 Subject: [PATCH] feat: add make a choice into dropdown --- packages/bot/src/components/select.ts | 10 ++++++++++ packages/bot/src/components/setup.ts | 8 +++++--- packages/bot/src/modals/setup.ts | 6 +++--- packages/bot/src/utils/returnRoleLpe.ts | 4 +++- packages/bot/src/utils/sendFinal.ts | 12 +++++++++--- packages/bot/src/utils/verify.ts | 13 ++++++++----- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/packages/bot/src/components/select.ts b/packages/bot/src/components/select.ts index 826ac20..363cd8a 100644 --- a/packages/bot/src/components/select.ts +++ b/packages/bot/src/components/select.ts @@ -41,6 +41,16 @@ new Component({ : // support for legacy select menus ctx.interaction.data.custom_id; + if (roleId === "nothing") { + return ctx.respond({ + type: InteractionResponseType.ChannelMessageWithSource, + data: { + content: "No role selected.", + flags: MessageFlags.Ephemeral, + }, + }); + } + const content = !ctx.interaction.member?.roles.includes(roleId) ? `Gave the <@&${roleId}> role!` : `Removed the <@&${roleId}> role!`; diff --git a/packages/bot/src/components/setup.ts b/packages/bot/src/components/setup.ts index a7b6f95..7de4035 100644 --- a/packages/bot/src/components/setup.ts +++ b/packages/bot/src/components/setup.ts @@ -82,7 +82,7 @@ new Component({ }, }); - const rawData = await REDIS.get( + const rawData: string | null = await REDIS.get( `roles-bot-setup:${ctx.interaction.guild_id}`, ); if (!rawData) @@ -141,7 +141,7 @@ new Component({ const interaction = ctx.interaction as APIMessageComponentSelectMenuInteraction; - const rawData = await REDIS.get( + const rawData: string | null = await REDIS.get( `roles-bot-setup:${ctx.interaction.guild_id}`, ); if (!rawData) @@ -233,7 +233,9 @@ new Component({ }, }); - const rawData = await REDIS.get(`roles-bot-setup:${ctx.guildId}`); + const rawData: string | null = await REDIS.get( + `roles-bot-setup:${ctx.guildId}`, + ); if (!rawData) return ctx.respond({ type: InteractionResponseType.ChannelMessageWithSource, diff --git a/packages/bot/src/modals/setup.ts b/packages/bot/src/modals/setup.ts index 377ba86..06953c6 100644 --- a/packages/bot/src/modals/setup.ts +++ b/packages/bot/src/modals/setup.ts @@ -18,7 +18,7 @@ new Modal({ id: "setup:part-roles-lpe", acknowledge: false, run: async (ctx) => { - const rawData = await REDIS.get( + const rawData: string | null = await REDIS.get( `roles-bot-setup:${ctx.interaction.guild_id}`, ); if (!rawData) @@ -95,7 +95,7 @@ new Modal({ id: "setup:part-messageContent", acknowledge: false, run: async (ctx) => { - const rawData = await REDIS.get( + const rawData: string | null = await REDIS.get( `roles-bot-setup:${ctx.interaction.guild_id}`, ); if (!rawData) @@ -168,7 +168,7 @@ new Modal({ id: "setup:part-webhook", acknowledge: false, run: async (ctx) => { - const rawData = await REDIS.get( + const rawData: string | null = await REDIS.get( `roles-bot-setup:${ctx.interaction.guild_id}`, ); if (!rawData) diff --git a/packages/bot/src/utils/returnRoleLpe.ts b/packages/bot/src/utils/returnRoleLpe.ts index b55e581..abd7804 100644 --- a/packages/bot/src/utils/returnRoleLpe.ts +++ b/packages/bot/src/utils/returnRoleLpe.ts @@ -11,7 +11,9 @@ import { decodeFromString } from "serialize"; import { BasicData } from "../types"; export default async function (data: BasicData, ctx: Context, rawRole: string) { - const rolesRaw = await REDIS.get(`roles-bot-setup-roles:${ctx.guildId}`); + const rolesRaw: string | null = await REDIS.get( + `roles-bot-setup-roles:${ctx.guildId}`, + ); if (!rolesRaw) return ctx.respond({ type: InteractionResponseType.ChannelMessageWithSource, diff --git a/packages/bot/src/utils/sendFinal.ts b/packages/bot/src/utils/sendFinal.ts index d62f3da..4312037 100644 --- a/packages/bot/src/utils/sendFinal.ts +++ b/packages/bot/src/utils/sendFinal.ts @@ -2,7 +2,6 @@ import { APIActionRowComponent, APIEmbed, APIMessageActionRowComponent, - ButtonStyle, InteractionResponseType, MessageFlags, RouteBases, @@ -65,11 +64,18 @@ export default async function (ctx: Context, data: Data) { } const components: APIActionRowComponent[] = []; - const array = splitArray(data.roleIds, data.selecting === "buttons" ? 5 : 25); + const array = splitArray(data.roleIds, data.selecting === "buttons" ? 5 : 24); for (const items of array) { const actionRow = new ActionRowBuilder(); - const selectMenu = new StringSelectMenuBuilder().setCustomId("select:role"); + const selectMenuNaiveOption = new StringSelectMenuOptionBuilder() + .setLabel("Make a choice") + .setValue("nothing") + .setDefault(true); + + const selectMenu = new StringSelectMenuBuilder() + .setCustomId("select:role") + .addOptions(selectMenuNaiveOption); for (const item of items) { switch (data.selecting) { diff --git a/packages/bot/src/utils/verify.ts b/packages/bot/src/utils/verify.ts index bf2adff..1b40847 100644 --- a/packages/bot/src/utils/verify.ts +++ b/packages/bot/src/utils/verify.ts @@ -1,7 +1,5 @@ // from https://gist.github.com/devsnek/77275f6e3f810a9545440931ed314dc1 -"use strict"; - import type { Env } from "../types"; function hex2bin(hex: string) { @@ -14,13 +12,18 @@ function hex2bin(hex: string) { const encoder = new TextEncoder(); +export function getAlgorithm() { + // @ts-expect-error + return typeof MINIFLARE !== "undefined" ? "Ed25519" : "NODE-ED25519"; +} + export async function verify(request: Request, env: Env) { const subtle = await crypto.subtle.importKey( "raw", hex2bin(env.publicKey), { - name: typeof MINIFLARE !== "undefined" ? "Ed25519" : "NODE-ED25519", - namedCurve: typeof MINIFLARE !== "undefined" ? "Ed25519" : "NODE-ED25519", + name: getAlgorithm(), + namedCurve: getAlgorithm(), }, true, ["verify"], @@ -32,7 +35,7 @@ export async function verify(request: Request, env: Env) { const unknown = await request.clone().text(); return await crypto.subtle.verify( - typeof MINIFLARE !== "undefined" ? "Ed25519" : "NODE-ED25519", + getAlgorithm(), subtle, signature, encoder.encode(timestamp + unknown),