I'm having some trouble trying to continuously run an async function while my discord bot is running at the same time.
This is a slimmed down version of my code which produces the same error in the same manner.
import discord.member
from discord.ext import commands, tasks
import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
intents = discord.Intents().all()
intents.members = True
client = commands.Bot(command_prefix="$", intents=intents)
async def function():
channel = client.get_channel(910690310022107156)
await channel.send(f'Hello World!')
scheduler = AsyncIOScheduler()
scheduler.add_job(function, 'interval', minutes=.1)
scheduler.start()
asyncio.get_event_loop().run_forever()
client.run('TOKEN')
If I run asyncio.get_event_loop().run_forever()
, line 21 client.run('TOKEN')
never runs which means the bot is never online and can't await channel.send(f'Hello World!')
. If I put client.run('TOKEN')
before the asyncio loop, the loop never runs and the function is never executed. Is there a way to run the bot and continuously schedule/ execute functions at the same time?