From 6aea13a56bb5f0978b941d2cc25dac901b53b3b9 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 5 May 2023 10:32:03 -0400 Subject: [PATCH] Add script --- README.md | 11 ++++++++++- output/.gitkeep | 0 scrape.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 output/.gitkeep create mode 100644 scrape.py diff --git a/README.md b/README.md index 66454d4..1bbec5e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ # Discord-scraper -Scrape a discord channel of its messages \ No newline at end of file +Scrape a discord channel of its messages + +## Usage + +``` +BOT_TOKEN='mydiscordbottoken' python3 ./scrape.py +``` + +Note that BOT_TOKEN is from the environment. The output of the above command +will be placed in the passes file. The output will be a json file diff --git a/output/.gitkeep b/output/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/scrape.py b/scrape.py new file mode 100644 index 0000000..c0e2638 --- /dev/null +++ b/scrape.py @@ -0,0 +1,48 @@ +import discord +from discord.ext import commands +import asyncio +import json +import sys + +BOT_TOKEN = os.environ['BOT_TOKEN'] +CHANNEL_ID = sys.argv[1] +OUTPUT_FILE = sys.argv[2] + +intents = discord.Intents.default() +intents.messages = True + +bot = commands.Bot(command_prefix='!', intents=intents) + +@bot.event +async def on_ready(): + print(f'{bot.user} has connected to Discord!') + channel = bot.get_channel(CHANNEL_ID) + if channel is None: + print(f'Channel with ID {CHANNEL_ID} not found. Make sure the bot has access to the channel.') + await bot.logout() + return + + print(f'Downloading chat history from #{channel.name}...') + messages = [] + count = 0 + async for message in channel.history(limit=None): + print("#{} {} - '{}'".format(count, message.author, message.content)) + messages.append({ + 'timestamp': message.created_at.isoformat(), + 'content': message.content, + 'author': str(message.author), + 'messageId': str(message.id) + }) + count = count + 1 + + print("Got {} messages.".format(count)) + + messages.reverse() + + with open(OUTPUT_FILE, 'w', encoding='utf-8') as f: + json.dump(messages, f, ensure_ascii=False, indent=2) + + print(f'Chat history saved to {OUTPUT_FILE}.') + exit() + +bot.run(BOT_TOKEN)