简体   繁体   English

如何让我的 Discord Bot 删除频道中的所有消息?

[英]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.

相关问题 如何让我的 discord.py 机器人删除在特定频道上发送的每条消息? - How can I make my discord.py bot delete every message send on a specific channel? Discord Python-如何使BOT声明频道的所有消息? - Discord Python - How to make the BOT say all the messages of a channel? 如何删除我的 discord 机器人在特定频道中发送的先前消息? - How do i delete the previous messages that my discord bot sent in a specific channel? 如何使用 discord.py 制作一个检查某个服务器通道中所有消息的机器人 - How do I use discord.py to make a bot that checks through all messages in a certain server channel 如何让我的 discord 机器人获取所有服务器中的所有文本通道 ID? - How do I make my discord bot get all text channel id's in all servers? 如何让我的 discord.py 机器人向我选择的频道发送我选择的消息? - how can i make my discord.py bot send a message i choose to a channel i choose? 如何让我的 discord.py 机器人对频道中发送的所有图像做出反应 - How do i make my discord.py bot react to all images sent in a channel 如何让我的 discord 机器人加入*类别*中的语音频道? - How to I make a my discord bot join a voice channel *in a category*? Discord.py 如何让机器人在特定时间后删除消息 - Discord.py How can I make a bot delete messages after a specific amount of time 如何让我的 Discord 机器人提及频道中的特定人员? - How can i make my Discord bot mention a specific person in the channel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM