我想知道如何让我的静音命令将某人静音一段时间。例如,p/mute @User 1h
会将某人静音 1 小时。
@client.command()
@commands.has_permissions(manage_roles = True)
async def mute(ctx, member : discord.Member = None):
if member == None:
await ctx.send("Please mention a user to mute")
else:
user = member
role = discord.utils.get(ctx.guild.roles, name="Muted")
if role is None:
await ctx.send("Please make a muted role name `Muted` or make sure to name the role `Muted` and not `muted` or `MUTED`")
else:
await user.add_roles(role)
await ctx.send(f"{member.name} has been muted by {ctx.author.name}")
答案 0 :(得分:0)
我有一个非常简单的方法来做到这一点。这将是高效的并且需要更少的代码。我已经在我的机器人中使用它很长时间了。我在这里回答了一位用户的另一个问题:Bot unmuting member when use tempmute command
在那里的答案中,我已经解释了如何编写完美的命令。
这个命令完全是另一个命令。您不必修改 mute
命令。但只需创建一个名为 tempmute
的新命令。
我将从那里复制并粘贴我的答案到这里。请注意每一个声明。 :)
我们将定义具有一些使用权限限制的函数。具有指定权限的人。
在使用此之前,您需要在代码中包含以下内容:
from discord.ext import commands
(如果有请忽略)
方法如下:
@bot.command()
@commands.has_permissions(manage_roles=True)
async def tempmute(ctx, member: discord.Member, time, *, reason=None):
如果需要,您可以添加更多权限,并使用 True 或 False 来允许或拒绝。
所以我正在为角色是否存在创建一个条件。它将检查角色是否存在。如果它存在,那么我们将简单地使用它,但如果它不存在,我们将创建一个具有特定权限的。
方法如下:
if discord.utils.get(ctx.guild.roles, name="Muted"):
mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
else:
perms = discord.Permissions(send_messages=False, add_reactions=False, connect=False, speak=False)
await bot.create_role(name="Muted", permissions=perms)
mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
现在我正在为成员是否静音创建一个条件。我们将通过成员的角色进行检查。
方法如下:
if mute_roles in member.roles:
await ctx.channel.send(f"{member.mention} is already muted!")
else:
# code here (more steps will explain how)
现在我们将为此命令添加限制权限。谁不能被所有人静音,谁可以被静音。
首先我们添加第一个条件,即管理员不能被静音。 方法如下:
if member.guild_permissions.administrator:
isadminembed=discord.Embed(title="Tempmute", description=f"Hi {ctx.author.mention}, you can't mute {member.mention} as they are a Server Administrator.", color=discord.Colour.red())
isadminembed.set_author(name="Bot")
await ctx.channel.send(embed=isadminembed)
现在我们将添加 else
条件,即除管理员之外的所有其他成员都可以静音。
方法如下:
else:
time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800, "M": 2419200, "y": 29030400}
mute_time = int(time[:-1]) * time_conversion[time[-1]]
await member.add_roles(mute_role)
mutedembed=discord.Embed(title="Tempmute", description=f"The member, {member.mention} has been muted by the moderator {ctx.author.mention}. \n \nTime: {mute_time} seconds \nReason: {reason}", color=discord.Colour.random())
mutedembed.set_author(name="Bot")
await ctx.channel.send(embed=mutedembed)
await asyncio.sleep(mute_time)
await member.remove_roles(mute_role)
await ctx.channel.send(f"{member.mention} has been unmuted!")
我在这里添加了时间转换。时间输入必须是:
1s
表示一秒,1m
表示一分钟,等等。您也可以将它用于 years
。
这是作为命令一起编译的所有代码。
@bot.command()
@commands.has_permissions(manage_roles=True)
async def tempmute(ctx, member: discord.Member, time, *, reason=None):
if discord.utils.get(ctx.guild.roles, name="Muted"):
mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
else:
perms = discord.Permissions(send_messages=False, add_reactions=False, connect=False, speak=False)
await bot.create_role(name="Muted", permissions=perms)
mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
if mute_roles in member.roles:
await ctx.channel.send(f"{member.mention} is already muted!")
else:
if member.guild_permissions.administrator:
isadminembed=discord.Embed(title="Tempmute", description=f"Hi {ctx.author.mention}, you can't mute {member.mention} as they are a Server Administrator.", color=discord.Colour.red())
isadminembed.set_author(name="Bot")
await ctx.channel.send(embed=isadminembed)
else:
time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800, "M": 2419200, "y": 29030400}
mute_time = int(time[:-1]) * time_conversion[time[-1]]
await member.add_roles(mute_role)
mutedembed=discord.Embed(title="Tempmute", description=f"The member, {member.mention} has been muted by the moderator {ctx.author.mention}. \n \nTime: {mute_time} seconds \nReason: {reason}", color=discord.Colour.random())
mutedembed.set_author(name="Bot")
await ctx.channel.send(embed=mutedembed)
await asyncio.sleep(mute_time)
await member.remove_roles(mute_role)
await ctx.channel.send(f"{member.mention} has been unmuted!")
我们将脚本中的函数定义为 tempmute 命令的错误并声明为错误。
方法如下:
@bot.error
async def tempmute_error(ctx, error):
我们现在将为错误添加条件。我们的错误将是:MissingRequiredArgument
错误,因为命令可能缺少必要的参数。
所以 isinstance
将检查错误,然后执行操作。
方法如下:
if isinstance(error, discord.ext.commands.MissingRequiredArgument):
tempmuteerrorembed=discord.Embed(title=f"Missing Argument! {error}", description=f"Hello {ctx.author.mention}! You have not entered the needed argument. \n Either you forgot to **mention the member** or you forgot to **enter the time of the mute** you want. \n \n Please check this again and add the necessary argument in the command. \n \nThis is the syntax: \n```!tempmute <mention member> <time: 1s, 2h, 4y etc..> <reason (optional)>```", color=discord.Colour.red())
tempmuteerrorembed.set_author(name="Bot")
await ctx.send(embed=tempmuteerrorembed)
这将适用于 MissingRequiredArguement
并显示错误及其语法,以便正确使用命令。
这里是TEMPMUTE命令的错误处理的编译代码。
@bot.error
async def tempmute_error(ctx, error):
if isinstance(error, discord.ext.commands.MissingRequiredArgument):
tempmuteerrorembed=discord.Embed(title=f"Missing Argument! {error}", description=f"Hello {ctx.author.mention}! You have not entered the needed argument. \n Either you forgot to **mention the member** or you forgot to **enter the time of the mute** you want. \n \n Please check this again and add the necessary argument in the command. \n \nThis is the syntax: \n```!tempmute <mention member> <time: 1s, 2h, 4y etc..> <reason (optional)>```", color=discord.Colour.red())
tempmuteerrorembed.set_author(name="Bot")
await ctx.send(embed=tempmuteerrorembed)
我希望我能够向您解释答案以及如何编写命令。请在评论中询问出现的任何问题。
谢谢! :D