Created
September 24, 2020 01:46
-
-
Save branw/8c48ad915b8c560715c767efc384fa30 to your computer and use it in GitHub Desktop.
Logs all text messages and metadata in a Discord channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
""" | |
Visit https://discord.com/, go to the Network tab of your Developer Tools, and | |
then open a channel. Find the latest request that looks like: | |
GET https://discord.com/api/v8/channels/12345678/messages?limit=50 | |
"12345678" is the CHANNEL_ID | |
Check the Response Headers section for the Authorization field that looks like: | |
Authorization: mfa.abcdefghij | |
"mfa.abcdefghij" is the AUTHORIZATION | |
""" | |
CHANNEL_ID = '123456' | |
AUTHORIZATION = 'abcdef' | |
def get_messages(before_id=None): | |
params = { | |
'limit': 100, | |
} | |
if before_id: | |
params['before'] = before_id | |
headers = { | |
'Authorization': AUTHORIZATION, | |
'Accept-Encoding': 'gzip, deflate', | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0', | |
} | |
r = requests.get(f'https://discord.com/api/v6/channels/{CHANNEL_ID}/messages', | |
params=params, headers=headers) | |
r.raise_for_status() | |
return r.json() | |
msgs = [] | |
more = True | |
last_id = None | |
while more: | |
m = get_messages(last_id) | |
msgs.extend(m) | |
more = len(m) == 100 | |
last_id = m[-1]['id'] | |
print(f'{len(msgs)} messages total, last message id={last_id}') | |
filename = f'msgs-{CHANNEL_ID}.json' | |
with open(filename, 'w') as f: | |
json.dump(msgs, f) | |
print(f'dumped to {filename}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment