-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
754 lines (640 loc) · 32.9 KB
/
bot.py
File metadata and controls
754 lines (640 loc) · 32.9 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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
import os
import re
import requests
import aiohttp
import discord
import unicodedata
import logging
import asyncio
from api_client import APIClient
from logging.handlers import RotatingFileHandler
from discord.ext import commands
from discord import Intents
from dotenv import load_dotenv
from database import Database
# Umgebungsvariablen laden
load_dotenv()
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
RCON_API_TOKEN = os.getenv("RCON_API_TOKEN")
RCON_API_URL = os.getenv("RCON_API_URL")
DB_FILE = os.getenv("DB_FILE", "vips.db")
ALLOWED_ROLES = os.getenv("ALLOWED_ROLES", "").split(",")
LOG_FILE = "send_vip.log"
VIP_FILTERS = os.getenv("VIP_FILTERS", "").split(",")
VIP_REGEX = re.compile(os.getenv("VIP_REGEX", r"(\S+)\s(.+)\s(\d{4}-\d{2}-\d{2}T.+)"))
AUTO_SYNC_INTERVAL = int(os.getenv("AUTO_SYNC_INTERVAL", 24)) # In Stunden
VIP_LOG_CHANNEL = int(os.getenv("VIP_LOG_CHANNEL", 0))
# Logger einrichten
logger = logging.getLogger("VIPBotLogger")
logger.setLevel(logging.INFO)
handler = RotatingFileHandler(LOG_FILE, maxBytes=5 * 1024 * 1024, backupCount=2, encoding="utf-8")
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def log_to_file(log_message, level="INFO"):
"""Schreibt Nachrichten mit verschiedenen Log-Levels in die Protokolldatei."""
if level == "INFO":
logger.info(log_message)
elif level == "DEBUG":
logger.debug(log_message)
elif level == "ERROR":
logger.error(log_message)
else:
logger.warning(f"Unbekanntes Log-Level: {level}. Nachricht: {log_message}")
# Datenbank-Setup
db = Database(DB_FILE)
db.setup_tables()
# API-Client für Hauptserver und Zielserver erstellen
main_api = APIClient(base_url=RCON_API_URL, token=RCON_API_TOKEN)
target_api = APIClient(base_url=os.getenv("TARGET_API_URL"), token=os.getenv("TARGET_API_TOKEN"))
# Intents für den Bot definieren
intents = Intents.default()
intents.message_content = True
class VIPBot(commands.Bot):
async def setup_hook(self):
self.loop.create_task(auto_sync_vips())
# Bot initialisieren
bot = VIPBot(command_prefix="!", intents=intents, reconnect=True)
def check_allowed_roles():
"""Decorator, um zu überprüfen, ob der Benutzer die erforderliche Rolle hat."""
async def predicate(ctx):
if not ALLOWED_ROLES:
return True
user_roles = [str(role.id) for role in ctx.author.roles]
return any(role_id in ALLOWED_ROLES for role_id in user_roles)
return commands.check(predicate)
@bot.command()
@check_allowed_roles()
async def restore_vip(ctx, player_id: str):
"""Stellt einen gelöschten VIP aus der Backup-Tabelle wieder her."""
try:
restored_vip = db.restore_vip(player_id)
if restored_vip:
player_id, description, expiration = restored_vip
log_to_file(f"🔄 VIP wiederhergestellt: {player_id} - {description} - {expiration}", level="INFO")
embed = discord.Embed(
title="✅ VIP wiederhergestellt",
description=f"Der VIP `{player_id}` wurde erfolgreich wiederhergestellt.",
color=discord.Color.green()
)
embed.add_field(name="📋 Beschreibung", value=description, inline=False)
embed.add_field(name="⏳ Ablaufdatum", value=expiration, inline=False)
else:
embed = discord.Embed(
title="❌ VIP nicht gefunden",
description=f"Es gibt keinen gelöschten VIP mit der ID `{player_id}`.",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
except Exception as e:
log_to_file(f"Fehler beim Wiederherstellen des VIPs {player_id}: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def update_vips(ctx):
"""VIP-Liste aktualisieren und Daten synchronisieren."""
try:
headers = {"Authorization": f"Bearer {RCON_API_TOKEN}"}
response = requests.get(f"{RCON_API_URL}/api/download_vips", headers=headers)
if response.status_code == 200:
raw_data = unicodedata.normalize("NFKC", response.content.decode("utf-8", errors="replace").strip())
lines = raw_data.split("\n")
# Filter und Regex anwenden
filtered_lines = [line for line in lines if any(filter_term in line for filter_term in VIP_FILTERS)]
parsed_vips = [
re.match(VIP_REGEX, line).groups() for line in filtered_lines if re.match(VIP_REGEX, line)
]
# Tabelle aktualisieren
db.delete_all("vips")
db.bulk_insert("vips", parsed_vips)
await ctx.send("VIP-Datenbank erfolgreich aktualisiert.")
else:
log_to_file(f"Fehler beim Abrufen der VIPs: {response.status_code}")
await ctx.send("Fehler beim Abrufen der VIPs.")
except Exception as e:
log_to_file(f"Fehler: {str(e)}")
await ctx.send(f"Ein Fehler ist aufgetreten: {str(e)}")
async def _update_vips(ctx):
"""Aktualisiert die VIP-Datenbank für Hauptserver und Zielserver."""
try:
headers = {"Authorization": f"Bearer {RCON_API_TOKEN}"}
target_headers = {"Authorization": f"Bearer {os.getenv('TARGET_API_TOKEN')}"}
# **VIPs vom Hauptserver abrufen**
response = requests.get(f"{RCON_API_URL}/api/download_vips", headers=headers)
if response.status_code == 200:
raw_data = unicodedata.normalize("NFKC", response.content.decode("utf-8", errors="replace").strip())
lines = raw_data.split("\n")
filtered_lines = [line for line in lines if any(filter_term in line for filter_term in VIP_FILTERS)]
parsed_vips = [
re.match(VIP_REGEX, line).groups() for line in filtered_lines if re.match(VIP_REGEX, line)
]
# **Hauptserver-Tabelle aktualisieren**
db.delete_all("vips")
db.bulk_insert("vips", parsed_vips)
log_to_file("VIP-Datenbank vom Hauptserver wurde aktualisiert.", level="INFO")
else:
log_to_file(f"Fehler beim Abrufen der VIPs vom Hauptserver: {response.status_code}", level="ERROR")
await ctx.send("❌ Fehler beim Abrufen der VIPs vom Hauptserver.")
return False
# **VIPs vom Zielserver abrufen (für receiver_vips)**
target_response = requests.get(f"{os.getenv('TARGET_API_URL')}/api/download_vips", headers=target_headers)
if target_response.status_code == 200:
target_raw_data = unicodedata.normalize("NFKC", target_response.content.decode("utf-8", errors="replace").strip())
target_lines = target_raw_data.split("\n")
target_filtered_lines = [line for line in target_lines if any(filter_term in line for filter_term in VIP_FILTERS)]
target_parsed_vips = [
re.match(VIP_REGEX, line).groups() for line in target_filtered_lines if re.match(VIP_REGEX, line)
]
# **Zielserver-Tabelle aktualisieren**
db.delete_all("receiver_vips")
db.bulk_insert("receiver_vips", target_parsed_vips)
log_to_file("VIP-Datenbank vom Zielserver (receiver_vips) wurde aktualisiert.", level="INFO")
else:
log_to_file(f"Fehler beim Abrufen der VIPs vom Zielserver: {target_response.status_code}", level="ERROR")
await ctx.send("❌ Fehler beim Abrufen der VIPs vom Zielserver.")
return False
return True
except Exception as e:
log_to_file(f"Fehler beim Aktualisieren der VIP-Daten: {str(e)}", level="ERROR")
await ctx.send(f"❌ Fehler beim Aktualisieren der VIP-Daten: {str(e)}")
return False
async def auto_sync_vips():
"""Automatische Synchronisation basierend auf dem Intervall in der .env-Datei."""
await bot.wait_until_ready() # Warten, bis der Bot bereit ist
while not bot.is_closed():
log_to_file(f"⏳ Automatische VIP-Synchronisation gestartet (Intervall: {AUTO_SYNC_INTERVAL} Stunden)...", level="INFO")
sync_result = await sync_vips_task()
if sync_result:
await apply_sync_task()
# Umrechnung von Stunden in Sekunden (1 Stunde = 3600 Sekunden)
await asyncio.sleep(AUTO_SYNC_INTERVAL * 3600)
async def sync_vips_task():
"""Vergleicht die VIP-Listen und speichert Änderungen in der `sync`-Tabelle."""
try:
success = await _update_vips(None) # Datenbank aktualisieren
if not success:
return False
main_vips = {row[0]: row for row in db.fetch_all("vips")}
target_vips = {row[0]: row for row in db.fetch_all("receiver_vips")}
to_add = []
to_remove = []
to_update = []
for player_id, description, expiration in main_vips.values():
if player_id not in target_vips:
to_add.append((player_id, description, expiration))
elif target_vips[player_id][1] != description or target_vips[player_id][2] != expiration:
to_update.append((player_id, description, expiration)) # Änderung in `description` oder `expiration`
for player_id in target_vips:
if player_id not in main_vips:
to_remove.append(target_vips[player_id])
# 🛠 Bevor neue Änderungen eingefügt werden, alte `player_id`-Einträge in `sync` löschen
for player_id, _, _ in to_add + to_remove + to_update:
db.execute_query("DELETE FROM sync WHERE player_id = ?", (player_id,))
# `sync`-Tabelle aktualisieren
if to_add:
db.bulk_insert("sync", to_add)
if to_remove:
for player_id, description, expiration in to_remove:
db.execute_query("INSERT INTO sync (player_id, description, expiration) VALUES (?, ?, ?)", (player_id, description, expiration))
if to_update:
for player_id, description, expiration in to_update:
db.execute_query("INSERT INTO sync (player_id, description, expiration) VALUES (?, ?, ?)", (player_id, description, expiration))
log_to_file(f"{len(to_add)} VIPs zur `sync`-Tabelle hinzugefügt.", level="INFO")
log_to_file(f"{len(to_remove)} VIPs zur Entfernung in `sync` gespeichert.", level="INFO")
log_to_file(f"{len(to_update)} VIPs mit aktualisiertem Ablaufdatum oder Namen gespeichert.", level="INFO")
# **📢 Log-Channel Update**
if VIP_LOG_CHANNEL:
channel = bot.get_channel(VIP_LOG_CHANNEL)
if channel:
embed = discord.Embed(
title="🔄 VIP-Synchronisation – Änderungen erkannt",
description="Diese Änderungen wurden ermittelt. Nutze `!apply_sync`, um sie zu übernehmen.",
color=discord.Color.orange()
)
if to_add:
embed.add_field(name="✅ Hinzugefügt", value="\n".join([f"🟢 `{player_id}` - {description}" for player_id, description, expiration in to_add]) or "Keine neuen VIPs.", inline=False)
if to_remove:
embed.add_field(name="❌ Entfernt", value="\n".join([f"🔴 `{player_id}` - {description}" for player_id, description, expiration in to_remove]) or "Keine VIPs entfernt.", inline=False)
if to_update:
embed.add_field(name="🔄 Aktualisiert", value="\n".join([f"📝 `{player_id}` - {description} → `{expiration}`" for player_id, description, expiration in to_update]) or "Keine Aktualisierungen.", inline=False)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await channel.send(embed=embed)
else:
log_to_file(f"❌ Fehler: VIP_LOG_CHANNEL ({VIP_LOG_CHANNEL}) konnte nicht gefunden werden.", level="ERROR")
return True
except Exception as e:
log_to_file(f"Fehler bei der Synchronisation: {str(e)}", level="ERROR")
return False
async def apply_sync_task():
"""Wendet die geplanten VIP-Änderungen an, indem sie an den Zielserver gesendet werden."""
try:
target_headers = {"Authorization": f"Bearer {os.getenv('TARGET_API_TOKEN')}"}
add_vip_url = f"{os.getenv('TARGET_API_URL')}/api/add_vip"
remove_vip_url = f"{os.getenv('TARGET_API_URL')}/api/remove_vip"
sync_data = db.fetch_all("sync")
if not sync_data:
log_to_file("ℹ️ Keine Änderungen in `sync` gespeichert. `!sync_vips` zuerst ausführen.", level="INFO")
return
log_to_file(f"📋 Geplante Änderungen aus `sync`: {sync_data}", level="INFO")
main_vips = {row[0]: row for row in db.fetch_all("vips")}
target_vips = {row[0]: row for row in db.fetch_all("receiver_vips")}
to_add = []
to_remove = []
for player_id, description, expiration in sync_data:
if player_id in main_vips:
main_desc, main_exp = main_vips[player_id][1], main_vips[player_id][2]
if player_id in target_vips:
target_desc, target_exp = target_vips[player_id][1], target_vips[player_id][2]
# Falls sich die Beschreibung oder das Ablaufdatum geändert hat, entfernen und neu hinzufügen
if main_desc != target_desc or main_exp != target_exp:
to_remove.append(player_id)
to_add.append((player_id, main_desc, main_exp))
else:
to_add.append((player_id, main_desc, main_exp))
else:
to_remove.append(player_id)
log_to_file(f"🔄 VIPs zum Entfernen: {to_remove}", level="INFO")
log_to_file(f"✅ VIPs zum Hinzufügen: {to_add}", level="INFO")
added_count = 0
removed_count = 0
for player_id in to_remove:
async with aiohttp.ClientSession(headers=target_headers) as session:
async with session.post(remove_vip_url, json={"player_id": player_id}) as response:
response_text = await response.text()
if response.status == 200:
removed_count += 1
log_to_file(f"✅ Entfernt: {player_id}")
else:
log_to_file(f"❌ Fehler beim Entfernen von VIP {player_id}: {response.status} - {response_text}", level="ERROR")
for player_id, description, expiration in to_add:
async with aiohttp.ClientSession(headers=target_headers) as session:
async with session.post(add_vip_url, json={"player_id": player_id, "description": description, "expiration": expiration}) as response:
response_text = await response.text()
if response.status == 200:
added_count += 1
log_to_file(f"✅ Hinzugefügt: {player_id} - {description} - {expiration}")
else:
log_to_file(f"❌ Fehler beim Hinzufügen von VIP {player_id}: {response.status} - {response_text}", level="ERROR")
db.delete_all("sync")
log_to_file(f"✅ Synchronisation abgeschlossen: {added_count} hinzugefügt, {removed_count} entfernt.", level="INFO")
except Exception as e:
log_to_file(f"❌ Fehler bei der Synchronisation: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler bei der Synchronisation",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def export_vips(ctx):
"""Exportiert die aktuelle VIP-Liste in eine Datei."""
if not await _update_vips(ctx):
return
try:
output_file = os.getenv("VIP_LIST_FILE", "vip_list.txt")
vips = db.fetch_all("vips")
if not vips:
embed = discord.Embed(
title="ℹ️ Keine VIP-Daten gefunden",
description="Die VIP-Datenbank ist leer. Keine Datei zum Exportieren.",
color=discord.Color.blue()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
return
with open(output_file, "w", encoding="utf-8") as file:
for player_id, description, expiration in vips:
file.write(f"{player_id} {description} {expiration}\n")
embed = discord.Embed(
title="📥 VIP-Liste exportiert",
description="Die aktuelle VIP-Liste wurde exportiert. Lade sie hier herunter:",
color=discord.Color.green()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed, file=discord.File(output_file))
except Exception as e:
log_to_file(f"Fehler beim Exportieren der VIP-Liste: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler beim Exportieren",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def sync_vips(ctx):
"""Vergleicht die VIP-Listen und speichert Änderungen in der `sync`-Tabelle, bevor sie an den Zielserver gesendet werden."""
if not await _update_vips(ctx):
return
try:
main_vips = {row[0]: row for row in db.fetch_all("vips")}
target_vips = {row[0]: row for row in db.fetch_all("receiver_vips")}
to_add = []
to_remove = []
to_update = []
for player_id, description, expiration in main_vips.values():
if player_id not in target_vips:
to_add.append((player_id, description, expiration))
elif target_vips[player_id][2] != expiration:
to_update.append((player_id, description, expiration))
for player_id in target_vips:
if player_id not in main_vips:
to_remove.append(target_vips[player_id])
# `sync`-Tabelle leeren und Änderungen speichern
db.delete_all("sync")
if to_add:
db.bulk_insert("sync", to_add)
if to_remove:
for player_id, description, expiration in to_remove:
db.execute_query("INSERT INTO sync (player_id, description, expiration) VALUES (?, ?, ?)", (player_id, description, expiration))
if to_update:
for player_id, description, expiration in to_update:
db.execute_query("INSERT INTO sync (player_id, description, expiration) VALUES (?, ?, ?)", (player_id, description, expiration))
log_to_file(f"{len(to_add)} VIPs zur `sync`-Tabelle hinzugefügt.", level="INFO")
log_to_file(f"{len(to_remove)} VIPs zur Entfernung in `sync` gespeichert.", level="INFO")
log_to_file(f"{len(to_update)} VIPs mit aktualisiertem Ablaufdatum gespeichert.", level="INFO")
# **📢 Log-Channel Update**
if VIP_LOG_CHANNEL:
channel = bot.get_channel(VIP_LOG_CHANNEL)
if channel:
embed = discord.Embed(
title="🔄 VIP-Synchronisation – Änderungen erkannt",
description="Diese Änderungen wurden ermittelt. Nutze `!apply_sync`, um sie zu übernehmen.",
color=discord.Color.orange()
)
if to_add:
embed.add_field(
name="✅ Hinzugefügt",
value="\n".join([f"🟢 `{player_id}` - {description} - {expiration}" for player_id, description, expiration in to_add]) or "Keine neuen VIPs.",
inline=False
)
if to_remove:
embed.add_field(
name="❌ Entfernt",
value="\n".join([f"🔴 `{player_id}` - {description}" for player_id, description, expiration in to_remove]) or "Keine VIPs entfernt.",
inline=False
)
if to_update:
embed.add_field(
name="🔄 Ablaufdatum aktualisiert",
value="\n".join([f"📝 `{player_id}` - {description} → `{expiration}`" for player_id, description, expiration in to_update]) or "Keine Ablaufdatum-Änderungen.",
inline=False
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await channel.send(embed=embed)
await ctx.send("✅ Synchronisation abgeschlossen. Änderungen mit `!apply_sync` übernehmen.")
except Exception as e:
log_to_file(f"Fehler bei der Synchronisation: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler bei der VIP-Synchronisation",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def apply_sync(ctx):
"""Wendet die geplanten VIP-Änderungen an, indem sie an den Zielserver gesendet werden."""
try:
target_headers = {"Authorization": f"Bearer {os.getenv('TARGET_API_TOKEN')}"}
add_vip_url = f"{os.getenv('TARGET_API_URL')}/api/add_vip"
remove_vip_url = f"{os.getenv('TARGET_API_URL')}/api/remove_vip"
sync_data = db.fetch_all("sync")
if not sync_data:
embed = discord.Embed(
title="ℹ️ Keine Änderungen in `sync` gespeichert",
description="Nutze zuerst `!sync_vips`, um Änderungen zu berechnen.",
color=discord.Color.blue()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
return
main_vips = {row[0]: row for row in db.fetch_all("vips")}
target_vips = {row[0]: row for row in db.fetch_all("receiver_vips")}
to_add = [row for row in sync_data if row[0] in main_vips and row[0] not in target_vips]
to_remove = [row for row in sync_data if row[0] in target_vips and row[0] not in main_vips]
added_count = 0
removed_count = 0
for player_id, description, expiration in to_add:
async with aiohttp.ClientSession(headers=target_headers) as session:
async with session.post(add_vip_url, json={"player_id": player_id, "description": description, "expiration": expiration}) as response:
if response.status == 200:
added_count += 1
log_to_file(f"✅ VIP hinzugefügt: {player_id} - {description} - {expiration}")
else:
error_text = await response.text()
log_to_file(f"❌ Fehler beim Hinzufügen von VIP {player_id}: {error_text}", level="ERROR")
for player_id, _, _ in to_remove:
async with aiohttp.ClientSession(headers=target_headers) as session:
async with session.post(remove_vip_url, json={"player_id": player_id}) as response:
if response.status == 200:
removed_count += 1
log_to_file(f"✅ VIP entfernt: {player_id}")
else:
error_text = await response.text()
log_to_file(f"❌ Fehler beim Entfernen von VIP {player_id}: {error_text}", level="ERROR")
db.delete_all("sync")
embed = discord.Embed(
title="✅ VIP-Änderungen übernommen",
description=f"Es wurden `{added_count}` VIPs hinzugefügt und `{removed_count}` VIPs entfernt.",
color=discord.Color.green()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
except Exception as e:
log_to_file(f"Fehler beim Anwenden der Synchronisation: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler bei der Synchronisation",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def show_sync(ctx):
"""Zeigt die geplanten VIP-Änderungen in der `sync`-Tabelle an."""
try:
sync_data = db.fetch_all("sync")
if not sync_data:
embed = discord.Embed(
title="ℹ️ Keine geplanten Änderungen",
description="Die `sync`-Tabelle ist leer. Nutze `!sync_vips`, um Änderungen zu berechnen.",
color=discord.Color.blue()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
return
embed = discord.Embed(
title="📋 Geplante VIP-Änderungen",
description="Diese VIPs sind in der `sync`-Tabelle gespeichert.",
color=discord.Color.blue()
)
for player_id, description, expiration in sync_data:
embed.add_field(
name=f"🆔 `{player_id}`",
value=f"📋 **Beschreibung**: `{description}`\n⏳ **Ablaufdatum**: `{expiration}`",
inline=False
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
except Exception as e:
log_to_file(f"Fehler beim Abrufen von `sync`: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler beim Abrufen der `sync`-Daten",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def clear_vips(ctx):
"""Löscht alle VIP-Daten und speichert sie vor dem Löschen in der Backup-Tabelle."""
try:
vips = db.fetch_all("vips")
for player_id, description, expiration in vips:
db.backup_vip(player_id, description, expiration)
db.delete_all("vips")
db.delete_all("receiver_vips")
db.delete_all("sync")
log_to_file("Alle VIP-Daten wurden gelöscht und gesichert.", level="INFO")
embed = discord.Embed(
title="🗑 VIP-Datenbank geleert",
description="Alle VIP-Daten wurden gelöscht und ins Backup verschoben.",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
except Exception as e:
log_to_file(f"Fehler beim Löschen der VIP-Daten: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler beim Löschen der VIP-Daten",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def check_vip(ctx, name: str):
"""Sucht nach einem VIP in der Datenbank basierend auf einem Teilstring des Namens."""
try:
query = "SELECT * FROM vips WHERE description LIKE ?"
results = db.execute_query(query, (f"%{name}%",))
if results:
embed = discord.Embed(
title="🔍 VIP-Suche",
description=f"Ergebnisse für `{name}`:",
color=discord.Color.blue()
)
for player_id, description, expiration in results:
embed.add_field(
name=f"🆔 `{player_id}`",
value=f"📋 **Beschreibung**: `{description}`\n⏳ **Ablaufdatum**: `{expiration}`",
inline=False
)
else:
embed = discord.Embed(
title="❌ Kein VIP gefunden",
description=f"Kein VIP enthält `{name}` in der Datenbank.",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
except Exception as e:
log_to_file(f"Fehler beim Überprüfen von VIPs mit Name `{name}`: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler bei der VIP-Suche",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def show_backup(ctx):
"""Exportiert das VIP-Backup als Datei."""
try:
backup_vips = db.fetch_all("vip_backup")
if not backup_vips:
embed = discord.Embed(
title="ℹ️ Kein VIP-Backup gefunden",
description="Es wurden keine gelöschten VIPs im Backup gespeichert.",
color=discord.Color.blue()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
return
backup_file = "vip_backup.txt"
with open(backup_file, "w", encoding="utf-8") as file:
for player_id, description, expiration, deleted_at in backup_vips:
file.write(f"{player_id} {description} {expiration} {deleted_at}\n")
embed = discord.Embed(
title="📥 VIP-Backup exportiert",
description="Die gelöschten VIPs wurden exportiert. Lade die Datei hier herunter:",
color=discord.Color.green()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed, file=discord.File(backup_file))
except Exception as e:
log_to_file(f"Fehler beim Exportieren des VIP-Backups: {str(e)}", level="ERROR")
embed = discord.Embed(
title="❌ Fehler beim Exportieren des VIP-Backups",
description=f"Ein Fehler ist aufgetreten: `{str(e)}`",
color=discord.Color.red()
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.command()
@check_allowed_roles()
async def vipbot(ctx):
"""Zeigt eine Übersicht aller verfügbaren Befehle des VIP-Bots."""
embed = discord.Embed(
title="🤖 VIP-Bot Befehle",
description="Hier sind alle verfügbaren Befehle für den VIP-Bot:",
color=discord.Color.blue()
)
# Synchronisation & Verwaltung
embed.add_field(name="🔄 `!sync_vips`", value="Berechnet Änderungen und speichert sie in `sync`.", inline=False)
embed.add_field(name="📋 `!show_sync`", value="Zeigt die geplanten VIP-Änderungen aus `sync` an.", inline=False)
embed.add_field(name="✅ `!apply_sync`", value="Wendet die geplanten Änderungen aus `sync` an und sendet sie an den Zielserver.", inline=False)
# VIP-Datenbank
embed.add_field(name="📥 `!export_vips`", value="Exportiert die aktuelle VIP-Liste und sendet sie als Datei.", inline=False)
embed.add_field(name="🗑 `!clear_vips`", value="Speichert VIPs im Backup und löscht sie aus `vips`, `receiver_vips` und `sync`.", inline=False)
embed.add_field(name="🔍 `!check_vip <name>`", value="Überprüft, ob ein VIP mit einem bestimmten Namen in der Datenbank existiert (auch Teilstrings).", inline=False)
# Backup & Wiederherstellung
embed.add_field(name="🛡 `!show_backup`", value="Zeigt alle VIPs im Backup an.", inline=False)
embed.add_field(name="♻️ `!restore_vip <player_id>`", value="Stellt einen gelöschten VIP aus dem Backup wieder her.", inline=False)
embed.add_field(name="ℹ️ `!vipbot`", value="Zeigt diese Befehlsübersicht an.", inline=False)
embed.add_field(
name="ℹ️ `!?????`",
value="Kommende Features-Ideen an [Fw.Schultz](https://discord.com/users/275297833970565121).",
inline=False
)
embed.set_footer(text="VIP-Bot | Erstellt von Fw.Schultz")
await ctx.send(embed=embed)
@bot.event
async def on_ready():
print(f"✅ Bot ist eingeloggt als {bot.user.name}")
# Debugging für VIP_LOG_CHANNEL
if VIP_LOG_CHANNEL:
channel = bot.get_channel(VIP_LOG_CHANNEL)
if channel:
print(f"✅ VIP_LOG_CHANNEL gefunden: {channel.name} (ID: {VIP_LOG_CHANNEL})")
await channel.send("🔔 **VIP-Bot ist gestartet und sendet in diesen Kanal!**")
else:
print(f"❌ VIP_LOG_CHANNEL {VIP_LOG_CHANNEL} nicht gefunden! Überprüfe die Channel-ID.")
# Bot starten
bot.run(DISCORD_BOT_TOKEN)