Last active
November 2, 2020 01:41
-
-
Save cgarz/74b2ca0728b9fcbea9785eac135cbcc9 to your computer and use it in GitHub Desktop.
A small script to parse messages from a specific time range from the better discord plugin MessgeLoggerv2's json file. Made specifically for WA dojo.
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
#!/usr/bin/env python3 | |
import json | |
import datetime | |
CHANNELS = { | |
506442111084920845: 'about', | |
733402345316286464: 'members', | |
497469150881513472: 'news', | |
497471609553158164: 'worms-faq', | |
762071368568143892: 'server-feedback', | |
416225356706480130: 'general', | |
733402229851291669: 'bug-reports', | |
691342271861358592: 'community-workshop', | |
450730221209911302: 'post-your-shot', | |
749923208211333230: 'roper-league', | |
750371635843956767: 'worms-1st-gen', | |
735205896346402888: 'worms-2', | |
750371889792286751: 'worms-asia', | |
508118991445688355: 'offtopic', | |
450733740314984451: 'memes', | |
769624251341733921: 'politics-and-debate', | |
541315292115107852: 'music-listening⚡' | |
} | |
CHANNEL_ID = 'offtopic' | |
CHANNEL_ID = [str(key) for key, value in CHANNELS.items() if value == CHANNEL_ID][0] | |
#BEFORE = datetime.datetime.fromisoformat('2020-10-03 21:53:49.269000+00:00') | |
#AFTER = datetime.datetime.fromisoformat('2020-10-03T21:07:10.844000+00:00') | |
AFTER = datetime.datetime.fromisoformat('2020-11-01T20:49:18.027000+00:00') | |
BEFORE = datetime.datetime.fromisoformat('2020-11-01T23:29:48.640000+00:00') | |
# Box drawing characters from https://www.compart.com/en/unicode/block/U+2500 | |
MSG_SEP = '━' * 40 | |
# /home/cgar/.config/BetterDiscord/plugins/MessageLoggerV2Data.config.json | |
with open('2020-11-02_MessageLoggerV2Data.config.json', 'rt') as f: | |
data = json.load(f) | |
formatted_messages = [] | |
for deleted_msg_id in data['data']['deletedMessageRecord'][CHANNEL_ID]: | |
message = data['data']['messageRecord'][deleted_msg_id] | |
timestamp = message['message']['timestamp'] | |
if timestamp.lower().endswith('z'): | |
timestamp = timestamp[:-1] + '+00:00' | |
timestamp = datetime.datetime.fromisoformat(timestamp) | |
if not BEFORE >= timestamp >= AFTER: | |
continue | |
username = message['message']['author']['username'] | |
content = message['message']['content'] | |
for mention in message['message']['mentions']: | |
content = content.replace(f'<@!{mention["id"]}>', f'<@{mention["username"]}>') | |
for attachment in message['message']['attachments']: | |
content += f'\n<ATTACHMENT:{attachment["url"]}>' | |
if '<#' in content: | |
for key, value in CHANNELS.items(): | |
content = content.replace(f'<#{key}>', f'<#{value}>') | |
formatted_messages.append({'timestamp': timestamp, 'username': username, 'content': content}) | |
formatted_messages.sort(key=lambda t: t['timestamp']) | |
for idx, msg in enumerate(formatted_messages, start=1): | |
print('[{t}] [{u}]:\n{c}'.format( | |
t = msg['timestamp'], | |
u = msg['username'], | |
c = msg['content'] | |
)) | |
if idx != len(formatted_messages): | |
print('\n', MSG_SEP, '\n', sep='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment