issork / gift

Godot IRC For Twitch addon
MIT License
150 stars 23 forks source link

Enabling disk_cache can lead to an exception (invalid_index) when changing channel #25

Closed jynus closed 1 year ago

jynus commented 1 year ago

I did the following:

I got the following error:

Invalid get index 'badge_sets' (on base 'Dictionary'). (gift_node.gd:525) Screenshot_20230805_175123

I guessed that it assumes that if the cache exists, it contains this key. I fixed the issue by changing the logic to this:

        if (disk_cache && FileAccess.file_exists(filename)):
            var cache = JSON.parse_string(FileAccess.get_file_as_string(filename))
            if "badge_sets" in cache:
                return cache["badge_sets"]

        var request : HTTPRequest = HTTPRequest.new()
        add_child(request)
        request.request("https://api.twitch.tv/helix/chat/badges" + ("/global" if channel_id == "_global" else "?broadcaster_id=" + channel_id), [USER_AGENT, "Authorization: Bearer " + token["access_token"], "Client-Id:" + client_id, "Content-Type: application/json"], HTTPClient.METHOD_GET)

Let me know what you think and I can send you a PR, if it seems like a reasonable change.

issork commented 1 year ago

Hey, sorry for the late response - the change looks good to me, feel free to create a PR ^^

jynus commented 1 year ago

Will do.

jynus commented 1 year ago

There you have it- I chose to short circuit the 3 main blocks, as I thought it was clearer in this case than the "single return at the end". I tested it by connecting and reconnecting to several channels and deleting the cache files several times, but further testing will be welcome, too! :-D

jynus commented 1 year ago

Thank you a lot!