class NullAdapter

This commit is contained in:
2025-11-17 18:27:00 +01:00
parent 2f0e817e12
commit c3d77974e3

View File

@@ -1,23 +1,33 @@
from twitchio.ext import commands from twitchio.ext import commands
class NullAdapter:
"""A dummy web adapter that does nothing to prevent twitchio from starting a webserver."""
async def start(self):
pass
async def stop(self):
pass
class TwitchBot(commands.Bot): class TwitchBot(commands.Bot):
def __init__(self, access_token: str, client_id: str, client_secret: str, bot_id: str, channel_name: str, websocket_manager, db_user_id: int): def __init__(self, access_token: str, client_id: str, client_secret: str, bot_id: str, channel_name: str, websocket_manager, db_user_id: int):
self.websocket_manager = websocket_manager self.websocket_manager = websocket_manager
# Store our application's database user ID to avoid conflict with twitchio's internal 'owner_id' # Store our application's database user ID to avoid conflict with twitchio's internal 'owner_id'
self.db_user_id = db_user_id self.db_user_id = db_user_id
# Force the bot to use our dummy adapter that does nothing.
# This is the definitive fix to prevent any port conflicts.
adapter = NullAdapter()
super().__init__( super().__init__(
token=access_token, token=access_token,
web_server_adapter=adapter,
prefix='!', # A prefix is required but won't be used for reading chat prefix='!', # A prefix is required but won't be used for reading chat
initial_channels=[channel_name], initial_channels=[channel_name],
# These are now required by twitchio # These are now required by twitchio
client_id=client_id, client_id=client_id,
client_secret=client_secret, client_secret=client_secret,
# The 'bot_id' is the Twitch ID of the user account the bot is running as # The 'bot_id' is the Twitch ID of the user account the bot is running as
bot_id=bot_id, id=bot_id, bot_id=bot_id, id=bot_id
# Force the internal webserver to a harmless, high port to permanently
# resolve the "address already in use" error during hot-reloads.
web_server_adapter_kwargs={'host': 'localhost', 'port': 8123}
) )
self.channel_name = channel_name self.channel_name = channel_name