Discord.py don';t tempmute命令不需要空间

Discord.py don';t tempmute命令不需要空间,discord.py,Discord.py,我的discord.py bot和tempmute命令有以下代码: @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:

我的discord.py bot和tempmute命令有以下代码:

@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)
    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.red())
            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

但是,当使用该命令时,如果我想要10分钟的静音,则必须按如下方式键入:“!tempmute@user 10 m”。注意它是怎样的“10米”。我该如何使“10米”工作(没有空间)。所以“!tempmute@user 10m”?如果用户写入时没有空格,则会出现错误“discord.ext.commands.errors.BadArgument:将参数“time”转换为“int”失败,可能是因为用户无法识别结尾带有字母的数字。谢谢

解决这个问题的最好方法是创建自己的转换器,并在参数中键入提示类。有案可查

因此,与其在命令函数中转换时间,不如在转换器中转换时间,使语法更清晰

转换器示例

time\u regex=re.compile(r“(\d{1,5}(?:[,]?\d{1,5}))([smhd]))
时间_dict={“h”:3600,“s”:1,“m”:60,“d”:86400}
类时间转换器(commands.Converter):
异步def转换(self、ctx、参数):
matches=time\u regex.findall(argument.lower())
时间=0
对于匹配中的v,k:
尝试:
时间+=时间[k]*浮动(v)
除KeyError外:
raise commands.BadArgument(f“{k}是无效的时间键!h/m/s/d有效!”)
除值错误外:
raise commands.BadArgument(f“{v}不是数字!”)
返回时间
这是一个时间转换器的示例,它将使用正则表达式,并在几秒钟内将其转换为int。它接受
,例如
2d

库在命令调用期间调用TimeConverter().convert,因此我们创建了一个名为convert的方法,该方法接受上下文对象和参数作为str。您所要做的就是返回一些内容,或者在出现错误时引发错误

为了使用它,您将按如下方式执行

@bot.command()
异步def tempmute(ctx,成员:discord.member,时间:TimeConverter,*,原因=无):
...
等待成员。添加_角色(角色)
等待asyncio.sleep(时间)
等待成员。删除_角色(角色)
...
命令调用将是


!tempmute@user 2d这是我自己一直在使用tempmute命令的原因。我想说,这个命令在一个简单的字典中可以很好地工作。如果你想转换时间单位,一个简单的字典就可以了,你不需要太多的代码

首先我会添加代码,然后给出一个简短的解释

代码: 说明: 我的代码几乎包含了静音命令所需的所有内容。只有角色具有执行此命令的
manage\u role
权限的成员。另外,如果有人试图使服务器管理员的成员静音,则会弹出一个嵌入窗口,说,
成员是管理员

代码还有一个方面,如果没有给出任何原因,它将不会显示任何错误,但会发布为
no-reason
命令制作的嵌入,即,如果没有提到任何原因。它还检查服务器是否没有名为
静音的角色。如果角色存在,它将简单地使用它,否则首先它将创建一个名为
mute
的角色,拒绝重要权限并将其用于mute

现在是时间转换。用于此操作的行:

time_convert = {'s' : 1 , 'm' : 60 , 'h' : 3600 , 'd' : 86400, 'y' : 31536000}
此命令将
second
作为转换的基本值,然后以秒为单位每隔一个单位进行转换。这里
s
等于
1
m
等于
60
,因为I分钟等于60秒。同样地,对其他单位也做同样的处理,我们把一切都推到秒。本词典将所有内容转换为秒,并使用
asyncio
为静音创建计时器


希望这有帮助,如果你还有任何疑问,请随时问我

谢谢大家!

time_convert = {'s' : 1 , 'm' : 60 , 'h' : 3600 , 'd' : 86400, 'y' : 31536000}