首页 > 解决方案 > Discord.py 临时禁令

问题描述

我只是想创建一个命令,如果我的机器人重新启动它仍然会取消禁止成员。这是我的代码:

@commands.command() # kihagyva
    async def tempban(self, ctx, member: discord.Member,seconds: int, d,*,reason=None):
        embed = discord.Embed(title="TempBanned",
                              description=f"{member.mention} has been tempbanned", 
                              colour=discord.Colour.blue())
        embed.add_field(name="reason:", value=reason, inline=False)
        embed.add_field(name="time left for the ban:", value=f"{seconds}{d}", inline=False)
        await ctx.send(embed=embed)


        if d == "s":
            await asyncio.sleep(seconds*1)

        if d == "m":
            await asyncio.sleep(seconds*60)

        if d == "h":
            await asyncio.sleep(seconds*60*60)

        if d == "d":
            await asyncio.sleep(seconds*60*60*24)

        await member.unban()
        embed = discord.Embed(title="unbanned (temp) ", description=f"unbanned {member.mention} ", colour=discord.Colour.blue())
        await ctx.send(embed=embed)

标签: discord.py

解决方案


因此,您可以将其存储在数据库中,例如 sqlite、mysql/mariadb 或 mongodb。

在 MongoDB 中,您将拥有一个集合,该集合就像一个充满用户和行会 ID 以及禁令何时到期的表。

您将使用任务每分钟或 30 秒循环一次,或者可以更长。遍历数据库并检查time nowtime stored是否小于time now. 然后解禁他们。


推荐阅读