Spaces:
Paused
Paused
File size: 7,630 Bytes
2fd3032 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | import discord
import os
from datetime import datetime
from typing import List
import html
class TranscriptGenerator:
"""工单转录生成器"""
@staticmethod
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
@staticmethod
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
@staticmethod
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
|