Issue
So, I tried to make a Twitch Chat(IRC) logger in Python 3.7 today. After running python -m pip install pipenv
pipenv --python 3.7
pipenv install setproctitle
pipenv install twitchio
pipenv run pip install --upgrade git+https://github.com/Gallopsled/pwntools.git@dev3
and I get this error when I run logger.py:
2019-12-20
Task exception was never retrieved
future: <Task finished coro=<WebsocketConnection.auth_seq() done, defined at /home/zoe/.local/share/virtualenvs/Twitch-Dev-B4hlqNxh/lib/python3.7/site-packages/twitchio/websocket.py:200> exception=KeyError('zoes17')>
Traceback (most recent call last):
File "/home/zoe/.local/share/virtualenvs/Twitch-Dev-B4hlqNxh/lib/python3.7/site-packages/twitchio/websocket.py", line 280, in _join_channel
await asyncio.wait_for(fut, timeout=10)
File "/usr/lib/python3.7/asyncio/tasks.py", line 449, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/zoe/.local/share/virtualenvs/Twitch-Dev-B4hlqNxh/lib/python3.7/site-packages/twitchio/websocket.py", line 228, in auth_seq
await self.join_channels(*channels)
File "/home/zoe/.local/share/virtualenvs/Twitch-Dev-B4hlqNxh/lib/python3.7/site-packages/twitchio/websocket.py", line 271, in join_channels
await asyncio.gather(*[self._join_channel(x) for x in channels])
File "/home/zoe/.local/share/virtualenvs/Twitch-Dev-B4hlqNxh/lib/python3.7/site-packages/twitchio/websocket.py", line 282, in _join_channel
self._pending_joins.pop(channel)
KeyError: 'zoes17'
^C/home/zoe/.local/share/virtualenvs/Twitch-Dev-B4hlqNxh/lib/python3.7/site-packages/twitchio/websocket.py:618: RuntimeWarning: coroutine 'WebSocketCommonProtocol.close' was never awaited
self._websocket.close()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Here's the relevent code from logger.py and it seems to connect but after the 10 second timeout crashes. I am using oauth:oathkeyFromTwitch as my password in the TMI_TOKEN
env varrible using a .env file.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Logger main class."""
import os # for importing env vars for the bot to use
import sys # for argument processing
from datetime import *
from pwnlib.term import text
from setproctitle import setproctitle
from twitchio.ext import commands
channel = ""
if len(sys.argv) > 1:
channel = sys.argv[1]
bot = commands.Bot(
# set up the bot
irc_token=os.environ['TMI_TOKEN'],
client_id=os.environ['CLIENT_ID'],
nick=os.environ['BOT_NICK'],
prefix=os.environ['BOT_PREFIX'],
initial_channels=[os.environ['CHANNEL'], channel]
)
@bot.event
async def event_ready():
'Called once when the bot goes online.'
print(f"{os.environ['BOT_NICK']} is online!")
ws = bot._ws # this is only needed to send messages within event_ready
await ws.send_privmsg(os.environ['CHANNEL'], f"/me has landed!")
@bot.event
async def event_message(ctx):
"""Runs every time a message is sent in chat."""
# make sure the bot ignores itself and the streamer
if ctx.author.name.lower() == os.environ['BOT_NICK'].lower():
return
await bot.handle_commands(ctx)
if 'hello' in ctx.content.lower():
await ctx.channel.send(f"Hi, @{ctx.author.name}!")
if __name__ == "__main__":
from commands import Handler as command_handler
bot.run()
What is my code addled brain missing here?
Solution
initial_channels=[os.environ['CHANNEL'], channel]
needed to become either:
initial_channels=[os.environ['CHANNEL']]
or initial_channels=[channel]
the relevent code from commands.Bot instantiates thusly:
class Bot(Client):
def __init__(self, irc_token: str, api_token: str=None, *, client_id: str=None, prefix: Union[list, tuple, str],
nick: str, loop: asyncio.BaseEventLoop=None, initial_channels: Union[list, tuple]=None,
webhook_server: bool=False, local_host: str=None, external_host: str=None, callback: str=None,
port: int=None, **attrs):
Answered By - Zoe S17 Answer Checked By - Timothy Miller (WPSolving Admin)