Python Tempmute命令,转换时间为discord.py
我想在我的discord机器人中转换一些类似时间的东西。现在,要使用tempmute命令,我只需要以秒为单位设置时间,并且我要进行转换,例如1s=1;1h=3600等 回答,我所发现的,并不能解决我的问题 这是我的密码:Python Tempmute命令,转换时间为discord.py,python,discord,discord.py,Python,Discord,Discord.py,我想在我的discord机器人中转换一些类似时间的东西。现在,要使用tempmute命令,我只需要以秒为单位设置时间,并且我要进行转换,例如1s=1;1h=3600等 回答,我所发现的,并不能解决我的问题 这是我的密码: # tempmute @client.command() @commands.has_permissions(kick_members=True) async def tempmute(ctx, member: discord.Member, time=0, reason=No
# 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
如果您的输入可能是15秒、20分钟、1小时、2天,您可能的解决方案是:
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
更新:所以完整的代码应该是这样的。您的输入将是一个字符串,如果不是字符串,请不要返回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
欢迎来到SO。请澄清你的问题。您试图转换为秒的具体内容是什么?BadArgument('转换为参数“{}.”失败。格式(名称,param.name))从exc discord.ext.commands.errors。BadArgument:转换为参数“time”失败。