Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Schedule不执行函数_Python_Discord_Bots_Discord.py - Fatal编程技术网

Python Schedule不执行函数

Python Schedule不执行函数,python,discord,bots,discord.py,Python,Discord,Bots,Discord.py,我的计划脚本不执行任何功能。我做了所有应该做的事情,函数没有调用,所以不要取消静音,也不要打印lol。当我把它而不是打印函数,它被执行了。我不知道我做错了什么 def unmutetempmute(ctx, member: discord.Member): print('lol') role = discord.utils.get(ctx.guild.roles, name='Wyciszony/a') if role in member.roles: u

我的计划脚本不执行任何功能。我做了所有应该做的事情,函数没有调用,所以不要取消静音,也不要打印lol。当我把它而不是打印函数,它被执行了。我不知道我做错了什么

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ł.')

        ctx.channel.send(embed=unmuteembed)
        member.send(embed=unmuteembed)
        member.remove_roles(role)
        return schedule.CancelJob

@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:
        muteembed = discord.Embed(
            title='Wyciszono {}!'.format(member.name), color=28159)

        muteembed.add_field(name='Powód:', value=reason, inline=False)
        muteembed.add_field(
            name='Czas:', value='{}m'.format(time), inline=False)
        muteembed.add_field(
            name='Administrator:', value=ctx.author.mention, inline=False)

        await ctx.channel.send(embed=muteembed)
        await member.send(embed=muteembed)

        await member.add_roles(role)
        await ctx.message.delete()

        await schedule.every(time).seconds.do(
            unmutetempmute, ctx=ctx, member=member)
        return

    errorembed = discord.Embed(
        title='Błąd!',
        color=16711686,
        description='Użytkownik już jest wyciszony!')

    await ctx.channel.send(embed=errorembed)
    await ctx.message.delete()


你也需要让时间表运行

等待时间表。每次。。。 schedule.run_pending()
您可能不想在这里使用它,因为它不是基于
asyncio
构建的,而
discord.py
是基于它构建的。您可以强制它工作,但这些解决方案相当麻烦,因为它们试图绕过
调度
不是设计用于
异步
的事实

相反,由于这是一个一次性的延迟任务,您可以简单地利用基本
asyncio

导入异步IO
异步def delayed_coro(秒:int,coro):
#将给定协同路由的执行延迟`秒`
尝试:
等待异步睡眠(秒)
等待科罗
除asyncio.Cancelled错误外:
coro.close()
#修正:这需要一个协同程序!
异步def unmutetempmute(ctx,成员:discord.member):
打印('lol')
role=discord.utils.get(ctx.guild.roles,name='wycoszony/a')
如果角色位于member.roles中:
unmuteembed=discord.Embed(
title='Odciszono{}!'格式(member.name),
颜色=28159,
description='Czas Wycisenia sięskończył
等待ctx.channel.send(嵌入=unmuteembed)
等待成员发送(嵌入=unmuteembed)
等待成员。删除_角色(角色)
@client.command()
@commands.has_权限(kick_members=True)
异步def tempmute(ctx,
成员:不和谐。成员,
时间:int,,
*,
原因是:
role=discord.utils.get(ctx.guild.roles,name='wycoszony/a')
如果不是member.roles中的角色:
#创建并发送您的邮件
#向用户添加静音角色等。
#如果需要,还可以保存“创建任务”返回的任务。
等待ctx.bot.loop.create_任务(
延迟(时间,取消静音(ctx,成员)))
返回
#错误嵌入东西
一步一步地 首先,我们创建一个小的包装程序coroutine
delay\u coro
,它在等待给定的coroutine
coro
之前需要等待几秒钟:

async def delayed_coro(秒:int,coro):
#将给定协同路由的执行延迟`秒`
尝试:
等待异步睡眠(秒)
等待科罗
除asyncio.Cancelled错误外:
coro.close()
然后,在
tempmute
中,我们不使用
schedule.every(time).seconds
,而是使用
ctx.bot.loop.create_task
在bot的事件循环上创建一个新任务,传递
unmutetempmute
包装在
delay\u coro
中:

等待ctx.bot.loop.create\u任务(
延迟(时间,取消静音(ctx,成员)))
最后,您忘了将
unmutetempmute
设置为一个协程,并等待相应的discord库调用,因此我在完整示例中修复了这个问题。请确保包含这些更改,否则它将不会运行


多一点
如果在某个时候,您发现需要安排某个项目多次运行,我建议您使用
discord.py
;当使用
discord.py

此选项不起作用时,它专门为在循环中安排任务提供帮助