首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord py mute命令指定原因,不带时间

Discord py mute命令指定原因,不带时间
EN

Stack Overflow用户
提问于 2021-03-04 21:32:40
回答 1查看 246关注 0票数 0

所以我昨天做了一个mute命令,并将它转换为一个可选的tempmute,它工作得很好,除非我指定了一个原因而没有时间。显然,这会产生一个问题,但遗憾的是,我不知道如何解决它。也就是说:N?静音用户时间原因起作用,但N?静音用户原因不起作用。如果我不指定原因和时间,它会将它们静音2年。我想要能够用一个理由来烫人,也可以用一个理由(已经起作用了)让人暂时静音。代码如下:

代码语言:javascript
复制
@commands.command()
@commands.has_permissions(manage_roles=True)
async def mute(self, ctx, member: discord.Member, time="670d", *, reason="no reason provided"):
    role = discord.utils.get(ctx.guild.roles, name="Muted")
    admin = discord.utils.get(ctx.guild.roles, name="Admin")
    mod = discord.utils.get(ctx.guild.roles, name="Moderator")
    guild = ctx.guild
    pfp = member.avatar_url
    time_convert = {"s":1, "m":60, "h":3600,"d":86400}
    tempmute= int(time[0]) * time_convert[time[-1]]
    if admin in member.roles or mod in member.roles:
        embed = discord.Embed(title="Mute failed", description="Staff cannot be muted.", color=0xff4654)
        await ctx.channel.send(embed=embed)
        return
    if role in member.roles:
        embed = discord.Embed(title="Mute failed", description="**{0}** is already muted!".format(member), color=0xff4654)
        await ctx.channel.send(embed=embed)
    if role not in guild.roles:
        perms = discord.Permissions(send_messages=False, speak=False)
        await guild.create_role(name="Muted", permissions=perms)
        await member.add_roles(role, reason=reason) #User muted
        embed = discord.Embed(title="User muted!", description="**{0}** was succesfully muted by **{1}** for **{2}**.".format(member, ctx.message.author, reason), color=0xff4654)
        await ctx.channel.send(embed=embed) 
        channel = discord.utils.get(guild.channels, name="logs")
        embed = discord.Embed(title=" ", description="**{0}** was muted by **{1}**. Reason: **{2}**".format(member, ctx.message.author, reason), color=0xff4654)
        embed.set_author(name="{0}".format(member), icon_url=pfp)
        await channel.send(embed=embed) #Log
        await asyncio.sleep(tempmute)
        await member.remove_roles(role)
    else:
        await member.add_roles(role, reason=reason) #User muted
        embed = discord.Embed(title="User muted!", description="**{0}** was succesfully muted by **{1}** for **{2}**.".format(member, ctx.message.author, reason), color=0xff4654)
        await ctx.channel.send(embed=embed) 
        channel = discord.utils.get(guild.text_channels, name="logs") 
        embed = discord.Embed(title=" ", description="<@{0}> was muted by <@{1}>. Reason: **{2}**".format(member.id, ctx.message.author.id, reason), color=0xff4654)
        embed.set_author(name="{0}".format(member), icon_url=pfp)
        await channel.send(embed=embed) #Log
        await asyncio.sleep(tempmute)
        await member.remove_roles(role)

我试着做时间:其他人建议的Optionalint,但它完全破坏了它。

EN

回答 1

Stack Overflow用户

发布于 2021-03-05 06:00:15

直截了当地回答,没有一个好的方法可以做到这一点。我建议您将该命令拆分为两个单独的命令:mutetempmute

如果你真的想把它作为一个函数,你可以这样做:

代码语言:javascript
复制
import re

async def mute(self, ctx, member: discord.Member, *options):
    if len(options) == 2: # User specified date and reason
        expiry, reason = options
    if len(options) == 1: # User specified one of the above so we need to find out which
        if re.match(r"\d+\D", options[0]):
           # Regex to check for one or more digit then a single non-digit
           expiry = options[0]
           reason = None
        else:
            expiry = None
            reason = options[0]

在一个稍微不相关的注释中,您不应该执行await asyncio.sleep(tempmute)。长时间的睡眠是不精确的,如果机器人重启,这个人将永远不会被静音。您应该使用datetime创建一个数据库,并使用一个循环任务来查询数据库,并在用户的时间到时取消用户的静音。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66476097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档