feat: add support for edit message

This commit is contained in:
xHyroM 2023-05-26 23:08:45 +02:00
parent 334f24f30c
commit acf0a42a45
No known key found for this signature in database
GPG key ID: BE0423F386C436AA
8 changed files with 202 additions and 10 deletions

View file

@ -18,5 +18,8 @@
"packageManager": "pnpm@8.1.0", "packageManager": "pnpm@8.1.0",
"devDependencies": { "devDependencies": {
"rome": "^12.0.0" "rome": "^12.0.0"
},
"dependencies": {
"turbo": "^1.9.9"
} }
} }

View file

@ -0,0 +1,97 @@
import {
APIMessageApplicationCommandInteractionDataResolved,
InteractionResponseType,
MessageFlags,
RouteBases,
Routes,
APIRole,
ButtonStyle,
} from "discord-api-types/v10";
import { Command } from "../structs/Command";
import { ActionRowBuilder, ButtonBuilder } from "builders";
import { REDIS } from "../things";
import { encodeToHex } from "serialize";
new Command({
name: "Edit Message",
acknowledge: false,
run: async (ctx) => {
if (!ctx.guildId)
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Guild not found.",
},
});
const resolved = ctx.interaction.data
.resolved as APIMessageApplicationCommandInteractionDataResolved;
const messages = resolved.messages;
const message = Object.values(messages)[0];
if (message.author.id !== ctx.env.applicationId) {
return ctx.respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "This message was not sent by me.",
flags: MessageFlags.Ephemeral,
},
});
}
// Delete the data if it exists
await REDIS.del(`roles-bot-setup:${ctx.guildId}`);
await REDIS.setex(
`roles-bot-setup:${ctx.guildId}`,
encodeToHex({
channelId: message.channel_id,
originalMessageId: message.id,
sendAs: "bot",
}),
600,
);
const roles = (await (
await fetch(`${RouteBases.api}${Routes.guildRoles(ctx.guildId)}`, {
headers: {
Authorization: `Bot ${ctx.env.token}`,
},
})
).json()) as APIRole[];
await REDIS.setex(
`roles-bot-setup-roles:${ctx.guildId}`,
encodeToHex(
roles.map((r) => ({
id: r.id,
name: r.name,
})),
),
3600,
);
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,
},
});
},
});

View file

