fix(bot): make everything return respond

CF workers yep
This commit is contained in:
xHyroM 2023-04-09 16:52:30 +02:00
parent 857510e42e
commit cd9a5fdd90
No known key found for this signature in database
GPG key ID: BE0423F386C436AA
7 changed files with 235 additions and 140 deletions

View file

@ -2,6 +2,7 @@ import { Command } from "../structs/Command";
import {
APIRole,
ChannelType,
InteractionResponseType,
MessageFlags,
RouteBases,
Routes,
@ -13,11 +14,14 @@ import { encodeToHex } from "serialize";
// Part 1 ## select channel
new Command({
name: "setup",
flags: MessageFlags.Ephemeral,
acknowledge: false,
run: async (ctx) => {
if (!ctx.guildId)
return await ctx.editReply({
content: "Guild not found.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
},
});
// Delete the data if it exists
@ -42,20 +46,24 @@ new Command({
3600,
);
await ctx.editReply({
content: "Select the channel to which the panel will be sent.",
components: [
new ActionRowBuilder<ChannelSelectMenuBuilder>()
.addComponents(
new ChannelSelectMenuBuilder()
.setCustomId("setup:part-channel")
.addChannelTypes(
ChannelType.GuildAnnouncement,
ChannelType.GuildText,
),
)
.toJSON(),
],
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Select the channel to which the panel will be sent.",
components: [
new ActionRowBuilder<ChannelSelectMenuBuilder>()
.addComponents(
new ChannelSelectMenuBuilder()
.setCustomId("setup:part-channel")
.addChannelTypes(
ChannelType.GuildAnnouncement,
ChannelType.GuildText,
),
)
.toJSON(),
],
flags: MessageFlags.Ephemeral,
},
});
},
});

View file

@ -1,5 +1,6 @@
import {
ComponentType,
InteractionResponseType,
MessageFlags,
RouteBases,
Routes,
@ -9,16 +10,24 @@ import { Component } from "../structs/Component";
new Component({
id: "select:role",
default: true,
flags: MessageFlags.Ephemeral,
acknowledge: false,
run: async (ctx) => {
if (!ctx.guildId)
return ctx.editReply({
content: "Guild not found.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
flags: MessageFlags.Ephemeral,
},
});
if (!ctx.interaction.member)
return ctx.editReply({
content: "Member not found.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Member not found.",
flags: MessageFlags.Ephemeral,
},
});
const roleId =
@ -54,8 +63,12 @@ new Component({
},
);
await ctx.editReply({
content,
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content,
flags: MessageFlags.Ephemeral,
},
});
},
});

View file

