refactor announement

This commit is contained in:
deflax 2024-01-17 05:27:30 +02:00
parent eacea567ba
commit b3480c8bd2

View file

@ -72,28 +72,33 @@ async def update_database():
response = requests.get(db_url) response = requests.get(db_url)
response.raise_for_status() response.raise_for_status()
database = response.json() database = response.json()
if database != {}: else:
for key, value in database.items(): logger_discord.error('Cannot connect to the database!')
stream_name = value['name'] return
stream_start_at = value['start_at']
if stream_start_at == 'now': # Search for live streams
try: if database != {}:
job = scheduler.get_job('announce_live_channel') for key, value in database.items():
if not job: stream_name = value['name']
logger_discord.info(f'{stream_name} live stream detected!') stream_start_at = value['start_at']
scheduler.add_job(func=announce_live_channel, trigger='interval', seconds=60, id='announce_live_channel', args=(stream_name,)) if stream_start_at == 'now':
return # Check if the job already exists
except JobLookupError: if scheduler.get_job('announce_live_channel') is None:
pass # Job doesn't exist, so add it
try: logger_discord.info(f'{stream_name} live stream detected!')
job = scheduler.get_job('announce_live_channel') scheduler.add_job(func=announce_live_channel, trigger='interval', seconds=60, id='announce_live_channel', args=(stream_name,))
if job: # Exit the loop since we found one
scheduler.remove_job('announce_live_channel') return
live_channel = bot.get_channel(announce_channel_id) else:
logger_discord.info(f'{stream_name} finished') # Exit the loop since we already have a announcement job
await live_channel.send(f'{stream_name} finished') return
except JobLookupError:
pass # Cleanup the announce job
if scheduler.get_job('announce_live_channel') is not None:
scheduler.remove_job('announce_live_channel')
#live_channel = bot.get_channel(announce_channel_id)
logger_discord.info(f'{stream_name} finished')
#await live_channel.send(f'{stream_name} finished')
async def announce_live_channel(stream_name): async def announce_live_channel(stream_name):
if announce_channel_id == 'disabled': if announce_channel_id == 'disabled':