Python TwitchIO Не подключается к каналу. Переменная _websockets представляет собой пустой DictPython

Программы на Python
Ответить
Anonymous
 Python TwitchIO Не подключается к каналу. Переменная _websockets представляет собой пустой Dict

Сообщение Anonymous »

В попытках интегрировать чат Twitch в удобный формат я обнаружил, что мой бот не подключается.
Я использую веб-сокеты Asyncio, поскольку у меня есть несколько задач, которые необходимо выполнять параллельно, и мой бот Twitch не показывает ошибок, но ничего из того, что вводится в чат Twitch, не регистрируется в боте. Поразмыслив некоторое время, я обнаружил, что переменная self._websockets отображается как defaultdict(, {}). Я посмотрел это, и источники сказали, что это означает, что мой бот на самом деле не подключался к моему каналу.
Не раскрывая слишком много информации, позвольте мне пройти процесс, который я настраивал, а также раскрытие моего кода:
1. Я настроил новое приложение в категории «Чат-бот» в Twitch Dev Tools.

- Параметры Chat:Read и Chat:Write отключены.

- Для обратного вызова URL-адреса OAuth установлено значение «http://localhost:4343/oauth/callback»

2. Мой «бот» использует ClientID и сгенерированный на его основе SECRET, а идентификатор бота — это просто идентификатор моей учетной записи, поскольку я предпочитаю не создавать совершенно отдельную учетную запись только для управления источником моего браузера OBS. Я дважды проверил токен OAUTH, и он должен быть правильным. Я даже пытался повторно сгенерировать код, оба параметра chat:read/write отключены.

3. Исходные каналы представляют собой список из одного элемента, в котором используется только имя моего канала в нижнем регистре.
Я не уверен, что делаю неправильно, я могу показать код, чтобы вы могли увидеть мою борьбу.
main.py
import asyncio, json, websockets

from classes import ws_logic
from classes.twitchintegration import TwitchChatBot

# Config ---
def get_config() -> dict:
with open(f"config/settings.json", 'r') as conffile:
return json.load(conffile)

async def main():
config = get_config()

server = await websockets.serve(
ws_logic.handler,
config["websocket"]["host"],
config["websocket"]["port"]
)

print(f"WebSocket Server started on ws://{config['websocket']['host']}:{config['websocket']['port']}")
dispatcher_task = asyncio.create_task(ws_logic.dispatcher())

twitchchat_bot = TwitchChatBot(
token=f"oauth:{config["twitch"]["oauth_token"]}",
client_id=config["twitch"]["client_id"],
client_secret=config["twitch"]["client_secret"],
bot_id=config["twitch"]["bot_id"],
channel=config["twitch"]["channel"]
)
twitchchat_task = await asyncio.create_task(twitchchat_bot.start())

try:
await asyncio.Future() # Run forever
except KeyboardInterrupt:
print("Shutting down server...")
except Exception as e:
print(f"Unexpected exception in main: {e}")
finally:
twitchchat_task.cancel()
dispatcher_task.cancel()
server.close()

await server.wait_closed()
await asyncio.gather(
twitchchat_task,
dispatcher_task,
return_exceptions=True
)

print("Server shut down successfully.")

if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass

ws_logic.py
import asyncio, websockets

from classes.messagetypes import Base_Message

clients = set()
message_queue: asyncio.Queue[Base_Message] = asyncio.Queue(maxsize=200)

async def handler(websocket: websockets):
clients.add(websocket)
print(f"Client connected. Total clients: {len(clients)}")

try:
await websocket.wait_closed()
except Exception as e:
print(f"Unexpected exception in handler: {e}")
finally:
clients.discard(websocket)
print(f"Client disconnected. Total clients: {len(clients)}")

async def dispatcher():
"""
Waits for Messages in the Queue then dispatches them to the websocket clients.
Sleeps Automatically when the queue is empty.
"""

print("Dispatcher started.")

while True:
message = await message_queue.get()

if not clients:
# No Clients -> Drop Message
continue

disconnected = set()

for client in clients:
try:
await client.send(message.model_dump_json())
except (websockets.ConnectionClosedOK, websockets.ConnectionClosedError):
disconnected.add(client)
except KeyboardInterrupt:
print("Server stopping...")
return

for d in disconnected:
# Removes Clients in one large batch to avoid modifying the set while iterating.
clients.remove(d)

async def publish_message(message: Base_Message):
"""
Publishes a Message to the Message Queue for dispatching to all connected clients.
"""
print(f"Publishing message of type: {message.model_dump()}")
await message_queue.put(message)

twitchintegration.py
import logging
import twitchio

from twitchio.ext import commands

from classes.messagetypes import Base_Message
from classes import ws_logic

handler = logging.FileHandler(filename='twitchio.log', encoding='utf-8', mode='w')
twitchio.utils.setup_logging(level=logging.DEBUG, handler=handler)

class TwitchChatBot(commands.Bot):
def __init__(self, token: str, channel: str, client_id: str, client_secret: str, bot_id: str, nick: str = "CloverlyTwitchBot"):
"""
Initialize the Twitch chat bot.

:param token: OAuth token (must include 'oauth:' prefix)
:param channel: Twitch channel to join
:param nick: Bot nickname (optional)
"""
self.initial_channels = [channel]

super().__init__(
token=token,
client_id=client_id,
client_secret=client_secret,
bot_id=bot_id,
owner_id=bot_id,
prefix='!',
initial_channels=self.initial_channels,
nick=nick
)

async def event_ready(self):
print(f"[READY] Connected as {self.user.name} (ID: {self.user.id})")
print(f"[READY] Initial channels: {self.initial_channels}")
print(f"[READY] Websockets: {self._websockets}") # contains joined channels

@commands.command(name='ping')
async def ping_command(self, ctx: commands.Context):
await ctx.send('Pong!')

async def event_message(self, message):
print(f"[DEBUG] Received message from {message.author.name}: {message.content}")
chat_message = Base_Message(
type="twitch_chat",
message="New chat message received",
data={
"author": message.author.name,
"content": message.content,
"timestamp": str(message.timestamp)
}
)

await ws_logic.publish_message(chat_message)

async def event_follow(self, user):
follow_message = Base_Message(
type="twitch_follow",
message="New follower detected",
data={
"follower": user.name,
"timestamp": str(user.followed_at)
}
)

await ws_logic.publish_message(follow_message)
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»