discordgo icon indicating copy to clipboard operation
discordgo copied to clipboard

fix(Activity): accept non-string application ids

Open TomWright opened this issue 3 months ago • 1 comments

I've been writing some code to read VoiceStates on application start-up and noticed it wasn't finding records where I would expect.

I noticed this coincided with the error wsapi.go:646:onEvent() error unmarshalling GUILD_CREATE event, json: cannot unmarshal number into Go struct field Presence.presences.activities of type string.

After doing some digging, I realised that discord is sending along activities with an application_id of 1 in some cases (Note that is 1 the integer, not "1" the string)

This causes the GUILD_CREATE event (and others) to fail parsing and not get added to the state, meaning I then have no way of accessing the current voice states when my app starts up.

I have run my app against my discordgo fork and it is now working as expected.

I know this logic isn't ideal, but I don't know of a better way to fix it given:

  1. We don't want to break the API.
  2. I don't understand the scenarios in which discord would be returning a number instead of a string.

Example activity with a bad app id:

{
          "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
          "type": 1,
          "timestamps": {
            "start": 1711557036583
          },
          "state": "Playing Single Player",
          "session_id": "cedf635683339917672bc194bf59b35d",
          "name": "Grand Theft Auto 6",
          "id": "876e03ca62d9cfc2",
          "flags": 1,
          "details": "Grand Theft Auto VI",
          "created_at": 1711557037898,
          "assets": {
            "small_image": "mp:external/ruoyEd5pgWX_V3GB4FWCzMZyRVJZFedojHrb07B-nJA/https/media.tenor.com/ByYRHA88z_8AAAAj/bottas-crash.gif",
            "large_image": "mp:attachments/812394872392122390/1218155883431858207/gtavi.png?ex=6606a312&is=65f42e12&hm=8a29b83a446c91ea04500d265ee317709a0c3aff971e04807ee48e1b2b3b5da2&"
          },
          "application_id": 1
        }

Out of 1142 activities found on this GUILD_CREATE event, the above is the single activity with a non-string application_id.

TomWright avatar Mar 28 '24 01:03 TomWright

Related to:

  • https://github.com/discord/discord-api-docs/issues/6763
  • https://github.com/discord/discord-api-docs/issues/6609

It seems as though a numeric response for snowflake types is intended. A better fix may be to have a Snowflake type that deals with this, although that is a breaking change.

TomWright avatar Mar 28 '24 11:03 TomWright