Discord.py 碰撞提醒命令

时间:2021-06-11 20:20:16

标签: python discord.py

我一直在尝试编写一个碰撞提醒/阻止disboard在这段时间内处理命令,并且没有任何类似于我在互联网上找到的东西: 我当前的代码是:

async def bump_reminder(ctx: discord.ext.commands.Context, action: str):
    disboard=ctx.guild.get_member(302050872383242240)
    if ctx.channel.id!=852646981759270972 :
        await ctx.send("Use this command at the bump channel")
        return
    if not action.casefold() == "bump":
        return
    if not disboard.status == discord.Status.online:
        await ctx.send(embed=(discord.Embed(color=discord.Colour.red,description=(f"Whoa {disboard.mention} appears to be offline right now!\nI'll monitor the bump bot's status and notify everyone when it comes back online."))))
        return
    else :
        await ctx.send("thanks for bumping,disboard will be back in two hours to be bumped:)")
        await ctx.channel.set_permissions(disboard, view_channel =False)
        lock_disboard_Out_for_two_hours.start()
        return
@tasks.loop(hours=2)
async def lock_disboard_Out_for_two_hours():
    guild =client.get_guild(844231449014960160)
    disband_role=discord.utils.get(guild.roles, name="DISBOARD")
    bump_role=discord.utils.get(guild.roles, name="Bumpers")
    channel=guild.get_channel(852646981759270972)
    await channel.set_permissions(disband_role, view_channel = True)
    await channel.send(f"Discord is back, {bump_role.mention} come bump the server!")
    lock_disboard_Out_for_two_hours.cancel()

有没有更好的方法来做到这一点,或者我该怎么做才能使这项工作发挥作用,因为循环 task.loop() 首先运行,然后等待 2 小时,然后再次运行,但我只希望它在 2 后运行小时,我希望有一个简单的方法让这个命令工作。

1 个答案:

答案 0 :(得分:1)

我必须阅读您的问题 4 次才能理解它。无论如何,Discord Docs 表示您可以访问当前循环值(作为索引,第一个循环为 0,第二个为 1,第 10 个循环为 9 等)

你可以这样使用它:

@tasks.loop(seconds=1.0, count=6)
async def slow_count(my_msg):
    current_loop = slow_count.current_loop

因此,您可以在发送所有消息之前检查 lock_disboard_Out_for_two_hours.current_loop 是否为零,这意味着这是第一次迭代。

@tasks.loop(hours=2)
async def lock_disboard_Out_for_two_hours():
    if lock_disboard_Out_for_two_hours.current_loop == 0:
        return None
    guild =client.get_guild(844231449014960160)
    disband_role=discord.utils.get(guild.roles, name="DISBOARD")
    bump_role=discord.utils.get(guild.roles, name="Bumpers")
    channel=guild.get_channel(852646981759270972)
    await channel.set_permissions(disband_role, view_channel = True)
    await channel.send(f"Discord is back, {bump_role.mention} come bump the server!")
    lock_disboard_Out_for_two_hours.cancel()