bwmarrin / discordgo

(Golang) Go bindings for Discord
BSD 3-Clause "New" or "Revised" License
5.11k stars 812 forks source link

Unknown Message Component Type 16 #1562

Open Suhaibinator opened 2 months ago

Suhaibinator commented 2 months ago

I received a Message Create message that is unable to be unmarshalled because it has a Message Component of type 16.

Here is the components field of the message create with identifying information censored

"components": [
    {
      "type": 16,
      "id": 1,
      "content_inventory_entry": {
        "traits": [
          {
            "type": 4,
            "range": 1
          },
          {
            "type": 2,
            "duration_seconds": 81737
          }
        ],
        "participants": [
          "CENSORED"
        ],
        "original_id": "CENSORED",
        "id": "CENSORED",
        "extra": {
          "type": "played_game_extra",
          "platform": 0,
          "game_name": "Red Dead Redemption 2",
          "application_id": "CENSORED"
        },
        "content_type": 3,
        "author_type": 1,
        "author_id": "CENSORED"
      }
    }
  ],

This component prevents the entire message from being unmarshalled because we do an early return if we receive a message component whose type is not handled:

// UnmarshalJSON is a helper function to unmarshal MessageComponent object.

func (umc *unmarshalableMessageComponent) UnmarshalJSON(src []byte) error {
    var v struct {
        Type ComponentType `json:"type"`
    }
    err := json.Unmarshal(src, &v)
    if err != nil {
        return err
    }

    switch v.Type {
    case ActionsRowComponent:
        umc.MessageComponent = &ActionsRow{}
    case ButtonComponent:
        umc.MessageComponent = &Button{}
    case SelectMenuComponent, ChannelSelectMenuComponent, UserSelectMenuComponent,
        RoleSelectMenuComponent, MentionableSelectMenuComponent:
        umc.MessageComponent = &SelectMenu{}
    case TextInputComponent:
        umc.MessageComponent = &TextInput{}
    default:
        return fmt.Errorf("unknown component type: %d", v.Type)
    }
    return json.Unmarshal(src, umc.MessageComponent)
}
glotchimo commented 2 months ago

Looks like metadata for an Activity Card, part of the Recent Activity beta

Suhaibinator commented 2 months ago

@glotchimo if that's the case then it is breaking the marshaling. Should we change the marshaling to be more robust to this? @bwmarrin @FedorLap2006

glotchimo commented 2 months ago

Without documentation (not sure if these have been written/posted by Discord yet, all we have is the known 8 here), it's hard to say what that would look like here. Also, considering how much higher it is than the current max type of 8, has me wondering if it's temporary or if there are 8+ more message component types on the way? https://github.com/bwmarrin/discordgo/blob/41a66e5f151f0f325cb7cf1e96974b567d5180d3/components.go#L8-L21

Suhaibinator commented 2 months ago

Either way, marshaling ceases completely when an unexpected field is encountered. Seeing as how this can and actually is happening, I believe we should not completely cease marshaling when this happens.