[英]How can I make my Discord Bot delete all messages in a channel?
I am trying to make my bot delete all messages at once when a user asks the bot to do so, but my code isn't working.当用户要求机器人这样做时,我试图让我的机器人一次删除所有消息,但我的代码不起作用。
import os
import discord
client = discord.Client()
client = commands.Bot(command_prefix='+')
@client.command(name='effacer')
async def purge(ctx):
async for msg in client.logs_from(ctx.message.channel):
await client.delete_messages(msg)
await ctx.send("Who am I? What is this place? And where the hell did the messages go?")
client.run(TOKEN)
How can I fix my code so that my bot can delete all messages?如何修复我的代码,以便我的机器人可以删除所有消息? I believe my biggest problem is
await client.delete_messages(msg)
, since Python continuously says that the client has no attribute to delete_messages
.我相信我最大的问题是
await client.delete_messages(msg)
,因为 Python 不断说客户端没有delete_messages
的属性。
By deleting every message would rate limit the bot, creating performance issues which would also slow down the bot.通过删除每条消息会限制机器人的速率,从而产生性能问题,这也会减慢机器人的速度。 Instead it would be more efficient if the bot just deleted the channel and made a clone of it in the exact same place.
相反,如果机器人只是删除通道并在完全相同的位置复制它,效率会更高。
Here's the purge included in your command这是您的命令中包含的清除
@client.command(name='effacer')
async def purge(ctx):
await ctx.channel.delete()
new_channel = await ctx.channel.clone(reason="Channel was purged")
await new_channel.edit(position=ctx.channel.position)
await new_channel.send("Channel was purged")
So the way you would do that is with purge, not delete messages.所以你这样做的方式是清除,而不是删除消息。 This will delete all the messages in the channel AND keep the channel id the same, meaning you'll only need the
manage_messages
permission to run this command.这将删除频道中的所有消息并保持频道 ID 相同,这意味着您只需要
manage_messages
权限即可运行此命令。 The way it works is it counts all the messages in the channel and then purges that number of messages它的工作方式是计算频道中的所有消息,然后清除该数量的消息
import os
import discord
client = discord.Client()
client = commands.Bot(command_prefix='+')
@client.command(name='effacer')
async def purge(ctx):
limit = 0
async for msg in ctx.channel.history(limit=None):
limit += 1
await ctx.channel.purge(limit=limit)
client.run(TOKEN)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.