2024-01-16 10:42:22 -05:00
|
|
|
import asyncio
|
2024-01-14 21:45:31 -05:00
|
|
|
import os
|
2024-01-15 22:32:57 -05:00
|
|
|
import requests
|
2024-01-16 11:06:57 -05:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands, tasks
|
2024-01-16 10:42:22 -05:00
|
|
|
from datetime import datetime
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
2024-01-16 20:18:58 -05:00
|
|
|
from apscheduler.jobstores.base import JobLookupError
|
2024-01-14 21:45:31 -05:00
|
|
|
|
|
|
|
# Read env variables
|
|
|
|
bot_token = os.environ.get('DISCORDBOT_TOKEN', 'token')
|
2024-01-16 19:42:32 -05:00
|
|
|
announce_channel_id = os.environ.get('DISCORDBOT_LIVE_CHANNEL_ID', 'disabled')
|
2024-01-14 21:45:31 -05:00
|
|
|
scheduler_hostname = os.environ.get('SCHEDULER_API_HOSTNAME', 'tv.example.com')
|
|
|
|
|
2024-01-16 11:06:57 -05:00
|
|
|
# Discord API Intents
|
2024-01-16 11:16:02 -05:00
|
|
|
intents = discord.Intents.all()
|
|
|
|
intents.members = True
|
|
|
|
intents.guilds = True
|
|
|
|
intents.messages = True
|
|
|
|
intents.reactions = True
|
|
|
|
intents.presences = True
|
|
|
|
intents.message_content = True
|
2024-01-14 21:45:31 -05:00
|
|
|
|
2024-01-16 11:06:57 -05:00
|
|
|
# Discord client
|
|
|
|
bot = commands.Bot(command_prefix="!", intents=intents)
|
2024-01-14 21:45:31 -05:00
|
|
|
|
2024-01-16 11:06:57 -05:00
|
|
|
# Scheduler
|
|
|
|
scheduler = AsyncIOScheduler()
|
2024-01-15 16:48:20 -05:00
|
|
|
|
2024-01-16 19:42:32 -05:00
|
|
|
database = {}
|
2024-01-16 11:50:59 -05:00
|
|
|
|
2024-01-16 19:42:32 -05:00
|
|
|
# Bot functions
|
2024-01-16 11:06:57 -05:00
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
2024-01-16 19:42:32 -05:00
|
|
|
# Schedule a database update to run every 5 seconds
|
|
|
|
scheduler.add_job(func=update_database, trigger='interval', seconds=5, id='update_database')
|
2024-01-16 11:06:57 -05:00
|
|
|
scheduler.start()
|
|
|
|
|
|
|
|
@bot.command(name='hello')
|
|
|
|
async def hello(ctx):
|
2024-01-16 19:42:32 -05:00
|
|
|
author_name = ctx.author.name
|
|
|
|
await ctx.channel.send(f'hi, {author_name}! >^.^<')
|
2024-01-16 11:06:57 -05:00
|
|
|
|
|
|
|
@bot.command(name='epg')
|
|
|
|
async def epg(ctx):
|
2024-01-16 19:42:32 -05:00
|
|
|
global database
|
|
|
|
await ctx.channel.send('epg:')
|
|
|
|
if database != {}:
|
|
|
|
for key, value in database.items():
|
2024-01-16 20:13:45 -05:00
|
|
|
item_name = value['name']
|
|
|
|
item_start = value['start_at']
|
|
|
|
if item_start != 'now' and item_start != 'never':
|
|
|
|
await ctx.channel.send(f'{item_name} starts at {item_start}h UTC')
|
2024-01-16 19:42:32 -05:00
|
|
|
else:
|
|
|
|
await ctx.channel.send('Empty database!')
|
|
|
|
|
2024-01-16 11:56:20 -05:00
|
|
|
@bot.command(name='time')
|
2024-01-16 11:57:34 -05:00
|
|
|
async def time(ctx):
|
2024-01-16 20:20:55 -05:00
|
|
|
await ctx.channel.send(f'The time is: {datetime.now()} UTC')
|
2024-01-16 11:50:59 -05:00
|
|
|
|
2024-01-16 19:42:32 -05:00
|
|
|
# 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':
|
2024-01-16 20:13:45 -05:00
|
|
|
scheduler.add_job(func=announce_live_channel, seconds=60, id='announce_live_channel')
|
|
|
|
return
|
2024-01-16 20:18:58 -05:00
|
|
|
try:
|
|
|
|
job = scheduler.get_job('announce_live_channel')
|
|
|
|
if job:
|
|
|
|
scheduler.remove_job('announce_live_channel')
|
|
|
|
live_channel = bot.get_channel(announce_channel_id)
|
|
|
|
await live_channel.send(f'{announce_channel_id} removed')
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
except JobLookupError:
|
|
|
|
return
|
2024-01-16 11:50:59 -05:00
|
|
|
|
2024-01-16 19:42:32 -05:00
|
|
|
async def announce_live_channel():
|
|
|
|
if announce_channel_id == 'disabled':
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
live_channel = bot.get_channel(announce_channel_id)
|
2024-01-16 20:13:45 -05:00
|
|
|
await live_channel.send(f'{announce_channel_id} ')
|
2024-01-16 10:42:22 -05:00
|
|
|
|
2024-01-16 11:06:57 -05:00
|
|
|
# Run the bot with your token
|
2024-01-16 20:13:45 -05:00
|
|
|
asyncio.run(bot.run(bot_token))
|