Teekeks / pyTwitchAPI

A Python 3.7 compatible implementation of the Twitch API, EventSub, PubSub and Chat
https://pytwitchapi.dev
MIT License
254 stars 38 forks source link

create_poll error: KeyError: 'data' #244

Closed Bergruebe closed 1 year ago

Bergruebe commented 1 year ago

Hello, I have a problem with the poll creation:

Traceback (most recent call last):
  File "/Users/gb/PycharmProjects/JOCR-Bot2/module/twitchapi.py", line 68, in videovote
    await cmd.chat.twitch.create_poll(broadcaster_id=str(cmd.room.room_id), title="Bewertung des letzten Videos", choices=["⭐", "⭐⭐", "⭐⭐⭐", "⭐⭐⭐⭐", "⭐⭐⭐⭐⭐"], duration=90, channel_points_voting_enabled=False)
  File "/Users/gb/PycharmProjects/JOCR-Bot2/venv/lib/python3.11/site-packages/twitchAPI/twitch.py", line 2751, in create_poll
    return await self._build_result('POST', 'polls', {}, AuthType.USER, [AuthScope.CHANNEL_MANAGE_POLLS], Poll, body_data=body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gb/PycharmProjects/JOCR-Bot2/venv/lib/python3.11/site-packages/twitchAPI/twitch.py", line 585, in _build_result
    d = data['data']
        ~~~~^^^^^^^^
KeyError: 'data'

My command:

await cmd.chat.twitch.create_poll(broadcaster_id=str(cmd.room.room_id), title="Bewertung des letzten Videos", choices=["⭐", "⭐⭐", "⭐⭐⭐", "⭐⭐⭐⭐", "⭐⭐⭐⭐⭐"], duration=90, channel_points_voting_enabled=False)

Everything else works perfectly, thank you very much for your work!

Edit: I forgot to write my version: I'm using PyTwitchAPI v3.11.0 with Python 3.11.

Teekeks commented 1 year ago

I tried to reproduce this with your exact code but was unable to do so. Does this bug happen every time?

Could you give a full code example?

(tried with py 3.11 and v3.11.0 as well as the v3.12.0 dev version)

Bergruebe commented 1 year ago

Thank you for your fast answer.

Here ist the full code example:

from twitchAPI import Twitch
from twitchAPI.types import AuthScope, ChatEvent
from twitchAPI.chat import Chat, EventData, ChatMessage, ChatSub, ChatCommand
from module import get_config
from module import db

USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT, AuthScope.CHANNEL_MANAGE_BROADCAST,
              AuthScope.CHANNEL_MANAGE_POLLS, AuthScope.CHANNEL_MANAGE_BROADCAST]

async def check_mod(cmd: ChatCommand):
    # Todo: read trusted_users from config
    if not (cmd.user.mod or cmd.user.name in config.trusted_user):
        await cmd.reply("Du bist kein Mod.")
        return False
    else:
        return True

async def videovote(cmd: ChatCommand):
    if not await check_mod(cmd):
        return
    await cmd.chat.twitch.create_poll(broadcaster_id=str(cmd.room.room_id), title="Bewertung des letzten Videos", choices=["⭐", "⭐⭐", "⭐⭐⭐", "⭐⭐⭐⭐", "⭐⭐⭐⭐⭐"], duration=90, channel_points_voting_enabled=False)

async def run(pass_config):
    # global config
    config = pass_config
    twitch = await Twitch(config.app_id, config.app_secret)
    twitch.auto_refresh_auth = False
    await twitch.set_user_authentication(config.user_token, USER_SCOPE, config.user_refresh_token)
    chat = await Chat(twitch)

    chat.register_event(ChatEvent.READY, on_ready)
    chat.register_event(ChatEvent.MESSAGE, on_message)
    chat.register_event(ChatEvent.SUB, on_sub)

    chat.register_command('marker', set_marker)
    chat.register_command('videovote', videovote)
    chat.register_command('add', add_command)
    chat.register_command('remove', remove_command)
    chat.register_command('delete', remove_command)

    chat.start()
    try:
        input('press ENTER to stop\n')
    finally:
        chat.stop()
        await twitch.close()

I left out some of the other functions.

Maybe it is important to know, that my bot user is a different user than the broadcaster, but he has moderation and editor rights.

Teekeks commented 1 year ago

Maybe it is important to know, that my bot user is a different user than the broadcaster, but he has moderation and editor rights.

yes thats quite important. Only the broadcaster themself can create a poll for their own channel.

That you dont get a proper exception is problematic but to solve this you need to use a user authenticated twitch instance for that broadcaster to create this poll. You could do that by having 2 separate Twitch instances, one for the bot and one for the broadcaster.

Bergruebe commented 1 year ago

Oh, I wasn't aware of this limitation. I thought that everything is possible over the API, what is also possible over the normal website.

I will try it with another instance, thank you for the idea and your help!