Changelog - Version 3.14 (2024-04-16)
Added
- Added a new function
timed_message_loopto handle scheduling of timed messages with a sleep interval of 150 seconds.async def timed_message_loop(): bot_logger.info(f"Started timed messages loop") while True: await asyncio.sleep(150) await timed_message()
Changed
- Updated some error handling and logging in the Twitch Shoutout Function
- Changed the timed message queueing system from
queuetoasyncio queuefor the scheduling of messages.scheduled_tasks = asyncio.Queue() - Updated the
timed_messagefunction to check if a message is already scheduled before queuing it for sending.async def timed_message(): if stream_online: cursor.execute('SELECT interval, message FROM timed_messages') messages = cursor.fetchall() bot_logger.info(f"Timed Messages: {messages}") for interval, message in messages: if message in scheduled_tasks: return bot_logger.info(f"Timed Message: {message} has a {interval} minute wait.") time_now = datetime.now() send_time = time_now + timedelta(minutes=int(interval)) wait_time = (send_time - time_now).total_seconds() bot_logger.info(f"Scheduling message: '{message}' to be sent in {wait_time} seconds") task = asyncio.create_task(send_timed_message(message, wait_time)) scheduled_tasks.append(task) # Keep track of the task else: # Cancel all scheduled tasks if the stream goes offline for task in scheduled_tasks: task.cancel() scheduled_tasks.clear() # Clear the list of tasks - Removed the interval parameter from the
send_timed_message()function as it’s no longer needed.async def send_timed_message(message): try: if stream_online: channel = bot.get_channel(CHANNEL_NAME) bot_logger.info(f"Sending Timed Message: {message}") await channel.send(message) else: bot_logger.info("Stream is offline. Message not sent.") except asyncio.CancelledError: bot_logger.info(f"Task cancelled for {message}") pass