import json, requests, sys
sys.setrecursionlimit(3000)
print ("Delete all messages from specific channel")
username1 = "123"
username2 = "123"
auth_token = "123"
channel_id = "123"
delete_from_all_users = "False"
def get_all_messages(auth, id, last="", prev=[]):
if not last:
messages = json.loads(requests.get("http://canary.discordapp.com/api/v6/channels/" + id + "/messages", headers={"authorization": auth}, params={"limit": 100}).content)
else:
messages = json.loads(requests.get("http://canary.discordapp.com/api/v6/channels/" + id + "/messages", headers={"authorization": auth}, params={"before" : last, "limit" : 100}).content)
prev = prev + messages
if len(messages) < 100:
print ("Got to end of channel at " + str(len(prev)) + " messages")
return prev
else:
oldest = sorted(messages, key=lambda x: x["timestamp"], reverse=True)[-1]
return get_all_messages(auth, id, last=oldest["id"], prev=prev)
def delete_all(auth, id, user1, user2, messages):
print ("Trying to delete all messages in " + id + " from username " + user1)
for message in messages:
# print(message["author"]["username"])
if (message["author"]["username"] == user1):
requests.delete("http://canary.discordapp.com/api/v6/channels/" + id + "/messages/" + message["id"],headers={"authorization": auth})
print ("All messages were deleted")
delete_all(auth_token, channel_id, username1, username2, get_all_messages(auth_token, channel_id))
这是我从互联网上某人那里获得的脚本,带有“ 123”的部分最初被替换为一些私人信息。我修复了一些问题以使此脚本正常工作,我解决了所有错误,但脚本本身总体上停止了工作。我通过添加“ sys.setrecursionlimit(3000)”修复了递归错误,并通过安装python 3.7 64bit(之前有32bit)修复了内存错误。现在,我运行了脚本,经过几个小时,它没有任何错误地返回并且没有执行任何操作。
答案 0 :(得分:1)
好吧……这似乎是对递归的一种非常非常差的使用。没有您要处理的数据的示例很难帮上忙,但是我将第一部分替换为以下内容:
def get_all_messages(auth, id, limit=100):
messages = json.loads(requests.get("http://canary.discordapp.com/api/v6/channels/" + str(id) + "/messages", headers={"authorization": auth}, params={"limit": limit}).content)
last = last_message_id(messages)
n = len(messages)
while n == limit:
new_messages = json.loads(requests.get("http://canary.discordapp.com/api/v6/channels/" + str(id) + "/messages", headers={"authorization": auth}, params={"before" : last, "limit" : limit}).content)
last = last_message_id(new_messages)
n = len(new_messages)
messages.extend(new_messages)
print("Retrieved {n} messages [Total: {tot}]".format(n=n, tot=len(messages)))
return messages
def last_message_id(mes):
return sorted(mes, key=lambda x: x["timestamp"], reverse=True)[-1]["id"]
同样,如果没有要处理的数据的示例,很难知道编写此数据的最佳方法是什么。