我在堆栈溢出时使用了一个问题代码,但是其中使用的功能似乎已更改。
代码:
from discord.ext.commands import Bot
.
.
@client.command(pass_context = True)
async def clear(ctx, number):
mgs = [] #Empty list to put all the messages in the log
number = int(number) #Converting the amount of messages to delete to an integer
async for x in client(ctx.message.channel, limit = number):
mgs.append(x)
await client.delete_messages(mgs)
错误:
客户端中x的异步(ctx.message.channel,limit = number): TypeError:“ Bot”对象不可调用
和
discord.ext.commands.errors.CommandInvokeError:命令引发了异常:TypeError:'Bot'对象不可调用
答案 0 :(得分:0)
您似乎正在使用旧版本的discord.py
如果要清除最新版本的频道中的消息,这非常简单。您只需使用'discord.TextChannel.purge()'方法。
@client.command()
async def clear(ctx, amount=None):
if amount is None:
await ctx.channel.purge(limit=5)
elif amount == "all":
await ctx.channel.purge()
else:
await ctx.channel.purge(limit=int(amount))
您可能会注意到的一个区别是,您不再需要在'client.command()'装饰器中使用'pass_context'。
此外,在代码的顶部,您不应该直接导入Bot
,而应将语句中的内容替换为
from discord.ext import commands
并使用
实例化您的客户client = commands.Bot(command_prefix="!")
答案 1 :(得分:0)
在新版本的discord.py中,一种非常简单的方法可能看起来像这样:
@client.event
async def on_message(message):
if '-clear all' in message.content and message.author.permissions_in(message.channel).manage_messages:
deleted = await message.channel.purge(limit=10000, check=is_not_pinned)
await message.channel.send('All messages deleted.'.format(deleted))
await client.process_commands(message)
您也可以为此使用常规命令,这样就不需要await client.process_commands(message)
,但是您追求的功能仍然是await message.channel.purge(limit=amount, check=is_not_pinned)
,这样,只有具有“管理消息”权限的人才能使用此命令,它不会删除固定的消息。
您可以将金额设置为令人难以置信的高数字,这样它就可以删除几乎所有内容(如果不是全部)。我只尝试了大约300次,但效果很好(不过可能要花一些时间)。