announce live channel

This commit is contained in:
deflax 2024-01-17 02:42:32 +02:00
parent 4ab45cd8bc
commit be3b09d3a3
2 changed files with 36 additions and 41 deletions

View file

@ -8,6 +8,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
# Read env variables # Read env variables
bot_token = os.environ.get('DISCORDBOT_TOKEN', 'token') 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') scheduler_hostname = os.environ.get('SCHEDULER_API_HOSTNAME', 'tv.example.com')
# Discord API Intents # Discord API Intents
@ -25,62 +26,55 @@ bot = commands.Bot(command_prefix="!", intents=intents)
# Scheduler # Scheduler
scheduler = AsyncIOScheduler() scheduler = AsyncIOScheduler()
counter = 0 database = {}
# Bot functions
@bot.event @bot.event
async def on_ready(): 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() scheduler.start()
@bot.command(name='hello') @bot.command(name='hello')
async def hello(ctx): 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') @bot.command(name='epg')
async def epg(ctx): async def epg(ctx):
try: 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()
content = response.json()
await ctx.channel.send('epg:') await ctx.channel.send('epg:')
if database != {}:
if content != {}: for key, value in database.items():
for key, value in content.items(): if value['start_at'] != 'now' and value['start_at'] != 'never':
if value['start_at'] == 'now' or value['start_at'] == 'never': await ctx.channel.send(f'{value['name']} starts at {value['start_at']}h UTC')
await ctx.channel.send('x')
continue
else:
await ctx.channel.send('x')
await ctx.channel.send(value['start_at'])
else: else:
await ctx.channel.send('Empty database!') await ctx.channel.send('Empty database!')
except Exception as e:
print(e)
@bot.command(name='time') @bot.command(name='time')
async def time(ctx): async def time(ctx):
await ctx.channel.send('The time is: `%s`' % datetime.now()) await ctx.channel.send('The time is: `%s`' % datetime.now())
@bot.command(name='start') # Helper functions
async def start_task(ctx): async def update_database():
# Schedule a task to run every 5 seconds global database
scheduler.add_job(func=my_task, trigger='interval', seconds=5, id='my_task_id') db_url = f'https://{scheduler_hostname}/database'
#scheduler.add_job(func=tick, id='tick_id', args=(ctx)) if requests.get(db_url).status_code == 200:
#channel = bot.get_channel(channel_id) response = requests.get(db_url)
#if channel: response.raise_for_status()
# # Send the message to the specified channel database = response.json()
# await channel.send(message) 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(): async def announce_live_channel():
global counter if announce_channel_id == 'disabled':
counter += 1 return
else:
live_channel = bot.get_channel(announce_channel_id)
await live_channel.send(f'{announce_channel_id}')
# Run the bot with your token # Run the bot with your token
asyncio.run(bot.run(bot_token)) asyncio.run(bot.run(bot_token))

View file

@ -7,3 +7,4 @@ CORE_API_AUTH_PASSWORD=changeme
SCHEDULER_LOG_LEVEL=warn SCHEDULER_LOG_LEVEL=warn
SCHEDULER_API_HOSTNAME=tv.example.com SCHEDULER_API_HOSTNAME=tv.example.com
DISCORDBOT_TOKEN=changeme DISCORDBOT_TOKEN=changeme
DISCORDBOT_LIVE_CHANNEL_ID=disabled