@ -20,11 +20,14 @@ import sendFinal from "../utils/sendFinal";
// Part 2 Channels ## select button/dropdowns
new Component({
id: "setup:part-channel",
flags: MessageFlags.Ephemeral,
acknowledge: false,
run: async (ctx) => {
if (!ctx.interaction.guild_id)
return await ctx.editReply({
content: "Guild not found.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
},
});
const interaction =
@ -40,23 +43,27 @@ new Component({
600,
);
await ctx.editReply({
content:
"Choose whether you want to use buttons or dropdown menu (select menu).",
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel("Buttons")
.setCustomId("setup:part-selecting:buttons")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setLabel("Dropdowns")
.setCustomId("setup:part-selecting:dropdowns")
.setStyle(ButtonStyle.Primary),
)
.toJSON(),
],
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content:
"Choose whether you want to use buttons or dropdown menu (select menu).",
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel("Buttons")
.setCustomId("setup:part-selecting:buttons")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setLabel("Dropdowns")
.setCustomId("setup:part-selecting:dropdowns")
.setStyle(ButtonStyle.Primary),
)
.toJSON(),
],
flags: MessageFlags.Ephemeral,
},
});
},
});
@ -64,17 +71,27 @@ new Component({
// Part 3 Selecting ## select roles
new Component({
id: "setup:part-selecting",
flags: MessageFlags.Ephemeral,
acknowledge: false,
run: async (ctx) => {
if (!ctx.interaction.guild_id)
return await ctx.editReply({ content: "Guild not found." });
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
flags: MessageFlags.Ephemeral,
},
});
const rawData = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`,
);
if (!rawData)
return await ctx.editReply({
content: "Data not found. Try running setup again.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Data not found. Try running setup again.",
flags: MessageFlags.Ephemeral,
},
});
const data = decodeFromString(rawData);
@ -86,19 +103,23 @@ new Component({
600,
);
await ctx.editReply({
content: "Select the roles that will be available in the menu.",
components: [
new ActionRowBuilder<RoleSelectMenuBuilder>()
.addComponents(
new RoleSelectMenuBuilder()
.setCustomId("setup:part-roles")
.setPlaceholder("Select roles")
.setMinValues(1)
.setMaxValues(25),
)
.toJSON(),
],
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Select the roles that will be available in the menu.",
components: [
new ActionRowBuilder<RoleSelectMenuBuilder>()
.addComponents(
new RoleSelectMenuBuilder()
.setCustomId("setup:part-roles")
.setPlaceholder("Select roles")
.setMinValues(1)
.setMaxValues(25),
)
.toJSON(),
],
flags: MessageFlags.Ephemeral,
},
});
},
});
@ -109,7 +130,13 @@ new Component({
acknowledge: false,
run: async (ctx) => {
if (!ctx.interaction.guild_id)
return await ctx.editReply({ content: "Guild not found." });
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
flags: MessageFlags.Ephemeral,
},
});
const interaction =
ctx.interaction as APIMessageComponentSelectMenuInteraction;
@ -198,7 +225,13 @@ new Component({
acknowledge: false,
run: async (ctx) => {
if (!ctx.guildId)
return await ctx.editReply({ content: "Guild not found." });
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
flags: MessageFlags.Ephemeral,
},
});
const rawData = await REDIS.get(`roles-bot-setup:${ctx.guildId}`);
if (!rawData)
@ -253,15 +286,7 @@ new Component({
});
}
case "bot": {
sendFinal(ctx, data);
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Setup completed!",
flags: MessageFlags.Ephemeral,
},
});
return sendFinal(ctx, data);
}
}
},

View file

@ -58,6 +58,8 @@ export default {
flags: command.flags,
},
});
} catch (e) {
console.log(e);
} finally {
if (command.acknowledge)
command.run(new CommandContext(interaction, env));
@ -83,6 +85,8 @@ export default {
flags: modal.flags,
},
});
} catch (e) {
console.log(e);
} finally {
if (modal.acknowledge) modal.run(context);
// rome-ignore lint/correctness/noUnsafeFinally: it works, must do better typings etc...
@ -109,6 +113,8 @@ export default {
flags: component.flags,
},
});
} catch (e) {
console.log(e);
} finally {
if (component.acknowledge) component.run(context);
// rome-ignore lint/correctness/noUnsafeFinally: it works, must do better typings etc...

View file

@ -1,6 +1,7 @@
import {
APIWebhook,
ButtonStyle,
InteractionResponseType,
MessageFlags,
RouteBases,
Routes,
@ -15,14 +16,18 @@ import { RoleId } from "../types";
// Part 5 Roles ## add label, placeholder, emoji OR message content
new Modal({
id: "setup:part-roles-lpe",
flags: MessageFlags.Ephemeral,
acknowledge: false,
run: async (ctx) => {
const rawData = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`,
);
if (!rawData)
return await ctx.editReply({
content: "Data not found. Try running setup again.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Data not found. Try running setup again.",
flags: MessageFlags.Ephemeral,
},
});
const data = decodeFromString(rawData);
@ -60,21 +65,27 @@ new Modal({
600,
);
return await ctx.editReply({
content:
rawRoleIds.length > 0
? "Click the button to set the label, placeholder and emoji for next role."
: "Click the button to set message content or embed.",
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel(rawRoleIds.length > 0 ? "Next Role" : "Message Content")
.setCustomId("setup:part-roles")
.setStyle(ButtonStyle.Primary),
)
.toJSON(),
],
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content:
rawRoleIds.length > 0
? "Click the button to set the label, placeholder and emoji for next role."
: "Click the button to set message content or embed.",
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel(
rawRoleIds.length > 0 ? "Next Role" : "Message Content",
)
.setCustomId("setup:part-roles")
.setStyle(ButtonStyle.Primary),
)
.toJSON(),
],
flags: MessageFlags.Ephemeral,
},
});
},
});
@ -82,14 +93,18 @@ new Modal({
// Part 6 Message Content ## select send as webhook/as bot
new Modal({
id: "setup:part-messageContent",
flags: MessageFlags.Ephemeral,
acknowledge: false,
run: async (ctx) => {
const rawData = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`,
);
if (!rawData)
return await ctx.editReply({
content: "Data not found. Try running setup again.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Data not found. Try running setup again.",
flags: MessageFlags.Ephemeral,
},
});
const data = decodeFromString(rawData);
@ -101,12 +116,14 @@ new Modal({
const embedColor = ctx.interaction.data.components[3].components[0].value;
if (!content && !embedTitle && !embedDescription) {
await ctx.editReply({
content: "You must provide a message content or embed.",
components: [],
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "You must provide a message content or embed.",
components: [],
flags: MessageFlags.Ephemeral,
},
});
return;
}
data.message = { content, embedTitle, embedDescription, embedColor };
@ -117,23 +134,27 @@ new Modal({
600,
);
await ctx.editReply({
content:
"Choose whether you want to send the message as a webhook or as a bot.",
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel("Webhook")
.setCustomId("setup:part-sendAs:webhook")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setLabel("Bot")
.setCustomId("setup:part-sendAs:bot")
.setStyle(ButtonStyle.Primary),
)
.toJSON(),
],
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content:
"Choose whether you want to send the message as a webhook or as a bot.",
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel("Webhook")
.setCustomId("setup:part-sendAs:webhook")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setLabel("Bot")
.setCustomId("setup:part-sendAs:bot")
.setStyle(ButtonStyle.Primary),
)
.toJSON(),
],
flags: MessageFlags.Ephemeral,
},
});
},
});
@ -141,14 +162,18 @@ new Modal({
// Part 8 (ONLY IF WEBHOOK) Webhook ## send webhook
new Modal({
id: "setup:part-webhook",
flags: MessageFlags.Ephemeral,
acknowledge: false,
run: async (ctx) => {
const rawData = await REDIS.get(
`roles-bot-setup:${ctx.interaction.guild_id}`,
);
if (!rawData)
return await ctx.editReply({
content: "Data not found. Try running setup again.",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Data not found. Try running setup again.",
flags: MessageFlags.Ephemeral,
},
});
const data = decodeFromString(rawData);
@ -180,10 +205,6 @@ new Modal({
token: webhook.token,
};
sendFinal(ctx, data);
await ctx.editReply({
content: "Setup completed!",
});
return sendFinal(ctx, data);
},
});

View file

@ -3,6 +3,8 @@ import {
APIEmbed,
APIMessageActionRowComponent,
ButtonStyle,
InteractionResponseType,
MessageFlags,
RouteBases,
Routes,
} from "discord-api-types/v10";
@ -127,12 +129,22 @@ export default async function (ctx: Context, data: Data) {
if (!res.ok) {
const json: { message: string; code: string } = await res.json();
await ctx.editReply({
content: `Error: ${json.message} (${json.code})`,
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `Error: ${json.message} (${json.code})`,
flags: MessageFlags.Ephemeral,
},
});
}
break;
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Done!",
flags: MessageFlags.Ephemeral,
},
});
}
case "webhook": {
const res = await fetch(
@ -153,22 +165,34 @@ export default async function (ctx: Context, data: Data) {
},
);
await fetch(
`${RouteBases.api}${Routes.webhook(
data.webhook.id,
data.webhook.token,
)}`,
{
method: "DELETE",
},
);
if (!res.ok) {
const json: { message: string; code: string } = await res.json();
await ctx.editReply({
content: `Error: ${json.message} (${json.code})`,
});
} else {
await fetch(
`${RouteBases.api}${Routes.webhook(
data.webhook.id,
data.webhook.token,
)}`,
{
method: "DELETE",
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `Error: ${json.message} (${json.code})`,
flags: MessageFlags.Ephemeral,
},
);
});
}
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Done!",
flags: MessageFlags.Ephemeral,
},
});
}
}
}

View file

@ -3,8 +3,6 @@ type = "javascript"
account_id = "294bee38d448e390dab3757215c63f03"
compatibility_date = "2022-07-12"
main = "dist/worker.mjs"
[build]
command = "pnpm build"
[build.upload]