2024-01-16 10:42:22 -05:00
|
|
|
import asyncio
|
2024-01-14 21:45:31 -05:00
|
|
|
import os
|
|
|
|
import discord
|
2024-01-15 22:32:57 -05:00
|
|
|
import requests
|
2024-01-16 10:42:22 -05:00
|
|
|
from datetime import datetime
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
2024-01-14 21:45:31 -05:00
|
|
|
|
|
|
|
# Read env variables
|
|
|
|
bot_token = os.environ.get('DISCORDBOT_TOKEN', 'token')
|
|
|
|
scheduler_hostname = os.environ.get('SCHEDULER_API_HOSTNAME', 'tv.example.com')
|
|
|
|
|
2024-01-16 10:42:22 -05:00
|
|
|
# Scheduler
|
|
|
|
scheduler = AsyncIOScheduler()
|
|
|
|
|
2024-01-14 22:07:15 -05:00
|
|
|
# Intents
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.message_content = True
|
2024-01-14 21:45:31 -05:00
|
|
|
|
2024-01-16 10:42:22 -05:00
|
|
|
# Client
|
2024-01-14 22:07:15 -05:00
|
|
|
client = discord.Client(intents=intents)
|
2024-01-14 21:45:31 -05:00
|
|
|
|
|
|
|
# Define an event when the bot is ready
|
2024-01-14 22:07:15 -05:00
|
|
|
@client.event
|
2024-01-14 21:45:31 -05:00
|
|
|
async def on_ready():
|
2024-01-14 22:07:15 -05:00
|
|
|
print(f'Logged in as {client.user}')
|
2024-01-14 21:45:31 -05:00
|
|
|
|
2024-01-15 16:48:20 -05:00
|
|
|
async def hello(message):
|
|
|
|
await message.channel.send('Hello!')
|
|
|
|
|
2024-01-15 22:32:57 -05:00
|
|
|
async def epg(message):
|
|
|
|
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.text
|
|
|
|
await message.channel.send(content)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
2024-01-14 22:07:15 -05:00
|
|
|
@client.event
|
|
|
|
async def on_message(message):
|
|
|
|
if message.author == client.user:
|
|
|
|
return
|
|
|
|
|
|
|
|
if message.content.startswith('!hello'):
|
2024-01-15 16:48:20 -05:00
|
|
|
await hello(message)
|
2024-01-15 22:32:57 -05:00
|
|
|
|
|
|
|
if message.content.startswith('!epg'):
|
|
|
|
await epg(message)
|
2024-01-14 22:07:15 -05:00
|
|
|
|
2024-01-16 10:42:22 -05:00
|
|
|
client.run(bot_token)
|
|
|
|
|
|
|
|
def tick():
|
|
|
|
print('Tick! The time is: %s' % datetime.now())
|
|
|
|
|
|
|
|
scheduler.add_job(tick, 'interval', seconds=3)
|
|
|
|
scheduler.start()
|
|
|
|
|
|
|
|
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
|
|
|
|
|
|
|
|
# Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
|
|
|
|
try:
|
|
|
|
asyncio.get_event_loop().run_forever()
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
pass
|
|
|
|
|