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({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
},
});
// Delete the data if it exists
@ -42,7 +46,9 @@ new Command({
3600,
);
await ctx.editReply({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Select the channel to which the panel will be sent.",
components: [
new ActionRowBuilder<ChannelSelectMenuBuilder>()
@ -56,6 +62,8 @@ new Command({
)
.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({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
flags: MessageFlags.Ephemeral,
},
});
if (!ctx.interaction.member)
return ctx.editReply({
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({
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({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
},
});
const interaction =
@ -40,7 +43,9 @@ new Component({
600,
);
await ctx.editReply({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content:
"Choose whether you want to use buttons or dropdown menu (select menu).",
components: [
@ -57,6 +62,8 @@ new Component({
)
.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({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Data not found. Try running setup again.",
flags: MessageFlags.Ephemeral,
},
});
const data = decodeFromString(rawData);
@ -86,7 +103,9 @@ new Component({
600,
);
await ctx.editReply({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Select the roles that will be available in the menu.",
components: [
new ActionRowBuilder<RoleSelectMenuBuilder>()
@ -99,6 +118,8 @@ new Component({
)
.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({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Data not found. Try running setup again.",
flags: MessageFlags.Ephemeral,
},
});
const data = decodeFromString(rawData);
@ -60,7 +65,9 @@ new Modal({
600,
);
return await ctx.editReply({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content:
rawRoleIds.length > 0
? "Click the button to set the label, placeholder and emoji for next role."
@ -69,12 +76,16 @@ new Modal({
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel(rawRoleIds.length > 0 ? "Next Role" : "Message Content")
.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({
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({
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,7 +134,9 @@ new Modal({
600,
);
await ctx.editReply({
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content:
"Choose whether you want to send the message as a webhook or as a bot.",
components: [
@ -134,6 +153,8 @@ new Modal({
)
.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({
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({
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,12 +165,6 @@ 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})`,
});
} else {
await fetch(
`${RouteBases.api}${Routes.webhook(
data.webhook.id,
@ -168,7 +174,25 @@ export default async function (ctx: Context, data: Data) {
method: "DELETE",
},
);
}
if (!res.ok) {
const json: { message: string; code: string } = await res.json();
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]