@ -1,11 +1,13 @@
import "./commands/setup"; import "./commands/setup";
import "./commands/info"; import "./commands/info";
import "./commands/edit_message";
import "./components/setup"; import "./components/setup";
import "./components/select"; import "./components/select";
import "./modals/setup"; import "./modals/setup";
import { import {
APIApplicationCommandInteraction, APIApplicationCommandInteraction,
APIMessageApplicationCommandInteraction,
APIMessageComponentInteraction, APIMessageComponentInteraction,
APIModalSubmitInteraction, APIModalSubmitInteraction,
APIPingInteraction, APIPingInteraction,
@ -35,6 +37,7 @@ export default {
const interaction = (await request.json()) as const interaction = (await request.json()) as
| APIPingInteraction | APIPingInteraction
| APIApplicationCommandInteraction | APIApplicationCommandInteraction
| APIMessageApplicationCommandInteraction
| APIModalSubmitInteraction | APIModalSubmitInteraction
| APIMessageComponentInteraction; | APIMessageComponentInteraction;

View file

@ -128,6 +128,10 @@ new Modal({
data.message = { content, embedTitle, embedDescription, embedColor }; data.message = { content, embedTitle, embedDescription, embedColor };
if (data.originalMessageId) {
return sendFinal(ctx, data);
}
await REDIS.setex( await REDIS.setex(
`roles-bot-setup:${ctx.interaction.guild_id}`, `roles-bot-setup:${ctx.interaction.guild_id}`,
encodeToHex(data), encodeToHex(data),

View file

@ -18,6 +18,7 @@ declare type DeclaredId = Record<
declare interface Env { declare interface Env {
publicKey: string; publicKey: string;
token: string; token: string;
applicationId: string;
redisApiClientKey: string; redisApiClientKey: string;
redisApiClientHost: string; redisApiClientHost: string;
} }
@ -32,6 +33,7 @@ declare interface RoleId {
declare interface BasicData { declare interface BasicData {
channelId: string; channelId: string;
originalMessageId?: string | null;
selecting: "buttons" | "dropdowns"; selecting: "buttons" | "dropdowns";
roleIds: RoleId[]; roleIds: RoleId[];
message: { message: {

View file

@ -115,7 +115,24 @@ export default async function (ctx: Context, data: Data) {
switch (data.sendAs) { switch (data.sendAs) {
case "bot": { case "bot": {
const res = await fetch( let res;
if (data.originalMessageId) {
res = await fetch(
`${RouteBases.api}${Routes.channelMessage(
data.channelId,
data.originalMessageId,
)}`,
{
method: "PATCH",
headers: {
Authorization: `Bot ${ctx.env.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
},
);
} else {
res = await fetch(
`${RouteBases.api}${Routes.channelMessages(data.channelId)}`, `${RouteBases.api}${Routes.channelMessages(data.channelId)}`,
{ {
method: "POST", method: "POST",
@ -126,6 +143,7 @@ export default async function (ctx: Context, data: Data) {
body: JSON.stringify(payload), body: JSON.stringify(payload),
}, },
); );
}
if (!res.ok) { if (!res.ok) {
const json: { message: string; code: string } = await res.json(); const json: { message: string; code: string } = await res.json();

View file

@ -3,6 +3,10 @@ lockfileVersion: '6.0'
importers: importers:
.: .:
dependencies:
turbo:
specifier: ^1.9.9
version: 1.9.9
devDependencies: devDependencies:
rome: rome:
specifier: ^12.0.0 specifier: ^12.0.0
@ -7337,6 +7341,67 @@ packages:
safe-buffer: 5.2.1 safe-buffer: 5.2.1
dev: false dev: false
/turbo-darwin-64@1.9.9:
resolution: {integrity: sha512-UDGM9E21eCDzF5t1F4rzrjwWutcup33e7ZjNJcW/mJDPorazZzqXGKEPIy9kXwKhamUUXfC7668r6ZuA1WXF2Q==}
cpu: [x64]
os: [darwin]
requiresBuild: true
dev: false
optional: true
/turbo-darwin-arm64@1.9.9:
resolution: {integrity: sha512-VyfkXzTJpYLTAQ9krq2myyEq7RPObilpS04lgJ4OO1piq76RNmSpX9F/t9JCaY9Pj/4TL7i0d8PM7NGhwEA5Ag==}
cpu: [arm64]
os: [darwin]
requiresBuild: true
dev: false
optional: true
/turbo-linux-64@1.9.9:
resolution: {integrity: sha512-Fu1MY29Odg8dHOqXcpIIGC3T63XLOGgnGfbobXMKdrC7JQDvtJv8TUCYciRsyknZYjyyKK1z6zKuYIiDjf3KeQ==}
cpu: [x64]
os: [linux]
requiresBuild: true
dev: false
optional: true
/turbo-linux-arm64@1.9.9:
resolution: {integrity: sha512-50LI8NafPuJxdnMCBeDdzgyt1cgjQG7FwkyY336v4e95WJPUVjrHdrKH6jYXhOUyrv9+jCJxwX1Yrg02t5yJ1g==}
cpu: [arm64]
os: [linux]
requiresBuild: true
dev: false
optional: true
/turbo-windows-64@1.9.9:
resolution: {integrity: sha512-9IsTReoLmQl1IRsy3WExe2j2RKWXQyXujfJ4fXF+jp08KxjVF4/tYP2CIRJx/A7UP/7keBta27bZqzAjsmbSTA==}
cpu: [x64]
os: [win32]
requiresBuild: true
dev: false
optional: true
/turbo-windows-arm64@1.9.9:
resolution: {integrity: sha512-CUu4hpeQo68JjDr0V0ygTQRLbS+/sNfdqEVV+Xz9136vpKn2WMQLAuUBVZV0Sp0S/7i+zGnplskT0fED+W46wQ==}
cpu: [arm64]
os: [win32]
requiresBuild: true
dev: false
optional: true
/turbo@1.9.9:
resolution: {integrity: sha512-+ZS66LOT7ahKHxh6XrIdcmf2Yk9mNpAbPEj4iF2cs0cAeaDU3xLVPZFF0HbSho89Uxwhx7b5HBgPbdcjQTwQkg==}
hasBin: true
requiresBuild: true
optionalDependencies:
turbo-darwin-64: 1.9.9
turbo-darwin-arm64: 1.9.9
turbo-linux-64: 1.9.9
turbo-linux-arm64: 1.9.9
turbo-windows-64: 1.9.9
turbo-windows-arm64: 1.9.9
dev: false
/type-check@0.4.0: /type-check@0.4.0:
resolution: {integrity: sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==} resolution: {integrity: sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==}
engines: {node: '>= 0.8.0'} engines: {node: '>= 0.8.0'}

View file