python Discord.py 删除文本频道中的所有消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43465082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python Discord.py delete all messages in a text channel
提问by Tyrell
so i am trying to make my discord bot delete all messages in the text channel because i like it when it's clean but i cant figure out how to do it this is what i have tried
所以我试图让我的不和谐机器人删除文本频道中的所有消息,因为我喜欢它干净的时候,但我不知道该怎么做,这是我尝试过的
@CLIENT.command()
async def Clear(message):
return await CLIENT.delete_message(message)
can't seem to figure it out can someone help thanks <3 i have tried other things and looked at other posts but i have only found out that the bot will delete the message everytime i type (not what im looking for)
似乎无法弄清楚有人可以帮忙吗谢谢 <3 我尝试了其他事情并查看了其他帖子,但我只发现机器人每次输入时都会删除消息(不是我要找的)
回答by Wright
If you wanted to bulk delete messages (that is, delete a number of messages at once, use await Client.delete_messages(list_of_messages)
. Here's an example
如果您想批量删除消息(即一次删除多条消息,请使用await Client.delete_messages(list_of_messages)
。这是一个示例
import asyncio
import discord
from discord.ext.commands import Bot
Client = 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.logs_from(ctx.message.channel, limit = number):
mgs.append(x)
await Client.delete_messages(mgs)
Client.run(Token)
NOTE:Doing this will only work for messages 14 days old and underand you can't delete above 100 messages at a time, meaning typing this !clear 120
would raise an error. However, its not impossible. You can add a while
loop in there if you really wanted to but that may produce unexpected results.
注意:这样做仅适用于 14 天及以下的消息,并且您不能一次删除 100 条以上的消息,这意味着输入此内容!clear 120
会引发错误。然而,这并非不可能。while
如果你真的想要,你可以在那里添加一个循环,但这可能会产生意想不到的结果。
Now, what if you have messages olderthan 14 days? You can't use Client.delete_messages(list_of_messages)
. Instead, you can use Client.delete_message(Message)
this would delete only one message at a time. Yes, I know slow but for now, that's all we've got. So, you can modify the original code to have it delete each time it loops in the logs_from()
.
现在,如果你有消息年长超过14天?你不能使用Client.delete_messages(list_of_messages)
. 相反,您可以使用Client.delete_message(Message)
它一次只删除一封邮件。是的,我知道很慢,但就目前而言,这就是我们所拥有的。因此,您可以修改原始代码以使其每次在logs_from()
.
Something like this:
像这样的东西:
import asyncio
import discord
from discord.ext.commands import Bot
Client = Bot('!')
@Client.command(pass_context = True)
async def clear(ctx, number):
number = int(number) #Converting the amount of messages to delete to an integer
counter = 0
async for x in Client.logs_from(ctx.message.channel, limit = number):
if counter < number:
await Client.delete_message(x)
counter += 1
await asyncio.sleep(1.2) #1.2 second timer so the deleting process can be even
Client.run(Token)
回答by Peter G
You can get a list of all of the messages in a channel using client.logs_from(someChannel)
. From there, just use client.delete_message(msg)
.
您可以使用 获取频道中所有消息的列表client.logs_from(someChannel)
。从那里,只需使用client.delete_message(msg)
.
Using the example listed in the readme of discord.py'srepo as a base, here's a method that should work for Python 3.5. Trigger this with "!clear":
使用discord.py 的repo自述文件中列出的示例作为基础,这里有一个适用于 Python 3.5 的方法。用 "!clear" 触发:
client = discord.Client()
@client.event
async def on_message(message):
if message.content.startswith('!clear'):
tmp = await client.send_message(message.channel, 'Clearing messages...')
async for msg in client.logs_from(message.channel):
await client.delete_message(msg)