Akash Bahetra
Akash Bahetra

Reputation: 23

How to call async function in a synchronus function?

I'm trying to make a discord bot that sends Hey!! in a specific channel on a specific time daily. but it's giving me an error

import discord
import schedule
    
bot = commands.Bot(command_prefix = "^")
    
@bot.event
async def on_ready():
   schedule.every().day.at("18:00").do(job)
        
   while 1:
       schedule.run_pending()
       time.sleep(1)

async def job():
        channel = bot.get_channel(72246xxxxxxxxx)
        await channel.send("Hey!!")
    

RuntimeWarning: coroutine 'job' was never awaited

Upvotes: 2

Views: 66

Answers (1)

RiveN
RiveN

Reputation: 2653

@bot.event
async def on_ready():
    while True:
        await asyncio.sleep(1)
        x=datetime.today() #identify the time right now
        y=x.replace(hour=18, minute=0, second=0) #here you can modify time
        delta_t=y-x
        secs=delta_t.seconds
        print(secs) #you can delete this line if you would like to disable visual countdown
        if secs == 0: #when countdown have 0 seconds left it sends the message
            channel = bot.get_channel(1234567890) #your channel id
            await channel.send("Hey!!") #your message

I know I modified your code a lot, but I preferred to use a built-in datetime library. If you want to use schedule library you can try to use asyncio.run(job), but I'm not sure if this would work.

Upvotes: 1

Related Questions