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

View file

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

View file

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

View file

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

View file

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

View file

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