From be3b09d3a3e1148df111ec5523c768a004511dbd Mon Sep 17 00:00:00 2001 From: deflax Date: Wed, 17 Jan 2024 02:42:32 +0200 Subject: [PATCH] announce live channel --- src/discordbot/discordbot.py | 76 +++++++++++++++++------------------- variables.env.dist | 1 + 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/discordbot/discordbot.py b/src/discordbot/discordbot.py index be79ef4..403cb96 100644 --- a/src/discordbot/discordbot.py +++ b/src/discordbot/discordbot.py @@ -8,6 +8,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler # Read env variables bot_token = os.environ.get('DISCORDBOT_TOKEN', 'token') +announce_channel_id = os.environ.get('DISCORDBOT_LIVE_CHANNEL_ID', 'disabled') scheduler_hostname = os.environ.get('SCHEDULER_API_HOSTNAME', 'tv.example.com') # Discord API Intents @@ -25,62 +26,55 @@ bot = commands.Bot(command_prefix="!", intents=intents) # Scheduler scheduler = AsyncIOScheduler() -counter = 0 +database = {} +# Bot functions @bot.event async def on_ready(): - print(f'Logged in as {bot.user.name} ({bot.user.id})') + # Schedule a database update to run every 5 seconds + scheduler.add_job(func=update_database, trigger='interval', seconds=5, id='update_database') scheduler.start() @bot.command(name='hello') async def hello(ctx): - await ctx.channel.send('Hello!') + author_name = ctx.author.name + await ctx.channel.send(f'hi, {author_name}! >^.^<') @bot.command(name='epg') async def epg(ctx): - try: - db_url = f'https://{scheduler_hostname}/database' - if requests.get(db_url).status_code == 200: - response = requests.get(db_url) - response.raise_for_status() - content = response.json() - await ctx.channel.send('epg:') - - if content != {}: - for key, value in content.items(): - if value['start_at'] == 'now' or value['start_at'] == 'never': - await ctx.channel.send('x') - continue - else: - await ctx.channel.send('x') - await ctx.channel.send(value['start_at']) - else: - await ctx.channel.send('Empty database!') - except Exception as e: - print(e) - + global database + await ctx.channel.send('epg:') + if database != {}: + for key, value in database.items(): + if value['start_at'] != 'now' and value['start_at'] != 'never': + await ctx.channel.send(f'{value['name']} starts at {value['start_at']}h UTC') + else: + await ctx.channel.send('Empty database!') + @bot.command(name='time') async def time(ctx): await ctx.channel.send('The time is: `%s`' % datetime.now()) -@bot.command(name='start') -async def start_task(ctx): - # Schedule a task to run every 5 seconds - scheduler.add_job(func=my_task, trigger='interval', seconds=5, id='my_task_id') - #scheduler.add_job(func=tick, id='tick_id', args=(ctx)) - #channel = bot.get_channel(channel_id) - #if channel: - # # Send the message to the specified channel - # await channel.send(message) +# Helper functions +async def update_database(): + global database + db_url = f'https://{scheduler_hostname}/database' + if requests.get(db_url).status_code == 200: + response = requests.get(db_url) + response.raise_for_status() + database = response.json() + if database != {}: + for key, value in database.items(): + if value['start_at'] == 'now': + scheduler.add_job(func=announce_live_channel, seconds=60, id='announce_live_channel') + -@bot.command(name='show') -async def show_task(ctx): - global counter - await ctx.channel.send(str(counter)) - -async def my_task(): - global counter - counter += 1 +async def announce_live_channel(): + if announce_channel_id == 'disabled': + return + else: + live_channel = bot.get_channel(announce_channel_id) + await live_channel.send(f'{announce_channel_id}') # Run the bot with your token asyncio.run(bot.run(bot_token)) diff --git a/variables.env.dist b/variables.env.dist index b406b12..bc92bd9 100644 --- a/variables.env.dist +++ b/variables.env.dist @@ -7,3 +7,4 @@ CORE_API_AUTH_PASSWORD=changeme SCHEDULER_LOG_LEVEL=warn SCHEDULER_API_HOSTNAME=tv.example.com DISCORDBOT_TOKEN=changeme +DISCORDBOT_LIVE_CHANNEL_ID=disabled