Skip to content

Commit

Permalink
Extending Components to Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pakkographic committed Sep 11, 2024
1 parent 7af10c3 commit 0508219
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 40 deletions.
6 changes: 2 additions & 4 deletions lib/gateway/events/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,13 @@ export class MessageHandler extends GatewayEventHandler {
if (channel && channel.messages.has(data.reaction.messageId)) {
const interactionMessage =
channel.messages.get(data.reaction.messageId)!;
const originalInteraction =
channel.interactions.get(interactionMessage.originals.triggerID ?? "none");
const hasComponents = interactionMessage.components.length !== 0;
const emoteComponent =
interactionMessage.components
.find(component =>
component.type === InteractionComponentType.BUTTON && component.emoteID === data.reaction.emote.id
);
if (originalInteraction
if (interactionMessage.originals.triggerID
&& hasComponents
&& emoteComponent
&& data.reaction.createdBy !== this.client.user?.id
Expand All @@ -268,7 +266,7 @@ export class MessageHandler extends GatewayEventHandler {
{
customID: emoteComponent.customID,
emoteID: emoteComponent.emoteID,
userTriggerMessageID: originalInteraction.id,
userTriggerMessageID: interactionMessage.originals.triggerID,
reactionInfo: ReactionInfo
},
this.client
Expand Down
22 changes: 15 additions & 7 deletions lib/routes/Channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,11 @@ export class Channels {
replyMessageIds: options.replyMessageIDs,
hiddenLinkPreviewUrls: options.hiddenLinkPreviewURLs
}
}).then(data =>
this.#manager.client.util.updateMessage(data.message, params)
);
}).then(async data => {
const message: Message<T> = this.#manager.client.util.updateMessage(data.message, params);
await this.#manager.client.util.bulkAddComponents(message.channelID, options.components ?? [], message);
return message;
});
}


Expand Down Expand Up @@ -1036,12 +1038,18 @@ export class Channels {
embeds: newMessage.embeds ? this.#manager.client.util.embedsToRaw(newMessage.embeds) : undefined,
hiddenLinkPreviewUrls: newMessage.hiddenLinkPreviewURLs
}
}).then(data =>
this.#manager.client.util.updateMessage(
}).then(async data => {
const message: Message<T> = this.#manager.client.util.updateMessage(
data.message as RawMessage,
params
)
);
);
if (newMessage.components) message.components = newMessage["components" as keyof object];
if (message.components) {
await this.#manager.channels.bulkDeleteReactions(message.channelID, "ChannelMessage", message.id);
await this.#manager.client.util.bulkAddComponents(message.channelID, message.components, message, false);
}
return message;
});
}


Expand Down
10 changes: 5 additions & 5 deletions lib/structures/CommandInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import type {
AnyTextableChannel,
InteractionConstructorParams,
CommandInteractionData,
CreateInteractionMessageOptions,
EditMessageOptions,
Embed,
InteractionData,
JSONCommandInteraction,
RawMentions
RawMentions,
CreateMessageOptions,
EditMessageOptions
} from "../types";
import { InteractionOptionWrapper } from "../util/InteractionOptionWrapper";

