【问题标题】:Tempmute command with converting time in discord.py在 discord.py 中转换时间的 Tempmute 命令
【发布时间】:2021-02-09 05:23:48
【问题描述】:

我想在我的不和谐机器人中进行时间转换。现在,要使用 tempmute 命令,我只需要以秒为单位设置时间,并且我想进行转换,例如1s = 1; 1h = 3600 等

回答,我发现了什么,并不能解决我的问题。

这是我的代码:

# tempmute
@client.command()
@commands.has_permissions(kick_members=True)
async def tempmute(ctx, member: discord.Member, time=0, reason=None):
if not member or time == 0 or time == str:
    return
elif reason == None:
    reason = 'no reason provided'

tempmuteembed = discord.Embed(colour=discord.Colour.from_rgb(0, 255, 0))
tempmuteembed.set_author(icon_url=member.avatar_url, name=f'{member} has been tempmuted!')
tempmuteembed.set_footer(text=f"{ctx.guild.name}  •  {datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
tempmuteembed.add_field(name=f'ID:', value=f'{member.id}', inline=False)
tempmuteembed.add_field(name='Reason:', value=f"{reason}")
tempmuteembed.add_field(name='Duration:', value=f"{time}")
tempmuteembed.add_field(name=f'By:', value=f'{ctx.author.name}#{ctx.author.discriminator}', inline=False)

await ctx.channel.purge(limit=1)

guild = ctx.guild
for role in guild.roles:
    if role.name == 'Muted':
        await member.add_roles(role)
        await ctx.send(embed=tempmuteembed)
        await asyncio.sleep(time)
        await member.remove_roles(role)
        return

【问题讨论】:

  • 欢迎来到 SO。请澄清你的问题。您要转换为秒的具体是什么?

标签: python discord discord.py


【解决方案1】:

如果您的输入可能是 15s、20min、1h、2d,您可能的解决方案是:

import re

def tempmute(time=0):
    time_list = re.split('(\d+)',time)
    if time_list[2] == "s":
        time_in_s = int(time_list[1])
    if time_list[2] == "min":
        time_in_s = int(time_list[1]) * 60
    if time_list[2] == "h":
        time_in_s = int(time_list[1]) * 60 * 60
    if time_list[2] == "d":
        time_in_s = int(time_list[1]) * 60 * 60 * 60
    return time_in_s


print(tempmute("15h"))

>>> 54000

更新:所以完整的代码应该是这样的。您的 Input 将是一个字符串,如果它不是一个字符串,则不要返回 None !您输入的格式为 15 分钟、3 秒或 5 小时,否则超时为 0 秒。

# tempmute
@client.command()
@commands.has_permissions(kick_members=True)
async def tempmute(ctx, member: discord.Member, time=0, reason=None):
    if not member or time == 0:
        return
    elif reason == None:
        reason = 'no reason provided'
    try:
        if time_list[2] == "s":
            time_in_s = int(time_list[1])
        if time_list[2] == "min":
            time_in_s = int(time_list[1]) * 60
        if time_list[2] == "h":
            time_in_s = int(time_list[1]) * 60 * 60
        if time_list[2] == "d":
            time_in_s = int(time_list[1]) * 60 * 60 * 60
    except:
        time_in_s = 0
 
    tempmuteembed = discord.Embed(colour=discord.Colour.from_rgb(0, 255, 0))
    tempmuteembed.set_author(icon_url=member.avatar_url, name=f'{member} has been tempmuted!')
    tempmuteembed.set_footer(text=f"{ctx.guild.name}  •  {datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
    tempmuteembed.add_field(name=f'ID:', value=f'{member.id}', inline=False)
    tempmuteembed.add_field(name='Reason:', value=f"{reason}")
    tempmuteembed.add_field(name='Duration:', value=f"{time}")
    tempmuteembed.add_field(name=f'By:', value=f'{ctx.author.name}#{ctx.author.discriminator}', inline=False)
 
    await ctx.channel.purge(limit=1)
 
    guild = ctx.guild
    for role in guild.roles:
        if role.name == 'Muted':
            await member.add_roles(role)
            await ctx.send(embed=tempmuteembed)
            await asyncio.sleep(time_in_s)
            await member.remove_roles(role)
            return

【讨论】:

  • BadArgument('Converting to "{}" failed for parameter "{}".'.format(name, param.name)) from exc discord.ext.commands.errors.BadArgument: Converting to参数“time”的“int”失败。
猜你喜欢
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 2021-06-02
  • 2020-11-21
  • 2021-07-08
  • 2021-03-11
  • 2021-06-23
相关资源
最近更新 更多