问题
I have this Python code for discord.py rewrite:
@bot.command(pass_context=True)
async def clean(ctx):
if ctx.author.guild_permissions.administrator:
llimit = ctx.message.content[10:].strip()
await ctx.channel.purge(limit=llimit)
await ctx.send('Cleared by <@{.author.id}>'.format(ctx))
await ctx.message.delete()
else:
await ctx.send("You cant do that!")
But every time i get this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: '<=' not supported between instances of 'str' and 'int'
Can someone here help me?
回答1:
You can treat single argument callables (like int
) like converters for the purpose of declaring arguments. I also changed your permissions check to be handled automatically by a commands.check
@bot.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def clean(ctx, limit: int):
await ctx.channel.purge(limit=limit)
await ctx.send('Cleared by {}'.format(ctx.author.mention))
await ctx.message.delete()
@clean.error
async def clear_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You cant do that!")
回答2:
The limit
argument of the purge
function takes an integer as a value
Try doing something like this
@bot.command(pass_context=True)
async def clean(ctx):
if ctx.author.guild_permissions.administrator:
llimit = ctx.message.content[10:].strip()
await ctx.channel.purge(limit=int(llimit))
await ctx.send('Cleared by <@{.author.id}>'.format(ctx))
await ctx.message.delete()
else:
await ctx.send("You cant do that!")
I'm not entirely sure what you're trying to accomplish with content[10:].strip()
but if you're trying to ignore the !clean
part of your command you are using too large of a number. content[7:]
would suffice
来源:https://stackoverflow.com/questions/52545638/i-have-a-error-in-discord-py-purge-command