DiscordPy Tempmute有多个持续时间选项的命令



我想编码一个临时静音命令,但我不知道如何使它,我可以给出2个参数,而不仅仅是一个像下面的代码。我想做这样一个命令:"!Tempmute @user;而不仅仅是"!Tempmute @user "或"!Tempmute @user "请帮帮我,我花了一整天的时间在这件事上,我不明白…

@bot.command(aliases=['tempmute'])
@commands.has_permission(manage_messages=True)
async def mute(ctx, member: discord.Member=None, time=None, *, reason=None):
if not member:
await ctx.send("You must mention a member to mute!")
elif not time:
await ctx.send("You must mention a time!")
else:
if not reason:
reason="No reason given"
#Now timed mute manipulation
try:
seconds = time[:-1] #Gets the numbers from the time argument, start to -1
duration = time[-1] #Gets the timed maniulation, s, m, h, d
if duration == "s":
seconds = seconds * 1
elif duration == "m":
seconds = seconds * 60
elif duration == "h":
seconds = seconds * 60 * 60
elif duration == "d":
seconds = seconds * 86400
else:
await ctx.send("Invalid duration input")
return
except Exception as e:
print(e)
await ctx.send("Invalid time input")
return
guild = ctx.guild
Muted = discord.utils.get(guild.roles, name="Muted")
if not Muted:
Muted = await guild.create_role(name="Muted")
for channel in guild.channels:
await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True, read_messages=False)
await member.add_roles(Muted, reason=reason)
muted_embed = discord.Embed(title="Muted a user", description=f"{member.mention} Was muted by {ctx.author.mention} for {reason} to {time}")
await ctx.send(embed=muted_embed)
await asyncio.sleep(seconds)
await member.remove_roles(Muted)
unmute_embed = discord.Embed(title="Mute over!", description=f'{ctx.author.mention} muted to {member.mention} for {reason} is over after {time}")
await ctx.send(embed=unmute_embed)

由于您希望以1d3h10m1s格式输入,我们可以使用正则表达式分割来处理时间输入。我还没有检查你剩下的代码。但现在你已经计算出了秒数,所以试试吧。你走对了路。

import re
...
@bot.command(aliases=['tempmute'])
@commands.has_permissions(manage_messages=True) # not .has_permission
async def mute(ctx, member:discord.Member=None, userInput=None, *, reason="No reason given"):
inputSplit = re.split('(d+)', userInput)
#   inputSplit looks like ['', '1', 'd', '3', 'h', '10', 'm', '1', 's']
del inputSplit[0]
#   inputSplit now looks like ['1', 'd', '3', 'h', '10', 'm', '1', 's']
seconds = 0
#  Looping through inputSplit, from first to last letter
for i in range(1, len(inputSplit),2):
timeModifier = inputSplit[i]     # Modifier is the letter
timeValue = int(inputSplit[i-1]) # Value is number before modifier
# Same if loop as yours. Checking modifiers and adding the value
if timeModifier == "d":
seconds += 86400 * timeValue
elif timeModifier == "h":
seconds += 3600 * timeValue
elif timeModifier == "m":
seconds += 60 * timeValue
elif timeModifier == "s":
seconds += timeValue
# just for checking
print(f"Timeout time in seconds: {seconds}. Reason: {reason}")
...

From my test:

input:
>> mute @member 1d3h10m1s You know what you did
output:
>> Timeout time in seconds: 97801. Reason: You know what you did

最新更新