我正在尝试在discord.py中发出临时命令。 我已经完成了实际的命令,但是我希望能够使用1秒一秒钟,1m 1分钟,1h 1小时和1d 1天。
目前,我只能使用以秒为单位的time参数。如何使它像我之前提到的那样?
这是我的代码:
@client.command()
@commands.has_any_role(569518553112510475, 641312393447866398, 673530318166294550, 670694992314105927, 680137747822018560, 689135000414715980, 677844825004834827, 677848974224392192)
async def tempmute(ctx, member : discord.Member, time=0, reason=None):
if not member or time == 0 or time == str:
await ctx.channel.send(embed=commanderror)
return
elif reason == None:
reason = "No Reason Provided"
muteRole = discord.utils.get(ctx.guild.roles, id=663076470180151339)
await member.add_roles(muteRole)
tempMuteEmbed = discord.Embed(colour=embedcolour, description=f"**Reason:** {reason}")
tempMuteEmbed.set_author(name=f"{member} Has Been Muted", icon_url=f"{member.avatar_url}")
tempMuteEmbed.set_footer(text=embedfooter)
await ctx.channel.send(embed=tempMuteEmbed)
tempMuteModLogEmbed = discord.Embed(color=embedcolour)
tempMuteModLogEmbed.set_author(name=f"[MUTE] {member}", icon_url=f"{member.avatar_url}")
tempMuteModLogEmbed.add_field(name="User", value=f"{member.mention}")
tempMuteModLogEmbed.add_field(name="Moderator", value=f"{ctx.message.author}")
tempMuteModLogEmbed.add_field(name="Reason", value=f"{reason}")
tempMuteModLogEmbed.add_field(name="Duration", value=f"{str(time)}")
tempMuteModLogEmbed.set_footer(text=embedfooter)
modlog = client.get_channel(638783464438759464)
await modlog.send(embed=tempMuteModLogEmbed)
tempMuteDM = discord.Embed(color=embedcolour, title="Mute Notification", description="You Were Muted In **The Official Vanmanyo Discord Server**")
tempMuteDM.set_footer(text=embedfooter)
tempMuteDM.add_field(name="Reason", value=f"{reason}")
tempMuteDM.add_field(name="Duration", value=f"{time}")
userToDM = client.get_user(member.id)
await userToDM.send(embed=tempMuteDM)
await asyncio.sleep(time)
await member.remove_roles(muteRole)
unMuteModLogEmbed = discord.Embed(color=embedcolour)
unMuteModLogEmbed.set_author(name=f"[UNMUTE] {member}", icon_url=f"{member.avatar_url}")
unMuteModLogEmbed.add_field(name="User", value=f"{member.mention}")
unMuteModLogEmbed.set_footer(text=embedfooter)
modlog = client.get_channel(638783464438759464)
await modlog.send(embed=unMuteModLogEmbed)
答案 0 :(得分:1)
您可以创建一个单独的函数,将值转换为所需的秒数。
例如:
time_convert = {"s": 1, "m": 60, "h": 3600, "d": 86400}
def convert_time_to_seconds(time):
try:
return int(time[:-1]) * time_convert[time[-1]]
except:
return time
这将尝试转换时间。如果无法转换,它将仅返回原始值。因此,您可以使用“ 40s”或“ 40”。
答案 1 :(得分:0)
让机器人将“时间”之后的每个字符都视为“原因”
async def tempmute(ctx, member : discord.Member, time=0,*, reason=None):