Add script

This commit is contained in:
Tyler Perkins 2023-05-05 10:32:03 -04:00
parent 978ab6d29c
commit 6aea13a56b
3 changed files with 58 additions and 1 deletions

View File

@ -1,3 +1,12 @@
# Discord-scraper
Scrape a discord channel of its messages
Scrape a discord channel of its messages
## Usage
```
BOT_TOKEN='mydiscordbottoken' python3 ./scrape.py <channel_id> <output_file>
```
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

0
output/.gitkeep Normal file
View File

48
scrape.py Normal file
View File

@ -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)