PythonistaGuild / TwitchIO

An Async Bot/API wrapper for Twitch made in Python.
https://twitchio.dev
MIT License
798 stars 162 forks source link

get_custom_rewards fails #214

Closed RobertErobert closed 3 years ago

RobertErobert commented 3 years ago

I want to get all custom rewards and then iterate over them.

I am on the master branch.

Thats my code:

import os

from twitchio import Channel, PartialChatter, User
from twitchio.ext import commands

import environment_djwindless

class Bot(commands.Bot):

    def __init__(self):
        super().__init__(
            token=os.getenv('ACCESS_TOKEN_DJWINDLESS'),
            prefix=os.getenv('PREFIX'),
            initial_channels=[os.getenv('INITIAL_CHANNELS')]
        )
        self.channel = None
        self.channel_user = None

    async def event_ready(self):
        self.channel: Channel = self.get_channel('djwindless')
        channel_partial_chatter: PartialChatter = self.channel.get_chatter('djwindless')
        print(channel_partial_chatter)  # Prints <PartialChatter name: djwindless, channel: <Channel name: djwindless>>
        self.channel_user: User = await channel_partial_chatter.user()
        print(self.channel_user) # Prints <User id=175202019 name=djwindless display_name=DJWindless type=UserTypeEnum.none>

        custom_rewards = await self.channel_user.get_custom_rewards(token=os.getenv('ACCESS_TOKEN_DJWINDLESS'))
        for custom_reward in custom_rewards:
            print(custom_reward)

bot = Bot()
bot.run()

Thats the traceback:

Traceback (most recent call last):
  File "C:\Users\rober\Google Drive\Twitch\Code\djwindless-chatbot-v2\virtual_env_3_8\lib\site-packages\twitchio\client.py", line 190, in wrapped
    await func(*args)
  File ".\minimal.py", line 33, in event_ready
    custom_rewards = await self.channel_user.get_custom_rewards(token=os.getenv('ACCESS_TOKEN_DJWINDLESS'))
  File "C:\Users\rober\Google Drive\Twitch\Code\djwindless-chatbot-v2\virtual_env_3_8\lib\site-packages\twitchio\user.py", line 156, in get_custom_rewards
    if not force and self._cached_rewards and self._cached_rewards[0] + 300 > time.monotonic():
AttributeError: _cached_rewards
RobertErobert commented 3 years ago

When i use force=True i got following:

Traceback (most recent call last):
  File "C:\Users\rober\Google Drive\Twitch\Code\djwindless-chatbot-v2\virtual_env_3_8\lib\site-packages\twitchio\client.py", line 190, in wrapped
    await func(*args)
  File ".\minimal.py", line 27, in event_ready
    custom_rewards = await self.channel_user.get_custom_rewards(token=os.getenv('ACCESS_TOKEN_DJWINDLESS').replace('oauth:', ''), force=True)
  File "C:\Users\rober\Google Drive\Twitch\Code\djwindless-chatbot-v2\virtual_env_3_8\lib\site-packages\twitchio\user.py", line 174, in get_custom_rewards
    values = [CustomReward(self._http, x, self) for x in data]
  File "C:\Users\rober\Google Drive\Twitch\Code\djwindless-chatbot-v2\virtual_env_3_8\lib\site-packages\twitchio\user.py", line 174, in <listcomp>
    values = [CustomReward(self._http, x, self) for x in data]
  File "C:\Users\rober\Google Drive\Twitch\Code\djwindless-chatbot-v2\virtual_env_3_8\lib\site-packages\twitchio\rewards.py", line 70, in __init__
    self._broadcaster_id = obj["channel_id"]
KeyError: 'channel_id'
RobertErobert commented 3 years ago

channel_id is not in the response from twitch:

{
  'broadcaster_name': 'DJWindless',
  'broadcaster_login': 'djwindless',
  'broadcaster_id': '175202019',
  'id': '0ddbccb7-58f9-460b-9850-4de8d5ccf9a2',
  'image': {
    'url_1x': 'https://static-cdn.jtvnw.net/custom-reward-images/175202019/0ddbccb7-58f9-460b-9850-4de8d5ccf9a2/0391c292-c535-43ad-b89a-42d5c434a4b1/custom-1.png',
    'url_2x': 'https://static-cdn.jtvnw.net/custom-reward-images/175202019/0ddbccb7-58f9-460b-9850-4de8d5ccf9a2/0391c292-c535-43ad-b89a-42d5c434a4b1/custom-2.png',
    'url_4x': 'https://static-cdn.jtvnw.net/custom-reward-images/175202019/0ddbccb7-58f9-460b-9850-4de8d5ccf9a2/0391c292-c535-43ad-b89a-42d5c434a4b1/custom-4.png'
  },
  'background_color': '#9146FF',
  'is_enabled': False,
  'cost': 50000,
  'title': 'DJ du hast ein Horn',
  'prompt': '10 Minuten Einhorn Mütze',
  'is_user_input_required': False,
  'max_per_stream_setting': {
    'is_enabled': True,
    'max_per_stream': 1
  },
  'max_per_user_per_stream_setting': {
    'is_enabled': True,
    'max_per_user_per_stream': 1
  },
  'global_cooldown_setting': {
    'is_enabled': False,
    'global_cooldown_seconds': 0
  },
  'is_paused': False,
  'is_in_stock': False,
  'default_image': {
    'url_1x': 'https://static-cdn.jtvnw.net/custom-reward-images/default-1.png',
    'url_2x': 'https://static-cdn.jtvnw.net/custom-reward-images/default-2.png',
    'url_4x': 'https://static-cdn.jtvnw.net/custom-reward-images/default-4.png'
  },
  'should_redemptions_skip_request_queue': False,
  'redemptions_redeemed_current_stream': None,
  'cooldown_expires_at': None
}