在 discord.py 中转换时间的 Tempmute 命令

2022-01-15 00:00:00 python discord discord.py

问题描述

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

i want to make something like time convert in my discord bot. Now, to use tempmute command, i need to set time only in seconds, and i want to make converting e.g. 1s = 1; 1h = 3600 etc.

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

Answer, what i found, do not solving my problem.

这是我的代码:

# 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


解决方案

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

If your input may be 15s, 20min, 1h, 2d, your possible solution is:

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 秒.

Update: so the complete code should look something like this. Your Input will be a string, dont return None if its not a string! You input will be in the form of like 15min, 3s or 5h, otherwise the timeout will be 0 seconds.

# 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

相关文章