Skip to content

Instantly share code, notes, and snippets.

@cbonesana
Created August 11, 2019 16:51
Show Gist options
  • Save cbonesana/80fe18bd78e73ae70d88284e8f287e5c to your computer and use it in GitHub Desktop.
Save cbonesana/80fe18bd78e73ae70d88284e8f287e5c to your computer and use it in GitHub Desktop.
Download all the messages, known as history or logs in previous version, from a Discord server using access via token using python 3.7.
import argparse
import asyncio
import json
import os
import discord
client = discord.Client()
dirHistory = 'history'
dirClean = 'clean'
@client.event
@asyncio.coroutine
def on_ready():
print(f'Logged in as: {client.user.name}')
print('------')
# create directories
if not os.path.exists(dirHistory):
os.mkdir(dirHistory)
if not os.path.exists(dirClean):
os.mkdir(dirClean)
if len(CHANNELS) == 0:
channels = client.get_all_channels()
else:
channels = [client.get_channel(c) for c in CHANNELS]
for channel in channels:
try:
print(channel)
yield from scrape_history(channel)
except Exception as e:
print(e)
try:
yield from client.close()
except Exception as e:
print(e)
@asyncio.coroutine
async def scrape_history(channel):
print(f'scraping logs for {channel.name}')
messages = []
clean = []
history = await channel.history(limit=None, oldest_first=True).flatten()
for message in history:
messages.append({
'id': message.id,
'timestamp': str(message.created_at),
'edited_timestamp': str(message.edited_at),
'author_id': message.author.id,
'author_name': message.author.name,
'content': message.content
})
clean.append(f'{message.created_at}\t{message.author.name}\t{message.content}')
with open(os.path.join(dirHistory, f'channel-{channel.id}_{channel.name}-message-history.txt'), mode='w') as f:
json.dump(messages, f)
with open(os.path.join(dirClean, f'channel-{channel.id}_{channel.name}-message-clean.txt'), mode='w') as f:
json.dump(clean, f)
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--server', type=int, required=True, default='', help='ID of the server')
parser.add_argument('-t', '--token', type=str, required=True, default='', help='Access token to use')
parser.add_argument('-c', '--channels', type=list, nargs='+', default=[],
help='List of channels to download. If missing, all channels with read access will be downloaded.')
args = parser.parse_args()
SERVER_ID = args.server
TOKEN = args.token
CHANNELS = args.channels
client.run(TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment