python - if not arg 的 discord.py 问题(清除命令)
问题描述
我有一个问题if not arg
(我直接跳过它),我知道放入arg:
int 会给我带来问题。你知道解决办法吗?我尝试了很多方法,但我不能
代码:
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def clear(ctx, arg: int):
if not arg:
embed = discord.Embed(
color=discord.Colour.red()
)
embed.set_author(
name="Specifica quante messaggi vuoi cancellare!",
icon_url="https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png"
)
await ctx.send(embed=embed, delete_after=10.0)
return
embed = discord.Embed(
color=discord.Colour.green()
)
embed.set_author(
name=f'Ho cancellato ufficialmente {arg} messaggi!',
icon_url=f'{ctx.author.avatar_url}'
)
await ctx.channel.purge(limit=arg+1)
await ctx.send(embed=embed, delete_after=10.0)
embed = discord.Embed(
color=discord.Colour.dark_gold()
)
embed.set_author(
name=f'{ctx.author._user} ha cancellato {arg}',
icon_url=f'{ctx.author.avatar_url}'
)
embed.add_field(
name='Messaggi cancellati da:',
value=f'{ctx.author._user}',
inline=True
)
embed.add_field(
name='Quantità:',
value=f'{arg}',
inline=True
)
channel = client.get_channel(729553772547932190)
await channel.send(embed=embed)
@clear.error
async def clear_error(ctx, error):
if isinstance(error, commands.CheckFailure):
embed = discord.Embed(
color=discord.Colour.red()
)
embed.set_author(
name="Non ti è permesso cancellare i messaggi!",
icon_url='https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png'
)
await ctx.send(embed=embed, delete_after=10.0)
解决方案
如果您在调用命令时未指定必需的参数,则会导致MissingRequiredArgument
错误并且不会执行您的函数。您只需在clear_error
函数中添加此错误,如下所示:
@clear.error
async def clear_error(ctx, error):
embed = discord.Embed(color=0xe74c3c)
if isinstance(error, commands.CheckFailure):
embed.set_author(
name="You're not authorized to delete messages!",
icon_url='your image'
)
elif isinstance(error, commands.MissingRequiredArgument):
embed.set_author(
name="You need to specify how many messages you want to delete!",
icon_url='your image'
)
await ctx.send(embed=embed, delete_after=10.0)
此外,您将不再需要您if not arg
的
推荐阅读
- oracle - Oracle Apex 从交互式报表中删除行
- python - 如何在 pyqt5 中为 QStackWidget 添加滑动动画?
- python-3.x - 无法在 Alpine Linux / Docker 中访问 argon2
- mathcad - 如何使用mathcad中的“求解”将方程转换为变量?
- node.js - npm 错误找不到模块'./config/core.js'
- r - 基于未使用 p 值、误差线和报告百分比绘制的变量的箱线图
- openssl - 使用密码短语为 Snowflake 生成密钥对身份验证
- r - 数据整理以添加汇总映射值 R 计数的列
- c++ - 有没有办法使用 LLVM 和 clang++ 生成带有 x86 基本块的控制流图?
- python - 在 YARN 上打包 PySpark 的依赖项时找不到 Conda 环境