49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
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)
|