combine asyncio and discord event loops
This commit is contained in:
parent
fcdef2e17c
commit
092682d725
1 changed files with 33 additions and 39 deletions
|
@ -1,7 +1,8 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import discord
|
|
||||||
import requests
|
import requests
|
||||||
|
import discord
|
||||||
|
from discord.ext import commands, tasks
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
|
|
||||||
|
@ -9,25 +10,29 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
bot_token = os.environ.get('DISCORDBOT_TOKEN', 'token')
|
bot_token = os.environ.get('DISCORDBOT_TOKEN', 'token')
|
||||||
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
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.messages = True
|
||||||
|
intents.guilds = True
|
||||||
|
intents.reactions = True
|
||||||
|
|
||||||
|
# Discord client
|
||||||
|
bot = commands.Bot(command_prefix="!", intents=intents)
|
||||||
|
|
||||||
# Scheduler
|
# Scheduler
|
||||||
scheduler = AsyncIOScheduler()
|
scheduler = AsyncIOScheduler()
|
||||||
|
|
||||||
# Intents
|
@bot.event
|
||||||
intents = discord.Intents.default()
|
|
||||||
intents.message_content = True
|
|
||||||
|
|
||||||
# Client
|
|
||||||
client = discord.Client(intents=intents)
|
|
||||||
|
|
||||||
# Define an event when the bot is ready
|
|
||||||
@client.event
|
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f'Logged in as {client.user}')
|
print(f'Logged in as {bot.user.name} ({bot.user.id})')
|
||||||
|
scheduler.start()
|
||||||
|
|
||||||
async def hello(message):
|
@bot.command(name='hello')
|
||||||
await message.channel.send('Hello!')
|
async def hello(ctx):
|
||||||
|
await ctx.channel.send('Hello!')
|
||||||
|
|
||||||
async def epg(message):
|
@bot.command(name='epg')
|
||||||
|
async def epg(ctx):
|
||||||
try:
|
try:
|
||||||
db_url = f'https://{scheduler_hostname}/database'
|
db_url = f'https://{scheduler_hostname}/database'
|
||||||
if requests.get(db_url).status_code == 200:
|
if requests.get(db_url).status_code == 200:
|
||||||
|
@ -35,34 +40,23 @@ async def epg(message):
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
content = response.text
|
content = response.text
|
||||||
await message.channel.send(content)
|
await ctx.channel.send(content)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
@client.event
|
@bot.command(name='start_task')
|
||||||
async def on_message(message):
|
async def start_task(ctx):
|
||||||
if message.author == client.user:
|
# Schedule a task to run every 5 seconds
|
||||||
return
|
scheduler.add_job(my_task, 'interval', seconds=5, id='my_task_id')
|
||||||
|
#scheduler.add_job(tick, 'interval', seconds=5, id='tick_id')
|
||||||
|
|
||||||
if message.content.startswith('!hello'):
|
@tasks.loop(seconds=10)
|
||||||
await hello(message)
|
async def my_task():
|
||||||
|
# Your asynchronous task goes here
|
||||||
|
print("Running my_task")
|
||||||
|
|
||||||
if message.content.startswith('!epg'):
|
async def tick():
|
||||||
await epg(message)
|
|
||||||
|
|
||||||
client.run(bot_token)
|
|
||||||
|
|
||||||
def tick():
|
|
||||||
print('Tick! The time is: %s' % datetime.now())
|
print('Tick! The time is: %s' % datetime.now())
|
||||||
|
|
||||||
scheduler.add_job(tick, 'interval', seconds=3)
|
# Run the bot with your token
|
||||||
scheduler.start()
|
bot.run(bot_token)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue