feat: add make a choice into dropdown

This commit is contained in:
Jozef Steinhübl 2024-03-18 15:59:16 +01:00
parent ee32db2f4d
commit c62cfa2438
No known key found for this signature in database
GPG key ID: E944BC293F5FF7E7
6 changed files with 38 additions and 15 deletions

View file

@ -41,6 +41,16 @@ new Component({
: // support for legacy select menus : // support for legacy select menus
ctx.interaction.data.custom_id; 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) const content = !ctx.interaction.member?.roles.includes(roleId)
? `Gave the <@&${roleId}> role!` ? `Gave the <@&${roleId}> role!`
: `Removed the <@&${roleId}> role!`; : `Removed the <@&${roleId}> role!`;

View file

@ -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}`, `roles-bot-setup:${ctx.interaction.guild_id}`,
); );
if (!rawData) if (!rawData)
@ -141,7 +141,7 @@ new Component({
const interaction = const interaction =
ctx.interaction as APIMessageComponentSelectMenuInteraction; ctx.interaction as APIMessageComponentSelectMenuInteraction;
const rawData = await REDIS.get( const rawData: string | null = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`, `roles-bot-setup:${ctx.interaction.guild_id}`,
); );
if (!rawData) 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) if (!rawData)
return ctx.respond({ return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource, type: InteractionResponseType.ChannelMessageWithSource,

View file

@ -18,7 +18,7 @@ new Modal({
id: "setup:part-roles-lpe", id: "setup:part-roles-lpe",
acknowledge: false, acknowledge: false,
run: async (ctx) => { run: async (ctx) => {
const rawData = await REDIS.get( const rawData: string | null = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`, `roles-bot-setup:${ctx.interaction.guild_id}`,
); );
if (!rawData) if (!rawData)
@ -95,7 +95,7 @@ new Modal({
id: "setup:part-messageContent", id: "setup:part-messageContent",
acknowledge: false, acknowledge: false,
run: async (ctx) => { run: async (ctx) => {
const rawData = await REDIS.get( const rawData: string | null = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`, `roles-bot-setup:${ctx.interaction.guild_id}`,
); );
if (!rawData) if (!rawData)
@ -168,7 +168,7 @@ new Modal({
id: "setup:part-webhook", id: "setup:part-webhook",
acknowledge: false, acknowledge: false,
run: async (ctx) => { run: async (ctx) => {
const rawData = await REDIS.get( const rawData: string | null = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`, `roles-bot-setup:${ctx.interaction.guild_id}`,
); );
if (!rawData) if (!rawData)

View file

@ -11,7 +11,9 @@ import { decodeFromString } from "serialize";
import { BasicData } from "../types"; import { BasicData } from "../types";
export default async function (data: BasicData, ctx: Context, rawRole: string) { 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) if (!rolesRaw)
return ctx.respond({ return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource, type: InteractionResponseType.ChannelMessageWithSource,

View file

@ -2,7 +2,6 @@ import {
APIActionRowComponent, APIActionRowComponent,
APIEmbed, APIEmbed,
APIMessageActionRowComponent, APIMessageActionRowComponent,
ButtonStyle,
InteractionResponseType, InteractionResponseType,
MessageFlags, MessageFlags,
RouteBases, RouteBases,
@ -65,11 +64,18 @@ export default async function (ctx: Context, data: Data) {
} }
const components: APIActionRowComponent<APIMessageActionRowComponent>[] = []; const components: APIActionRowComponent<APIMessageActionRowComponent>[] = [];
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) { for (const items of array) {
const actionRow = new ActionRowBuilder(); 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) { for (const item of items) {
switch (data.selecting) { switch (data.selecting) {

View file

@ -1,7 +1,5 @@
// from https://gist.github.com/devsnek/77275f6e3f810a9545440931ed314dc1 // from https://gist.github.com/devsnek/77275f6e3f810a9545440931ed314dc1
"use strict";
import type { Env } from "../types"; import type { Env } from "../types";
function hex2bin(hex: string) { function hex2bin(hex: string) {
@ -14,13 +12,18 @@ function hex2bin(hex: string) {
const encoder = new TextEncoder(); 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) { export async function verify(request: Request, env: Env) {
const subtle = await crypto.subtle.importKey( const subtle = await crypto.subtle.importKey(
"raw", "raw",
hex2bin(env.publicKey), hex2bin(env.publicKey),
{ {
name: typeof MINIFLARE !== "undefined" ? "Ed25519" : "NODE-ED25519", name: getAlgorithm(),
namedCurve: typeof MINIFLARE !== "undefined" ? "Ed25519" : "NODE-ED25519", namedCurve: getAlgorithm(),
}, },
true, true,
["verify"], ["verify"],
@ -32,7 +35,7 @@ export async function verify(request: Request, env: Env) {
const unknown = await request.clone().text(); const unknown = await request.clone().text();
return await crypto.subtle.verify( return await crypto.subtle.verify(
typeof MINIFLARE !== "undefined" ? "Ed25519" : "NODE-ED25519", getAlgorithm(),
subtle, subtle,
signature, signature,
encoder.encode(timestamp + unknown), encoder.encode(timestamp + unknown),