You probably don't want to use schedule
here, as it's not built with asyncio
in mind, whereas discord.py
is built on it. You could force it to work like in this question, but those solutions are pretty cumbersome since they try to work around the fact that schedule
wasn't designed to be used with asyncio
.
Instead, since this is a one-off delayed task, you can simply leverage basic asyncio
:
import asyncio
async def delayed_coro(seconds: int, coro):
# Delays execution of the given coroutine by `seconds`
try:
await asyncio.sleep(seconds)
await coro
except asyncio.CancelledError:
coro.close()
# FIXED: This needs to be a coroutine!
async def unmutetempmute(ctx, member: discord.Member):
print('lol')
role = discord.utils.get(ctx.guild.roles, name='Wyciszony/a')
if role in member.roles:
unmuteembed = discord.Embed(
title='Odciszono {}!'.format(member.name),
color=28159,
description='Czas wyciszenia si? skończy?.')
await ctx.channel.send(embed=unmuteembed)
await member.send(embed=unmuteembed)
await member.remove_roles(role)
@client.command()
@commands.has_permissions(kick_members=True)
async def tempmute(ctx,
member: discord.Member,
time: int,
*,
reason='Nie podano powodu'):
role = discord.utils.get(ctx.guild.roles, name='Wyciszony/a')
if not role in member.roles:
# create and send your embed
# add muted role to user, etc.
# You can also save the task returned by `create_task` if you want.
await ctx.bot.loop.create_task(
delay_coro(time, unmutetempmute(ctx, member)))
return
# errorembed stuff
Step by Step
First, we create a small wrapper coroutine delay_coro
that takes a number of seconds to wait before awaiting the given coroutine coro
:
async def delayed_coro(seconds: int, coro):
# Delays execution of the given coroutine by `seconds`
try:
await asyncio.sleep(seconds)
await coro
except asyncio.CancelledError:
coro.close()
Then, in tempmute
, instead of using schedule.every(time).seconds
, we create a new task on the bot's event loop with ctx.bot.loop.create_task
, passing in unmutetempmute
wrapped within delay_coro
:
await ctx.bot.loop.create_task(
delay_coro(time, unmutetempmute(ctx, member)))
Lastly, you forgot to make your unmutetempmute
a coroutine and await the appropriate discord library calls, so I fixed that in the full example. Be sure to include those changes or it will not run.
A Little Extra
If at some point you find that you need to schedule something to run more than once, I recommend the tasks extension that comes with discord.py
; it provides helpers specifically for scheduling tasks in a loop when using discord.py
.