ergqa
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import twitchio
|
||||
from sqlalchemy.orm import Session
|
||||
from database import SessionLocal
|
||||
import models
|
||||
import security
|
||||
|
||||
class TwitchBot(twitchio.Client):
|
||||
def __init__(self, access_token: str, client_id: str, client_secret: str, channel_name: str, websocket_manager, db_user_id: int):
|
||||
def __init__(self, access_token: str, refresh_token: str, client_id: str, client_secret: str, channel_name: str, websocket_manager, db_user_id: int):
|
||||
self.websocket_manager = websocket_manager
|
||||
# Store our application's database user ID to avoid conflict with twitchio's internal 'owner_id'
|
||||
self.db_user_id = db_user_id
|
||||
@@ -16,6 +20,7 @@ class TwitchBot(twitchio.Client):
|
||||
# identify itself with Twitch's services.
|
||||
client_id=client_id,
|
||||
client_secret=client_secret,
|
||||
refresh_token=refresh_token,
|
||||
initial_channels=[channel_name],
|
||||
ssl=True # Mandate: Explicitly use SSL for the IRC connection.
|
||||
)
|
||||
@@ -35,6 +40,21 @@ class TwitchBot(twitchio.Client):
|
||||
# Diagnostic Logging: Confirming the bot is ready and joined the channel.
|
||||
print(f"DIAGNOSTIC: Listener connected and ready for user_id: {self.db_user_id}, channel: #{self.channel_name}")
|
||||
|
||||
async def event_token_refresh(self, token: str, refresh_token: str):
|
||||
"""
|
||||
Called when twitchio automatically refreshes the token.
|
||||
We must save the new tokens back to our database.
|
||||
"""
|
||||
print(f"DIAGNOSTIC: Token refreshed for user {self.db_user_id}. Saving new tokens to database.")
|
||||
db: Session = SessionLocal()
|
||||
try:
|
||||
user = db.query(models.User).filter(models.User.id == self.db_user_id).first()
|
||||
if user:
|
||||
user.encrypted_tokens = security.encrypt_tokens(token, refresh_token)
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
async def event_message(self, message): # Mandate: Type hint removed to prevent import errors.
|
||||
"""Runs every time a message is sent in chat."""
|
||||
# Diagnostic Logging: Checkpoint 1 - A raw message is received from Twitch.
|
||||
|
||||
@@ -30,9 +30,11 @@ class ListenerManager:
|
||||
try:
|
||||
tokens = security.decrypt_tokens(user.encrypted_tokens)
|
||||
access_token = tokens['access_token']
|
||||
refresh_token = tokens['refresh_token']
|
||||
|
||||
bot = TwitchBot(
|
||||
access_token=access_token,
|
||||
refresh_token=refresh_token,
|
||||
channel_name=user.username,
|
||||
client_id=settings.TWITCH_CLIENT_ID,
|
||||
client_secret=settings.TWITCH_CLIENT_SECRET,
|
||||
|
||||
Reference in New Issue
Block a user