【问题标题】:discord.py Timed Mute command not workingdiscord.py 定时静音命令不起作用
【发布时间】:2022-01-19 19:37:33
【问题描述】:
@client.command(aliases=['tempmute'])
@commands.has_permissions(manage_messages=True)
async def tmute(ctx, member: discord.Member=None, time=None, *, reason=None):
    if not member:
           await ctx.send("You must mention a member to mute!")
    elif not time:
           await ctx.send("You must mention a time!")
    else:
           if not reason:
                  reason="No reason given"

    try:
        seconds = time[:-1] 
        duration = time[-1] 
        if duration == "s":
            seconds = seconds * 1
        elif duration == "m":
            seconds = seconds * 60
        elif duration == "h":
            seconds = seconds * 60 * 60
        elif duration == "d":
            seconds = seconds * 86400
        else:
            await ctx.send("Invalid duration input")
        return
    except Exception as e:
        print(e)
    await ctx.send("Invalid time input")
    return
    guild = ctx.guild
    Muted = discord.utils.get(guild.roles, name="Muted")
    if not Muted:
        Muted = 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, read_messages=False)
    await member.add_roles(Muted, reason=reason)
    muted_embed = discord.Embed(title="Muted a user", description=f"{member.mention} Was muted by {ctx.author.mention} for {reason} to {time}")
    await ctx.send(embed=muted_embed)
    await asyncio.sleep(seconds)
    await member.remove_roles(Muted)
    unmute_embed = discord.Embed(title="Mute over!", description=f'{ctx.author.mention} muted to {member.mention} for {reason} is over after {time}')
    await ctx.send(embed=unmute_embed)

我没有收到任何错误,代码工作正常,直到我到达命令的时间操作部分,所以 !mute @Test 1d testreason 代码工作正常,直到我到达 1d 部分然后停止工作,我有不知道为什么,请帮忙。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您的seconds 变量是一个字符串。因此乘法也返回字符串。您需要像这样将seconds 转换为int

    seconds = int(time[:-1])
    

    另请注意,将asyncio.sleep 用于定时静音命令是一种不好的做法。您应该使用任何调度程序或编写自己的调度程序。

    【讨论】:

    • 我现在收到错误:invalid literal for int() with base 10: 'm'
    • pastebin.com/0iduCFrB 这是时间转换器类的源代码。在您的代码中定义它。然后写time: TimeConverter。 D.py 将使用转换器类转换参数。这是一种非常简单正确的时长转换方法。
    • time 变量代表一秒的用户输入。例如 1h30s = 3630 秒。
    • 在 d.py here 中阅读有关转换器的更多信息。
    猜你喜欢
    • 2020-10-28
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多