我想检查用户是否给出了两个参数,第一个是要禁止(提及)的用户和原因。
如果未提及用户,则会显示一条消息,提示您“您必须提及您要禁止的用户”;如果未提供原因,则会禁止用户显示“未提供原因” “。
我也想做到这一点,以便即使discord.py尚未实现,也不能禁止自己
我尝试使用if args == None:
语句,但这不起作用。
@commands.has_permissions(ban_members=True)
@client.command()
async def ban(ctx, member: discord.Member, *, arg):
embed = discord.Embed(title="Punishments", color=0x3b55e8)
embed.add_field(name=f"{member} has been banned for:", value=arg)
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
await member.ban()
@ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
embed = discord.Embed(title="Error", color=0x3b55e8)
embed.add_field(name="You do not have permission to use this command", value="If you think this is incorrect, please contact a server administrator")
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
print(f"{ctx.author} has attempted to ban a player, but does not have the correct permissions")
我没有收到任何错误。
答案 0 :(得分:0)
您可以为自己的原因设置默认值,并在未传递必需参数时添加错误处理程序以处理失败:
@commands.has_permissions(ban_members=True)
@client.command()
async def ban(ctx, member: discord.Member, *, arg="No Reason Given"):
embed = discord.Embed(title="Punishments", color=0x3b55e8)
embed.add_field(name=f"{member} has been banned for:", value=arg)
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
await member.ban()
@ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
embed = discord.Embed(title="Error", color=0x3b55e8)
embed.add_field(name="You do not have permission to use this command", value="If you think this is incorrect, please contact a server administrator")
embed.set_footer(text=strftime("%Y-%m-%d %H:%M:%S", gmtime()))
await ctx.send(embed=embed)
print(f"{ctx.author} has attempted to ban a player, but does not have the correct permissions")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send("You must mention a user you'd like to ban")
else:
raise error
答案 1 :(得分:0)
Patrick Haugh已经回答了这个问题,但是最好创建一个error.py并在其中插入所有全局错误。 Link to the documentation (CommandErrors)
这看起来像这样:
import discord
from discord.ext import commands
class Error(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"You don't have the permission to execute this bot command!"))
if isinstance(error, commands.CommandNotFound):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"The bot command doesn't exist!"))
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"The command is incomplete, missing one or more parameters!"))
if isinstance(error, commands.BadArgument):
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description=f"The command was entered incorrectly, one or more parameters are wrong or in the wrong place!"))
def setup(bot):
bot.add_cog(Error(bot))