1

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?

OKAlex
  • 73
  • 6
  • 1
    Why not use `tasks` for that? https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html – moinierer3000 Oct 10 '22 at 20:10
  • https://stackoverflow.com/questions/61920560/how-to-loop-a-task-in-discord-py Found a solution similar to this using tasks. Thanks for the suggestion! – OKAlex Oct 10 '22 at 20:37

0 Answers0