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
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()
global database
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'])
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!')
except Exception as e:
print(e)
@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))

View file

@ -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