Expand Down Expand Up @@ -178,7 +178,7 @@ export class CommandInteraction<T extends AnyTextableChannel = AnyTextableChanne
* (use CommandInteraction#createMessage if the interaction has not been acknowledged).
* @param options Message options.
*/
async createFollowup(options: CreateInteractionMessageOptions): Promise<Message<T>> {
async createFollowup(options: CreateMessageOptions): Promise<Message<T>> {
if (!this.acknowledged || !this.originalID)
throw new Error(
"Interaction has not been acknowledged, " +
Expand Down Expand Up @@ -220,7 +220,7 @@ export class CommandInteraction<T extends AnyTextableChannel = AnyTextableChanne
* (use CommandInteraction#createFollowup on already acknowledged interactions).
* @param options Message options.
*/
async createMessage(options: CreateInteractionMessageOptions): Promise<Message<T>> {
async createMessage(options: CreateMessageOptions): Promise<Message<T>> {
if (this.acknowledged)
throw new Error(
"Interaction has already been acknowledged, " +
Expand Down
13 changes: 5 additions & 8 deletions lib/structures/ComponentInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import type {
AnyInteractionComponent,
AnyTextableChannel,
InteractionConstructorParams,
EditInteractionMessageOptions,
EditMessageOptions,
Embed,
CreateInteractionMessageOptions,
ComponentInteractionData,
JSONComponentInteraction
JSONComponentInteraction,
CreateMessageOptions
} from "../types";

/** Represents a Component Interaction. */
Expand Down Expand Up @@ -77,7 +76,7 @@ export class ComponentInteraction<V extends AnyInteractionComponent = AnyInterac
* (use ComponentInteraction#createMessage if the interaction has not been acknowledged).
* @param options Message options.
*/
async createFollowup(options: CreateInteractionMessageOptions): Promise<Message<T>> {
async createFollowup(options: CreateMessageOptions): Promise<Message<T>> {
if (!this.acknowledged || !this.originalID)
throw new Error(
"Interaction has not been acknowledged, " +
Expand Down Expand Up @@ -110,9 +109,7 @@ export class ComponentInteraction<V extends AnyInteractionComponent = AnyInterac
acknowledged: true
}
);

this._lastMessageID = response.id as string;
await this.client.util.bulkAddComponents<T>(this.channelID, options.components ?? [], response);
if (!(this.originalID)) this.originalID = response.id;
return response;
}
Expand All @@ -121,7 +118,7 @@ export class ComponentInteraction<V extends AnyInteractionComponent = AnyInterac
* (use ComponentInteraction#createFollowup on already acknowledged interactions).
* @param options Message options.
*/
async createMessage(options: CreateInteractionMessageOptions): Promise<Message<T>> {
async createMessage(options: CreateMessageOptions): Promise<Message<T>> {
if (this.acknowledged)
throw new Error(
"Interaction has already been acknowledged, " +
Expand Down Expand Up @@ -200,7 +197,7 @@ export class ComponentInteraction<V extends AnyInteractionComponent = AnyInterac
* Edit Parent Interaction.
* @param newMessage New message content.
*/
async editParent(newMessage: EditInteractionMessageOptions): Promise<Message<T>> {
async editParent(newMessage: EditMessageOptions): Promise<Message<T>> {
if (this.acknowledged) throw new Error("Cannot edit parent interaction that has already been acknowledged.");
this.acknowledged = true;
return (
Expand Down
8 changes: 1 addition & 7 deletions lib/structures/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export class Message<T extends AnyTextableChannel> extends Base<string> {
* @param newMessage New message's options
*/
async edit(newMessage: EditMessageOptions): Promise<Message<T>> {
const editedMessage = this.client.rest.channels.editMessage<T>(
return this.client.rest.channels.editMessage<T>(
this.channelID,
this.id as string,
newMessage,
Expand All @@ -432,12 +432,6 @@ export class Message<T extends AnyTextableChannel> extends Base<string> {
}
}
);
if (newMessage["components" as keyof object]) this.components = newMessage["components" as keyof object];
if (this.components) {
await this.client.rest.channels.bulkDeleteReactions(this.channelID, "ChannelMessage", this.id);
await this.client.util.bulkAddComponents(this.channelID, this.components, this, false);
}
return editedMessage;
}

/**
Expand Down
13 changes: 4 additions & 9 deletions lib/types/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ export interface MessageOriginals {
triggerMessage: Message<AnyTextableChannel> | null;
}

export type CreateInteractionMessageOptions = CreateMessageOptions & { components?: Array<AnyInteractionComponent>; };
export type EditInteractionMessageOptions = EditMessageOptions & { components?: Array<AnyInteractionComponent>; };

export interface CreateMessageOptions {
/** Message components */
components?: Array<AnyInteractionComponent>;
/** The content of the message (min length 1; max length 4000) */
content?: string;
/** Embeds */
Expand All @@ -110,19 +109,15 @@ export interface CreateMessageOptions {
}

export interface EditMessageOptions {
/** Message components */
components?: Array<AnyInteractionComponent>;
/** The content of the message (min length 1; max length 4000) */
content?: string;
/** Embeds */
embeds?: Array<Embed>;
/** Links in content to prevent unfurling as a link preview when displaying in Guilded
* (min items 1; must have unique items true) */
hiddenLinkPreviewURLs?: Array<string>;
// /** Message IDs to reply to (min items 1; max items 5) */
// replyMessageIds?: Array<string>;
// /** If set, this message will not notify any mentioned users or roles (default `false`) */
// isSilent?: boolean;
// /** If set, this message will only be seen by those mentioned or replied to */
// isPrivate?: boolean;
}

export interface Embed {
Expand Down

0 comments on commit 0508219

Please sign in to comment.