import asyncio import os from dotenv import load_dotenv import twitchio from twitchio.ext import commands # --- Standalone Twitch IRC Connection Test --- class TestBot(twitchio.Client): """A minimal twitchio client for testing IRC connectivity.""" def __init__(self, channel_name: str): # Load credentials from environment variables self.TMI_TOKEN = os.getenv("TWITCH_TEST_TOKEN") self.CLIENT_ID = os.getenv("TWITCH_CLIENT_ID") self.TARGET_CHANNEL = channel_name # Pre-flight checks if not all([self.TMI_TOKEN, self.CLIENT_ID, self.TARGET_CHANNEL]): raise ValueError("Missing required environment variables. Ensure TWITCH_TEST_TOKEN, TWITCH_CLIENT_ID, and a channel are provided.") print("--- Configuration ---") print(f"CLIENT_ID: {self.CLIENT_ID[:4]}...{self.CLIENT_ID[-4:]}") print(f"TOKEN: {self.TMI_TOKEN[:12]}...") print(f"TARGET CHANNEL: {self.TARGET_CHANNEL}") print("-----------------------") super().__init__( token=f"oauth:{self.TMI_TOKEN}", client_id=self.CLIENT_ID, initial_channels=[self.TARGET_CHANNEL], ssl=True ) async def event_ready(self): """Called once when the bot goes online.""" print("\n--- Connection Successful ---") print(f"Logged in as: {self.nick}") print(f"Listening for messages in #{self.TARGET_CHANNEL}...") print("---------------------------\n") async def event_message(self, message): """Runs every time a message is sent in chat.""" if message.echo: return print(f"#{message.channel.name} | {message.author.name}: {message.content}") async def main(): """Main function to run the test bot.""" # IMPORTANT: Replace 'ramforth' with the channel you want to test. channel_to_test = "ramforth" print(f"Attempting to connect to Twitch IRC for channel: {channel_to_test}") try: bot = TestBot(channel_name=channel_to_test) await bot.start() except Exception as e: print(f"\n--- AN ERROR OCCURRED ---") print(f"Error: {e}") print("Please check your credentials and network connection.") if __name__ == "__main__": asyncio.run(main())