make interactions more normal

This commit is contained in:
MathMan05
2025-11-19 14:44:17 -06:00
committed by Rory&
parent fed12bac01
commit d98d0f8e25
5 changed files with 11 additions and 52 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -20,6 +20,7 @@ import { ButtonStyle, InteractionCallbackSchema, InteractionCallbackType, Messag
import { route } from "@spacebar/api";
import { Request, Response, Router } from "express";
import { emitEvent, FieldErrors, InteractionSuccessEvent, Message, MessageCreateEvent, pendingInteractions, User } from "@spacebar/util";
import { sendMessage } from "../../../../util/handlers/Message";
const router = Router({ mergeParams: true });
@@ -95,11 +96,11 @@ router.post("/", route({}), async (req: Request, res: Response) => {
// TODO
break;
case InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE: {
const message = await Message.createWithDefaults({
const user = await User.findOneOrFail({ where: { id: interaction.userId } });
await sendMessage({
type: MessageType.APPLICATION_COMMAND,
timestamp: new Date(),
application_id: interaction.applicationId,
guild_id: interaction.guildId,
channel_id: interaction.channelId,
author_id: interaction.applicationId,
content: body.data.content,
@@ -115,11 +116,13 @@ router.post("/", route({}), async (req: Request, res: Response) => {
id: interactionId,
name: interaction.commandName,
type: 2,
user,
},
interaction_metadata: {
id: interactionId,
type: 2,
user_id: interaction.userId,
user,
authorizing_integration_owners: {
"1": interaction.userId,
},
@@ -128,53 +131,6 @@ router.post("/", route({}), async (req: Request, res: Response) => {
},
});
const user = await User.findOneOrFail({ where: { id: interaction.userId } });
// Don't save messages with ephemeral flag (64) set
if ((message.flags & (1 << 6)) == 0) {
message.save();
}
emitEvent({
event: "MESSAGE_CREATE",
...((message.flags & (1 << 6)) === 0 ? { channel_id: interaction.channelId } : { user_id: interaction.userId }),
data: {
application_id: interaction.applicationId,
attachments: message.attachments,
author: message.author?.toPublicUser(),
channel_id: message.channel_id,
channel_type: 0,
components: message.components,
content: message.content,
edited_timestamp: null,
embeds: message.embeds,
flags: message.flags,
id: message.id,
interaction: {
id: interactionId,
name: interaction.commandName,
type: interaction.type,
user,
},
interaction_metadata: {
authorizing_integration_owners: { "1": interaction.userId },
command_type: interaction.commandType,
id: interactionId,
name: interaction.commandName,
type: interaction.type,
user,
},
mention_everyone: false,
mentions: [],
nonce: interaction.nonce,
pinned: false,
position: 0,
timestamp: message.timestamp,
tss: message.tts,
type: message.type,
webhook_id: interaction.applicationId,
} as MessageCreateSchema,
} as MessageCreateEvent);
break;
}
case InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE:

View File

@@ -501,6 +501,7 @@ export async function postHandleMessage(message: Message) {
console.error(`[Embeds] Error while generating embed for ${link}`, e);
}
}
const ephermal = (message.flags & (1 << 6)) !== 0;
await Promise.all([
emitEvent({
@@ -508,7 +509,7 @@ export async function postHandleMessage(message: Message) {
channel_id: message.channel_id,
data,
} as MessageUpdateEvent),
Message.update({ id: message.id, channel_id: message.channel_id }, { embeds: data.embeds }),
ephermal ? null : Message.update({ id: message.id, channel_id: message.channel_id }, { embeds: data.embeds }),
...cachePromises,
]);
}
@@ -516,11 +517,12 @@ export async function postHandleMessage(message: Message) {
export async function sendMessage(opts: MessageOptions) {
const message = await handleMessage({ ...opts, timestamp: new Date() });
const ephermal = (message.flags & (1 << 6)) !== 0;
await Promise.all([
Message.insert(message),
ephermal ? null : Message.insert(message),
emitEvent({
event: "MESSAGE_CREATE",
channel_id: opts.channel_id,
...(ephermal ? { user_id: message.interaction_metadata?.user_id } : { channel_id: message.channel_id }),
data: message.toJSON(),
} as MessageCreateEvent),
]);

View File

@@ -89,6 +89,7 @@ interface MessageInteractionSchema {
command_type?: ApplicationCommandType;
ephemerality_reason?: number;
user?: PublicUser; // It has to be optional cause LSP gives an errors for some reason
user_id?: string;
authorizing_integration_owners?: object; // It has to be optional cause LSP gives an errors for some reason
original_response_message_id?: Snowflake;
interacted_message_id?: Snowflake;