Skip to content

Commit

Permalink
fix(Actions): inject built data by using a symbol (#9203)
Browse files Browse the repository at this point in the history
This fixes the hard crash encountered from message component interactions
where a `channel` property was added to them, causing hard crashes

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
vladfrangu and kodiakhq[bot] committed Mar 9, 2023
1 parent 4367ab9 commit a63ac88
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/actions/Action.js
Expand Up @@ -34,7 +34,7 @@ class GenericAction {
getChannel(data) {
const id = data.channel_id ?? data.id;
return (
data.channel ??
data[this.client.actions.injectedChannel] ??
this.getPayload(
{
id,
Expand All @@ -51,7 +51,7 @@ class GenericAction {
getMessage(data, channel, cache) {
const id = data.message_id ?? data.id;
return (
data.message ??
data[this.client.actions.injectedMessage] ??
this.getPayload(
{
id,
Expand Down Expand Up @@ -86,7 +86,7 @@ class GenericAction {

getUser(data) {
const id = data.user_id;
return data.user ?? this.getPayload({ id }, this.client.users, id, Partials.User);
return data[this.client.actions.injectedUser] ?? this.getPayload({ id }, this.client.users, id, Partials.User);
}

getUserFromMember(data) {
Expand Down
7 changes: 7 additions & 0 deletions packages/discord.js/src/client/actions/ActionsManager.js
@@ -1,6 +1,13 @@
'use strict';

class ActionsManager {
// These symbols represent fully built data that we inject at times when calling actions manually. Action#getUser,
// for example, will return the injected data (which is assumed to be a built structure) instead of trying to make it
// from provided data
injectedUser = Symbol('djs.actions.injectedUser');
injectedChannel = Symbol('djs.actions.injectedChannel');
injectedMessage = Symbol('djs.actions.injectedMessage');

constructor(client) {
this.client = client;

Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/structures/Message.js
Expand Up @@ -787,9 +787,9 @@ class Message extends Base {

return this.client.actions.MessageReactionAdd.handle(
{
user: this.client.user,
channel: this.channel,
message: this,
[this.client.actions.injectedUser]: this.client.user,
[this.client.actions.injectedChannel]: this.channel,
[this.client.actions.injectedMessage]: this,
emoji: resolvePartialEmoji(emoji),
},
true,
Expand Down

2 comments on commit a63ac88

@vercel
Copy link

@vercel vercel bot commented on a63ac88 Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a63ac88 Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.