【问题标题】:Discord.py tempmute command work even after rebootDiscord.py tempmute 命令即使在重新启动后也能正常工作
【发布时间】:2021-06-23 12:19:54
【问题描述】:

我有这个代码让我的不和谐机器人暂时静音:

@bot.command()
async def tempmute(ctx, member: discord.Member, time: int, d, *, reason=None):
    guild = ctx.guild
    mutedRole = discord.utils.get(guild.roles, name="Muted")
    if not mutedRole:
        mutedRole = await guild.create_role(name="Muted")

    for channel in guild.channels:
        await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True)
    for role in guild.roles:
        if role.name == "Muted":
            await member.add_roles(role)

            embed = discord.Embed(title="TempMuted!", description=f"{member.mention} has been tempmuted.", colour=discord.Colour.light_gray())
            embed.add_field(name="Reason:", value=reason, inline=False)
            embed.add_field(name="Time for the mute:", value=f"{time}{d}", inline=False)
            await ctx.send(embed=embed)

            if d == "s":
                await asyncio.sleep(time)

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

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

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

            await member.remove_roles(role)

            embed = discord.Embed(title="Unmute (temp mute expired) ", description=f"Unmuted -{member.mention} ", colour=discord.Colour.light_gray())
            await ctx.send(embed=embed)

            return

但是,我的机器人每 12-24 小时会自行重新启动,如果有人在机器人重新启动时当前处于临时静音状态,他们永远不会取消静音,因为机器人忘记了它曾经静音过他们,所以他们保持静音,直到我手动取消静音他们。有没有一种方法可以让机器人仍然记得取消静音,即使机器人在静音期间重新启动?如果是这样,请你告诉我我应该在我的代码中添加什么。谢谢!

注意:如果有帮助,我会通过 GitHub 和 Heroku 托管我的机器人,以防万一。

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    你可以用一个文本文件来做,但它不会很准确:

    @bot.command()
    async def tempmute(ctx, member: discord.Member, time: int, d, *, reason=None):
        guild = ctx.guild
        mutedRole = discord.utils.get(guild.roles, name="Muted")
        if not mutedRole:
            mutedRole = await guild.create_role(name="Muted")
    
        for channel in guild.channels:
            await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True)
    
        await member.add_roles(mutedRole)
    
        embed = discord.Embed(title="TempMuted!", description=f"{member.mention} has been tempmuted.", colour=discord.Colour.light_gray())
        embed.add_field(name="Reason:", value=reason, inline=False)
        embed.add_field(name="Time for the mute:", value=f"{time}{d}", inline=False)
        await ctx.send(embed=embed)
    
        if d == "s":
            mutetime = time
    
        if d == "m":
            mutetime = time*60
    
        if d == "h":
            mutetime = time*60*60
    
        if d == "d":
            mutetime = time*60*60*24
    
        with open("TXT_FILE_PATH", "w+") as mutetimef:
            mutetime.write(mutetime)
        
        while True:
            with open("TXT_FILE_PATH", "w+") as mutetimef:
                if int(mutetimef.read()) == 0:
                    await member.remove_roles(mutedRole)
    
                    embed = discord.Embed(title="Unmute (temp mute expired) ", description=f"Unmuted -{member.mention} ", colour=discord.Colour.light_gray())
                    await ctx.send(embed=embed)
      
                    return
                else:
                    mutetime -= 1
                    mutetimef.seek(0)
                    mutetimef.truncate()
                    mutetimef.write(mutetime)
            await asyncio.sleep(1)
    

    您还需要创建一个保存在其中的 .txt 文件。正如我所说,这不会太准确,但它可以工作。此外,如果它正在运行,您还必须定义变量或其他东西,如果它正在运行,那么它会在重新启动后运行它

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 2021-02-09
      • 2018-10-18
      • 2017-07-17
      • 1970-01-01
      相关资源
      最近更新 更多