Spaces:
Paused
Paused
Download Discordbot/utils/transcript.py from QwertOs/DiscordBot: direct link, hf CLI and curl.
- Browser
- Download file 7.63 kB
-
https://huggingface.co/spaces/QwertOs/DiscordBot/resolve/main/Discordbot/utils/transcript.py
- Command line
-
hf download hf://spaces/QwertOs/DiscordBot/Discordbot/utils/transcript.py
-
curl -L -o transcript.py https://huggingface.co/spaces/QwertOs/DiscordBot/resolve/main/Discordbot/utils/transcript.py
7.63 kB
| import discord | |
| import os | |
| from datetime import datetime | |
| from typing import List | |
| import html | |
| class TranscriptGenerator: | |
| """工单转录生成器""" | |
| async def generate_transcript(channel: discord.TextChannel, messages_limit: int = 100) -> str: | |
| """ | |
| 生成频道的HTML转录文件 | |
| Args: | |
| channel: Discord 文本频道 | |
| messages_limit: 要获取的最大消息数 | |
| Returns: | |
| HTML格式的转录内容 | |
| """ | |
| # 获取频道历史记录 | |
| messages = [] | |
| async for message in channel.history(limit=messages_limit, oldest_first=True): | |
| messages.append(message) | |
| # 生成HTML | |
| html_content = TranscriptGenerator._create_html(messages, channel) | |
| return html_content | |
| def _create_html(messages: List[discord.Message], channel: discord.TextChannel) -> str: | |
| """创建HTML转录文件""" | |
| messages_html = "" | |
| for msg in messages: | |
| # 处理消息内容 | |
| content = html.escape(msg.content) if msg.content else "" | |
| # 处理附件 | |
| attachments_html = "" | |
| if msg.attachments: | |
| for attachment in msg.attachments: | |
| attachments_html += f'<div class="attachment"><a href="{attachment.url}" target="_blank">📎 {html.escape(attachment.filename)}</a></div>' | |
| # 处理嵌入 | |
| embeds_html = "" | |
| if msg.embeds: | |
| for embed in msg.embeds: | |
| embed_title = html.escape(embed.title) if embed.title else "" | |
| embed_desc = html.escape(embed.description) if embed.description else "" | |
| if embed_title or embed_desc: | |
| embeds_html += f''' | |
| <div class="embed"> | |
| {f'<div class="embed-title">{embed_title}</div>' if embed_title else ''} | |
| {f'<div class="embed-description">{embed_desc}</div>' if embed_desc else ''} | |
| </div> | |
| ''' | |
| # 构建消息HTML | |
| timestamp = msg.created_at.strftime("%Y-%m-%d %H:%M:%S") | |
| avatar_url = msg.author.display_avatar.url if msg.author.display_avatar else "https://cdn.discordapp.com/embed/avatars/0.png" | |
| messages_html += f''' | |
| <div class="message"> | |
| <img class="avatar" src="{avatar_url}" alt="avatar"> | |
| <div class="message-content"> | |
| <div class="message-header"> | |
| <span class="author">{html.escape(str(msg.author))}</span> | |
| <span class="timestamp">{timestamp}</span> | |
| {f'<span class="bot-tag">BOT</span>' if msg.author.bot else ''} | |
| </div> | |
| {f'<div class="content">{content}</div>' if content else ''} | |
| {attachments_html} | |
| {embeds_html} | |
| </div> | |
| </div> | |
| ''' | |
| # 完整的HTML模板 | |
| html_template = f'''<!DOCTYPE html> | |
| <html lang="zh-CN"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>工单转录 - {html.escape(channel.name)}</title> | |
| <style> | |
| * {{ | |
| margin: 0; | |
| padding: 0; | |
| box-sizing: border-box; | |
| }} | |
| body {{ | |
| font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | |
| background-color: #36393f; | |
| color: #dcddde; | |
| padding: 20px; | |
| }} | |
| .header {{ | |
| background-color: #2f3136; | |
| padding: 20px; | |
| border-radius: 8px; | |
| margin-bottom: 20px; | |
| text-align: center; | |
| }} | |
| .header h1 {{ | |
| color: #fff; | |
| margin-bottom: 10px; | |
| }} | |
| .header p {{ | |
| color: #b9bbbe; | |
| }} | |
| .message {{ | |
| display: flex; | |
| padding: 10px; | |
| margin-bottom: 5px; | |
| border-radius: 4px; | |
| }} | |
| .message:hover {{ | |
| background-color: #32353b; | |
| }} | |
| .avatar {{ | |
| width: 40px; | |
| height: 40px; | |
| border-radius: 50%; | |
| margin-right: 15px; | |
| }} | |
| .message-content {{ | |
| flex: 1; | |
| }} | |
| .message-header {{ | |
| display: flex; | |
| align-items: center; | |
| margin-bottom: 5px; | |
| }} | |
| .author {{ | |
| font-weight: 600; | |
| color: #fff; | |
| margin-right: 10px; | |
| }} | |
| .timestamp {{ | |
| color: #72767d; | |
| font-size: 0.8em; | |
| }} | |
| .bot-tag {{ | |
| background-color: #5865f2; | |
| color: white; | |
| padding: 2px 6px; | |
| border-radius: 4px; | |
| font-size: 0.7em; | |
| margin-left: 8px; | |
| font-weight: 600; | |
| }} | |
| .content {{ | |
| color: #dcddde; | |
| line-height: 1.5; | |
| word-wrap: break-word; | |
| }} | |
| .attachment {{ | |
| margin-top: 5px; | |
| }} | |
| .attachment a {{ | |
| color: #00b0f4; | |
| text-decoration: none; | |
| }} | |
| .attachment a:hover {{ | |
| text-decoration: underline; | |
| }} | |
| .embed {{ | |
| background-color: #2f3136; | |
| border-left: 4px solid #5865f2; | |
| padding: 10px; | |
| margin-top: 5px; | |
| border-radius: 4px; | |
| max-width: 500px; | |
| }} | |
| .embed-title {{ | |
| font-weight: 600; | |
| color: #fff; | |
| margin-bottom: 5px; | |
| }} | |
| .embed-description {{ | |
| color: #b9bbbe; | |
| font-size: 0.9em; | |
| }} | |
| .footer {{ | |
| text-align: center; | |
| margin-top: 30px; | |
| padding: 20px; | |
| color: #72767d; | |
| font-size: 0.9em; | |
| }} | |
| </style> | |
| </head> | |
| <body> | |
| <div class="header"> | |
| <h1>🎫 工单转录</h1> | |
| <p>频道: {html.escape(channel.name)}</p> | |
| <p>生成时间: {datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")} UTC</p> | |
| <p>消息总数: {len(messages)}</p> | |
| </div> | |
| <div class="messages"> | |
| {messages_html} | |
| </div> | |
| <div class="footer"> | |
| <p>此转录由 Discord Bot 自动生成</p> | |
| </div> | |
| </body> | |
| </html>''' | |
| return html_template | |
| async def save_transcript(html_content: str, filename: str) -> str: | |
| """ | |
| 保存转录文件 | |
| Args: | |
| html_content: HTML内容 | |
| filename: 文件名 | |
| Returns: | |
| 文件路径 | |
| """ | |
| # 创建 transcripts 目录 | |
| os.makedirs("transcripts", exist_ok=True) | |
| filepath = os.path.join("transcripts", filename) | |
| with open(filepath, 'w', encoding='utf-8') as f: | |
| f.write(html_content) | |
| return filepath | |