Discord.py机器人清除



好吧,伙计们,我需要一些帮助。基本上,我正在制作一个不和机器人。我在清除(清除(命令方面遇到了问题。到目前为止,这是我的代码:

@client.command(aliases= ['purge','delete'])
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount : int):
if amount == None:
await ctx.channel.purge(limit=1000000)
else:
await ctx.channel.purge(limit=amount)

我的问题是这个if amount == None。请帮帮我!我犯了一个错误,我错过了必要的论据。。。

这是因为您没有给amount默认值None。这就是您应该如何定义函数:

@client.command(aliases= ['purge','delete'])
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=None): # Set default value as None
if amount == None:
await ctx.channel.purge(limit=1000000)
else:
try:
int(amount)
except: # Error handler
await ctx.send('Please enter a valid integer as amount.')
else:
await ctx.channel.purge(limit=amount)

这个对我有效

@client.command()
@has_permissions(manage_messages = True)
async def clear(ctx , amount=5):
await ctx.channel.purge(limit=amount + 1)

我从youtuber频道得到的,你可以把你想清除的消息的数量放在clear命令后面,例如:-清除10

试试这个:

@client.command(aliases = ['purge', 'delete'])
@commands.has_permissions(manage_messages = True)
async def clear(ctx, amount: int = 1000000):
await ctx.channel.purge(limit = amount)

这应该有效,因为我有一个看起来几乎完全像这样的清晰命令。amount参数正在转换为int,如果客户端不提供参数,则数量将设置为1000000

这就是我使用的atm。


@bot.command(name='clear', help='this command will clear msgs')
async def clear(ctx, amount = 50): # Change amount
if ctx.message.author.id == (YOUR USER ID HERE):
await ctx.channel.purge(limit=amount)
else:
await ctx.channel.send(f"{ctx.author.mention} you are not allowed")

我刚刚添加了一个"if"语句,因为我是唯一可以使用该命令的人。您也可以允许特定的角色。

import discord
from discord.ext import commands
client = commands.Bot(command_prefix='>')
@client.event
async def on_ready():
print("Log : "+str(client.user))
@client.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int):
await ctx.channel.purge(limit=amount+1)
await ctx.message.delete()
client.run("token")

您可以这样做:

@client.command(aliases= ['purge','delete'])
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount :int = -1):
if amount == -1:
await ctx.channel.purge(limit=1000000)
else:
await ctx.channel.purge(limit=amount)

我认为它应该起作用,如果客户不给出参数,金额将设置为"-1〃;

最新更新