Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import discord | |
| from discord.ext import commands | |
| import asyncio | |
| import os | |
| import re | |
| intents = discord.Intents.default() | |
| intents.members = True | |
| bot = commands.Bot(command_prefix='!', intents=intents) | |
| async def export_messages(ctx): | |
| await ctx.send('Exporting messages...') | |
| user = bot.get_user(USER_ID) | |
| messages = await user.history(limit=None).flatten() | |
| with open('messages.md', 'w', encoding='utf-8') as f: | |
| for message in messages: | |
| if message.author == user: | |
| f.write(f'{message.created_at} - {message.content}\n') | |
| await ctx.send('Messages exported!') | |
| urls = [] | |
| with open('urls.md', 'w', encoding='utf-8') as f: | |
| for message in messages: | |
| if message.author == user: | |
| if re.search("(?P<url>https?://[^\s]+)", message.content): | |
| urls.append(message.content) | |
| f.write(f'{message.created_at} - {message.content}\n') | |
| await ctx.send('URLs exported!') | |
| await ctx.send('Launching Gradio interface...') | |
| gr.Interface(fn=download_files, inputs=[gr.inputs.Textbox(label="Directory path")], | |
| outputs=["update", "update", "update", "update", "update", "update"]).launch() | |
| async def download_files(directory_path): | |
| messages_file_path = os.path.join(directory_path, 'messages.md') | |
| urls_file_path = os.path.join(directory_path, 'urls.md') | |
| total_messages = sum(1 for line in open(messages_file_path)) | |
| total_urls = sum(1 for line in open(urls_file_path)) | |
| messages_downloaded = 0 | |
| urls_downloaded = 0 | |
| with open(messages_file_path, 'r', encoding='utf-8') as f: | |
| for line in f: | |
| messages_downloaded += 1 | |
| await asyncio.sleep(0) # allow other tasks to run | |
| with open(urls_file_path, 'r', encoding='utf-8') as f: | |
| for line in f: | |
| urls_downloaded += 1 | |
| await asyncio.sleep(0) # allow other tasks to run | |
| messages_left = total_messages - messages_downloaded | |
| urls_left = total_urls - urls_downloaded | |
| time_left = (messages_left + urls_left) / 10 # assume 10 messages/urls downloaded per second | |
| update_messages = f"Messages Found: {total_messages}, Downloaded: {messages_downloaded}, Left: {messages_left}, ETA: {time_left:.0f} seconds" | |
| update_urls = f"URLs Found: {total_urls}, Downloaded: {urls_downloaded}, Left: {urls_left}, ETA: {time_left:.0f} seconds" | |
| update_progress = f"{messages_downloaded + urls_downloaded} / {total_messages + total_urls}" | |
| update_dir = f"Files will be saved in: {directory_path}" | |
| update_debug = f"Debug mode is {'on' if DEBUG_MODE else 'off'}" | |
| update_ip = f"Access the output at: http://localhost:8000/" | |
| return update_messages, update_urls, update_progress, update_dir, update_debug, update_ip | |
| DEBUG_MODE = False # set to True to enable debug mode | |
| if DEBUG_MODE: | |
| bot.run(TOKEN) | |
| else: | |
| import gradio as gr | |
| def export_messages(name, user_id, directory_path): | |
| # your script code here | |
| return "Messages have been exported." | |
| iface = gr.Interface(fn=export_messages, | |
| inputs=[gr.inputs.Textbox(label="Name"), | |
| gr.inputs.Textbox(label="User ID"), | |
| gr.inputs.Textbox(label="Directory path")], | |
| outputs="text", | |
| title='Discord Message Exporter') | |
| iface.launch() |