Reputation: 53
So, I'm a bit new with the Python code so I'm a bit lost when trying to decypher the issue here.
I keep getting this error when trying to run this module:
Traceback (most recent call last):
File "C:\Users\test\OneDrive\Documents\mass.py", line 33, in <module>
delete_all(auth_token, channel_id, username1, username2, get_all_messages(auth_token, channel_id))
File "C:\Users\test\OneDrive\Documents\mass.py", line 29, in delete_all
if (message["author"]["username"] == user1):
KeyError: 'author'
Here's all of the code right here:
import json, requests, sys
print ("Delete all messages from specific channel")
username1 = "test"
username2 = "test#0101"
auth_token = "ZMNHFHFKJkjfja.FJDJfhsd.EJjfda"
channel_id = "35345345345451"
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.append(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))
Upvotes: 0
Views: 1521
Reputation: 757
Kelsey, in your code in the 4th last line in the if condition,
if (message["author"]["username"] == user1):
the compiler does not find any "author" in the json messages that you are iterating upon. Now since we do not know the structure of your json we cannot help you out more there, but this is for sure that the message json does not contain any key such namely author.
Upvotes: 2