-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
616 lines (579 loc) · 25.6 KB
/
Copy pathbot.py
File metadata and controls
616 lines (579 loc) · 25.6 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import os
import json
import asyncio
import sqlite3
from pathlib import Path
from datetime import datetime
import discord
from discord.ext import commands
def load_env_from_file(path=".env"):
p = Path(path)
if not p.exists():
return
with open(p, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
value = value.strip().strip('"').strip("'")
if key and key not in os.environ:
os.environ[key] = value
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
def generate_progress_bar(current, total, length=20):
if total <= 0:
return "[--------------------] 0/0 (0%)"
if current > total:
current = total
progress = int(length * current / total)
bar = "█" * progress + "-" * (length - progress)
percent = int(100 * current / total)
return f"[{bar}] {current}/{total} ({percent}%)"
def sanitize_name(name):
return "".join(c for c in name if c.isalnum() or c in (" ", "-", "_")).strip() or "unknown"
def default_backup_options(method, mode):
normalized_method = (method or "json").lower()
normalized_mode = (mode or "rapido").lower()
if normalized_method not in ("json", "txt", "db"):
normalized_method = "json"
if normalized_mode not in ("rapido", "full"):
normalized_mode = "rapido"
return normalized_method, normalized_mode
async def backup_guild_structure(guild, backup_dir):
info = {
"guild_id": guild.id,
"guild_name": guild.name,
"owner_id": guild.owner_id,
"member_count": guild.member_count,
"created_at": guild.created_at.isoformat(),
}
roles = []
for role in guild.roles:
roles.append(
{
"id": role.id,
"name": role.name,
"position": role.position,
"permissions": role.permissions.value,
"color": role.color.value,
"hoist": role.hoist,
"mentionable": role.mentionable,
"managed": role.managed,
}
)
categories = []
for category in guild.categories:
overwrites = {}
for target, overwrite in category.overwrites.items():
overwrites[str(target.id)] = {
"allow": overwrite.pair()[0].value,
"deny": overwrite.pair()[1].value,
}
categories.append(
{
"id": category.id,
"name": category.name,
"position": category.position,
"overwrites": overwrites,
}
)
channels = []
for channel in guild.text_channels:
overwrites = {}
for target, overwrite in channel.overwrites.items():
overwrites[str(target.id)] = {
"allow": overwrite.pair()[0].value,
"deny": overwrite.pair()[1].value,
}
channels.append(
{
"id": channel.id,
"name": channel.name,
"category_id": channel.category.id if channel.category else None,
"position": channel.position,
"topic": channel.topic,
"nsfw": channel.is_nsfw(),
"slowmode_delay": channel.slowmode_delay,
"overwrites": overwrites,
}
)
emojis = []
for emoji in guild.emojis:
emojis.append(
{
"id": emoji.id,
"name": emoji.name,
"animated": emoji.animated,
"url": str(emoji.url),
}
)
structure = {
"info": info,
"roles": roles,
"categories": categories,
"channels": channels,
"emojis": emojis,
"version": 1,
}
structure_path = backup_dir / "backup_structure.json"
with open(structure_path, "w", encoding="utf-8") as f:
json.dump(structure, f, ensure_ascii=False, indent=2)
return structure_path
async def backup_messages_txt(guild, backup_dir, mode, progress_msg):
text_channels = guild.text_channels
total_channels = len(text_channels)
completed_channels = 0
media_dir = backup_dir / "media"
logs_dir = backup_dir / "logs"
media_dir.mkdir(exist_ok=True)
logs_dir.mkdir(exist_ok=True)
async def progress_updater():
while completed_channels < total_channels:
bar = generate_progress_bar(completed_channels, total_channels)
await progress_msg.edit(content=f"📨 Backup messaggi TXT: {bar}")
await asyncio.sleep(2)
updater_task = asyncio.create_task(progress_updater())
try:
for channel in text_channels:
safe_name = sanitize_name(channel.name) or str(channel.id)
log_path = logs_dir / f"{safe_name}.txt"
channel_media_dir = media_dir / safe_name
channel_media_dir.mkdir(exist_ok=True)
try:
limit = None
if mode == "rapido":
limit = 500
history_kwargs = {"limit": limit, "oldest_first": True}
with open(log_path, "w", encoding="utf-8") as f:
async for msg in channel.history(**history_kwargs):
timestamp = msg.created_at.isoformat()
author = f"{msg.author} ({msg.author.id})"
content = msg.clean_content or ""
f.write(f"[{timestamp}] {author}: {content}\n")
if msg.embeds:
for idx, embed in enumerate(msg.embeds, start=1):
embed_dict = embed.to_dict()
f.write(f"[EMBED {idx}] {json.dumps(embed_dict, ensure_ascii=False)}\n")
if mode == "full":
for att in msg.attachments:
try:
target = channel_media_dir / att.filename
await att.save(target)
f.write(f"[ATTACHMENT] {att.filename} -> {target.as_posix()}\n")
except Exception as e:
f.write(f"[ATTACHMENT ERROR] {att.filename} - {e}\n")
except Exception as e:
error_log = logs_dir / "errors.txt"
with open(error_log, "a", encoding="utf-8") as ef:
ef.write(f"Errore salvataggio canale {channel.id} ({channel.name}): {e}\n")
completed_channels += 1
finally:
updater_task.cancel()
async def backup_messages_json(guild, backup_dir, mode, progress_msg):
text_channels = guild.text_channels
total_channels = len(text_channels)
completed_channels = 0
media_dir = backup_dir / "media"
media_dir.mkdir(exist_ok=True)
data = {
"guild_id": guild.id,
"created_at": datetime.utcnow().isoformat(),
"mode": mode,
"channels": [],
"version": 1,
}
async def progress_updater():
while completed_channels < total_channels:
bar = generate_progress_bar(completed_channels, total_channels)
await progress_msg.edit(content=f"📨 Backup messaggi JSON: {bar}")
await asyncio.sleep(2)
updater_task = asyncio.create_task(progress_updater())
try:
for channel in text_channels:
safe_name = sanitize_name(channel.name) or str(channel.id)
channel_media_dir = media_dir / safe_name
channel_media_dir.mkdir(exist_ok=True)
channel_entry = {
"id": channel.id,
"name": channel.name,
"category_id": channel.category.id if channel.category else None,
"messages": [],
}
try:
limit = None
if mode == "rapido":
limit = 500
history_kwargs = {"limit": limit, "oldest_first": True}
async for msg in channel.history(**history_kwargs):
msg_payload = {
"id": msg.id,
"author_id": msg.author.id,
"author_tag": str(msg.author),
"content": msg.clean_content or "",
"created_at": msg.created_at.isoformat(),
"embeds": [embed.to_dict() for embed in msg.embeds],
"attachments": [],
}
if mode == "full":
for att in msg.attachments:
attachment_path = channel_media_dir / att.filename
try:
await att.save(attachment_path)
msg_payload["attachments"].append(
{
"filename": att.filename,
"saved_path": str(attachment_path.relative_to(backup_dir)),
}
)
except Exception as e:
msg_payload["attachments"].append(
{
"filename": att.filename,
"error": str(e),
}
)
channel_entry["messages"].append(msg_payload)
except Exception as e:
channel_entry["error"] = str(e)
data["channels"].append(channel_entry)
completed_channels += 1
finally:
updater_task.cancel()
data_path = backup_dir / "backup_data.json"
with open(data_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
return data_path
async def backup_messages_db(guild, backup_dir, mode, progress_msg):
db_path = backup_dir / "backup.db"
conn = sqlite3.connect(db_path)
conn.execute(
"CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, channel_id INTEGER, author_id INTEGER, author_tag TEXT, content TEXT, created_at TEXT)"
)
conn.execute(
"CREATE TABLE IF NOT EXISTS embeds (id INTEGER PRIMARY KEY AUTOINCREMENT, message_id INTEGER, payload TEXT)"
)
conn.execute(
"CREATE TABLE IF NOT EXISTS attachments (id INTEGER PRIMARY KEY AUTOINCREMENT, message_id INTEGER, filename TEXT, saved_path TEXT, error TEXT)"
)
conn.commit()
text_channels = guild.text_channels
total_channels = len(text_channels)
completed_channels = 0
media_dir = backup_dir / "media"
media_dir.mkdir(exist_ok=True)
async def progress_updater():
while completed_channels < total_channels:
bar = generate_progress_bar(completed_channels, total_channels)
await progress_msg.edit(content=f"📨 Backup messaggi DB: {bar}")
await asyncio.sleep(2)
updater_task = asyncio.create_task(progress_updater())
try:
for channel in text_channels:
safe_name = sanitize_name(channel.name) or str(channel.id)
channel_media_dir = media_dir / safe_name
channel_media_dir.mkdir(exist_ok=True)
try:
limit = None
if mode == "rapido":
limit = 500
history_kwargs = {"limit": limit, "oldest_first": True}
async for msg in channel.history(**history_kwargs):
conn.execute(
"INSERT OR REPLACE INTO messages (id, channel_id, author_id, author_tag, content, created_at) VALUES (?, ?, ?, ?, ?, ?)",
(
msg.id,
channel.id,
msg.author.id,
str(msg.author),
msg.clean_content or "",
msg.created_at.isoformat(),
),
)
for embed in msg.embeds:
conn.execute(
"INSERT INTO embeds (message_id, payload) VALUES (?, ?)",
(msg.id, json.dumps(embed.to_dict(), ensure_ascii=False)),
)
if mode == "full":
for att in msg.attachments:
attachment_path = channel_media_dir / att.filename
error = None
try:
await att.save(attachment_path)
except Exception as e:
error = str(e)
conn.execute(
"INSERT INTO attachments (message_id, filename, saved_path, error) VALUES (?, ?, ?, ?)",
(
msg.id,
att.filename,
str(attachment_path.relative_to(backup_dir)),
error,
),
)
conn.commit()
except Exception as e:
conn.execute(
"INSERT INTO messages (id, channel_id, author_id, author_tag, content, created_at) VALUES (?, ?, ?, ?, ?, ?)",
(
None,
channel.id,
0,
"system",
f"Errore durante il backup del canale {channel.id} ({channel.name}): {e}",
datetime.utcnow().isoformat(),
),
)
conn.commit()
completed_channels += 1
finally:
updater_task.cancel()
conn.close()
return db_path
async def create_metadata_file(backup_dir, guild, method, mode, structure_path, data_path):
timestamp = datetime.utcnow().isoformat()
meta = {
"guild_id": guild.id,
"guild_name": guild.name,
"created_at": timestamp,
"method": method,
"mode": mode,
"structure_file": str(structure_path.name),
"data_file": str(data_path.name) if data_path else None,
"version": 1,
}
safe_name = sanitize_name(guild.name) or str(guild.id)
file_name = f"backup_{safe_name}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.json"
meta_path = backup_dir / file_name
with open(meta_path, "w", encoding="utf-8") as f:
json.dump(meta, f, ensure_ascii=False, indent=2)
return meta_path
@bot.command()
@commands.has_permissions(administrator=True)
async def backup(ctx, metodo: str = None, tipo: str = None):
guild = ctx.guild
method, mode = default_backup_options(metodo, tipo)
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
safe_name = sanitize_name(guild.name) or str(guild.id)
backup_dir = Path(f"backup_{safe_name}_{timestamp}")
backup_dir.mkdir(exist_ok=True)
description = f"Metodo: {method.upper()} • Modalità: {mode.upper()}"
progress_msg = await ctx.send(f"📦 Avvio backup di **{guild.name}**\n{description}")
structure_path = await backup_guild_structure(guild, backup_dir)
await progress_msg.edit(content=f"📁 Struttura server salvata\n{description}")
data_path = None
if method == "txt":
await backup_messages_txt(guild, backup_dir, mode, progress_msg)
elif method == "json":
data_path = await backup_messages_json(guild, backup_dir, mode, progress_msg)
elif method == "db":
data_path = await backup_messages_db(guild, backup_dir, mode, progress_msg)
meta_path = await create_metadata_file(backup_dir, guild, method, mode, structure_path, data_path)
from discord import Embed
embed = Embed(
title="✅ Backup completato",
description="Il backup del server è stato completato con successo.",
color=discord.Color.green(),
)
embed.add_field(name="Metodo", value=method.upper(), inline=True)
embed.add_field(name="Modalità", value=mode.upper(), inline=True)
embed.add_field(name="File metadati", value=str(meta_path.name), inline=False)
embed.set_footer(text="Usa !restorebackup <nomefile> per ripristinare da questo backup")
await progress_msg.edit(content=None, embed=embed)
@bot.command()
@commands.has_permissions(administrator=True)
async def restorebackup(ctx, nomefile: str):
guild = ctx.guild
if not ctx.author.guild_permissions.administrator:
await ctx.send("❌ Devi essere amministratore per usare questo comando.")
return
if not guild.me.guild_permissions.administrator:
await ctx.send("❌ Il bot deve avere permessi di amministratore per ripristinare un backup.")
return
base = Path(nomefile)
if not base.is_absolute():
base = Path.cwd() / base
if base.is_dir():
await ctx.send("❌ Specifica il file dei metadati JSON, non una cartella.")
return
if base.suffix.lower() != ".json":
base = base.with_suffix(".json")
if not base.exists():
await ctx.send(f"❌ File di backup non trovato: {base.name}")
return
try:
with open(base, "r", encoding="utf-8") as f:
meta = json.load(f)
except Exception:
await ctx.send("❌ Impossibile leggere il file di metadati JSON.")
return
if str(meta.get("guild_id")) != str(guild.id):
await ctx.send("❌ Questo backup appartiene a un altro server.")
return
method = meta.get("method", "json").lower()
data_file_name = meta.get("data_file")
structure_file_name = meta.get("structure_file")
if not structure_file_name:
await ctx.send("❌ File di struttura mancante nei metadati, impossibile procedere.")
return
backup_dir = base.parent
structure_path = backup_dir / structure_file_name
if not structure_path.exists():
await ctx.send("❌ File di struttura non trovato accanto ai metadati.")
return
try:
with open(structure_path, "r", encoding="utf-8") as f:
structure = json.load(f)
except Exception:
await ctx.send("❌ Impossibile leggere la struttura del backup.")
return
warning = (
f"⚠️ Stai per ripristinare il backup di **{meta.get('guild_name','sconosciuto')}** "
f"creato il {meta.get('created_at','sconosciuto')}.\n"
"Tutti i canali e molti elementi del server verranno eliminati e ricreati.\n"
"Gli autori originali e i timestamp dei messaggi non possono essere ripristinati.\n\n"
"Scrivi `CONFERMO` entro 60 secondi per continuare."
)
await ctx.send(warning)
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
reply = await bot.wait_for("message", timeout=60.0, check=check)
except asyncio.TimeoutError:
await ctx.send("⏱️ Tempo scaduto, ripristino annullato.")
return
if reply.content.strip().upper() != "CONFERMO":
await ctx.send("❌ Ripristino annullato.")
return
progress_msg = await ctx.send("🧹 Pulizia server in corso...")
for channel in list(guild.channels):
try:
await channel.delete(reason="Restore backup")
except Exception:
continue
for role in sorted(guild.roles, key=lambda r: r.position, reverse=True):
if role.is_default():
continue
try:
await role.delete(reason="Restore backup")
except Exception:
continue
await progress_msg.edit(content="📁 Ricostruzione struttura server...")
role_map = {}
for role_data in sorted(structure.get("roles", []), key=lambda r: r.get("position", 0)):
if role_data.get("name") == "@everyone":
role_map[role_data["id"]] = guild.default_role
continue
try:
new_role = await guild.create_role(
name=role_data.get("name") or "role",
permissions=discord.Permissions(role_data.get("permissions", 0)),
colour=discord.Colour(role_data.get("color", 0)),
hoist=role_data.get("hoist", False),
mentionable=role_data.get("mentionable", False),
reason="Restore backup",
)
role_map[role_data["id"]] = new_role
except Exception:
continue
category_map = {}
for cat_data in sorted(structure.get("categories", []), key=lambda c: c.get("position", 0)):
overwrites = {}
for target_id, ov in cat_data.get("overwrites", {}).items():
role = role_map.get(int(target_id))
if not role:
continue
allow = discord.Permissions(ov.get("allow", 0))
deny = discord.Permissions(ov.get("deny", 0))
overwrites[role] = discord.PermissionOverwrite.from_pair(allow, deny)
try:
new_cat = await guild.create_category(
name=cat_data.get("name") or "categoria",
overwrites=overwrites,
reason="Restore backup",
)
category_map[cat_data["id"]] = new_cat
except Exception:
continue
channel_map = {}
for ch_data in sorted(structure.get("channels", []), key=lambda c: c.get("position", 0)):
category = None
if ch_data.get("category_id") and ch_data["category_id"] in category_map:
category = category_map[ch_data["category_id"]]
overwrites = {}
for target_id, ov in ch_data.get("overwrites", {}).items():
role = role_map.get(int(target_id))
if not role:
continue
allow = discord.Permissions(ov.get("allow", 0))
deny = discord.Permissions(ov.get("deny", 0))
overwrites[role] = discord.PermissionOverwrite.from_pair(allow, deny)
try:
new_channel = await guild.create_text_channel(
name=ch_data.get("name") or "canale",
category=category,
topic=ch_data.get("topic"),
nsfw=ch_data.get("nsfw", False),
slowmode_delay=ch_data.get("slowmode_delay", 0),
overwrites=overwrites,
reason="Restore backup",
)
channel_map[ch_data["id"]] = new_channel
except Exception:
continue
await progress_msg.edit(content="💬 Ripristino messaggi dai dati backup...")
if method == "json" and data_file_name:
data_path = backup_dir / data_file_name
if data_path.exists():
try:
with open(data_path, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception:
await progress_msg.edit(content="⚠️ Struttura ripristinata, ma lettura messaggi JSON fallita.")
return
media_root = backup_dir / "media"
for ch in data.get("channels", []):
target_channel = channel_map.get(ch.get("id"))
if not target_channel:
continue
for msg in ch.get("messages", []):
content_lines = []
header = f"[{msg.get('created_at','sconosciuto')}] {msg.get('author_tag','utente sconosciuto')}"
content_lines.append(header)
main_content = msg.get("content") or ""
if main_content:
content_lines.append(main_content)
content = "\n".join(content_lines)
embeds = []
for e_dict in msg.get("embeds", []):
try:
embeds.append(discord.Embed.from_dict(e_dict))
except Exception:
continue
files = []
for att in msg.get("attachments", []):
saved_path = att.get("saved_path")
if not saved_path:
continue
file_path = backup_dir / saved_path
if not file_path.exists():
continue
try:
files.append(discord.File(fp=str(file_path), filename=att.get("filename") or file_path.name))
except Exception:
continue
try:
await target_channel.send(content=content or None, embeds=embeds or None, files=files or None)
except Exception:
continue
await asyncio.sleep(0)
await progress_msg.edit(content="✅ Ripristino completato.")
load_env_from_file()
TOKEN = os.getenv("DISCORD_BOT_TOKEN", "").strip()
if not TOKEN:
raise RuntimeError("Imposta la variabile di ambiente DISCORD_BOT_TOKEN con il token del bot.")
bot.run(TOKEN)