-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
310 lines (266 loc) · 10.6 KB
/
bot.py
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
import os
import json
import discord
from discord.ext import commands
from dotenv import load_dotenv
from api_client import APIClient
# -------------------------------------------------------------------
# 1) Lade .env (z. B. für DISCORD_BOT_TOKEN, API_BASE_URL etc.)
# -------------------------------------------------------------------
load_dotenv()
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
API_BASE_URL = os.getenv("API_BASE_URL")
API_TOKEN = os.getenv("API_TOKEN")
ALLOWED_CHANNEL_ID = int(os.getenv("ALLOWED_CHANNEL_ID", 0))
VIP_LOG_CHANNEL_ID = int(os.getenv("VIP_LOG_CHANNEL_ID", 0))
# -------------------------------------------------------------------
# 2) Sprache über Umgebungsvariable setzen, Default = "en"
# -------------------------------------------------------------------
LANG_CODE = os.getenv("LANG_CODE", "en")
# -------------------------------------------------------------------
# 3) Lese language.json und lade die gewünschte Sprache in "lang"
# -------------------------------------------------------------------
here = os.path.dirname(os.path.abspath(__file__))
lang_file_path = os.path.join(here, "language.json")
try:
with open(lang_file_path, "r", encoding="utf-8") as f:
all_translations = json.load(f)
except FileNotFoundError:
raise FileNotFoundError(f"language.json nicht gefunden unter: {lang_file_path}")
if LANG_CODE not in all_translations:
print(f"Warnung: Sprache '{LANG_CODE}' nicht in language.json gefunden. Fallback = 'en'")
LANG_CODE = "en"
lang = all_translations[LANG_CODE]
# -------------------------------------------------------------------
# 4) Intents konfigurieren (muss auch im Developer Portal aktiviert sein)
# -------------------------------------------------------------------
intents = discord.Intents.default()
intents.message_content = True
intents.messages = True
intents.guilds = True
intents.guild_messages = True
class ConfirmVIPView(discord.ui.View):
"""
Ein View mit zwei Buttons (z.B. auf Deutsch oder Englisch).
"""
def __init__(self, api_client: APIClient, player_id: str, player_name: str):
super().__init__(timeout=None)
self.api_client = api_client
self.player_id = player_id
self.player_name = player_name # sollte bereits der neueste sein
@discord.ui.button(label=lang["confirm_vip_button"], style=discord.ButtonStyle.green)
async def confirm_vip_button(self, interaction: discord.Interaction, button: discord.ui.Button):
"""
Klickt man darauf, wird tatsächlich add_vip() ausgeführt.
"""
if not self.player_id:
await interaction.response.send_message(
lang["error_no_player_id"],
ephemeral=True
)
return
# VIP vergeben
try:
self.api_client.add_vip(self.player_id, description="VIP über Bot")
# Text mit Platzhaltern aus language.json
text = lang["vip_granted"].format(
player_id=self.player_id,
player_name=self.player_name
)
await interaction.response.send_message(text, ephemeral=True)
except Exception as e:
error_text = lang["error_vip_grant"].format(error=e)
await interaction.response.send_message(error_text, ephemeral=True)
return
# VIP erfolgreich vergeben -> Embed im Log-Kanal posten
log_channel = interaction.client.get_channel(VIP_LOG_CHANNEL_ID)
if log_channel:
embed = discord.Embed(
title=lang["new_vip_title"],
color=discord.Color.green(),
description=lang["new_vip_description"].format(
player_id=self.player_id,
player_name=self.player_name,
mention=interaction.user.mention
)
)
await log_channel.send(embed=embed)
# Beide Buttons deaktivieren
for child in self.children:
child.disabled = True
await interaction.message.edit(view=self)
self.stop()
@discord.ui.button(label=lang["cancel_button"], style=discord.ButtonStyle.red)
async def cancel_button(self, interaction: discord.Interaction, button: discord.ui.Button):
"""
Bricht den Vorgang ab, deaktiviert die Buttons
und sendet eine kurze Bestätigung.
"""
for child in self.children:
child.disabled = True
await interaction.message.edit(view=self)
await interaction.response.send_message(lang["process_aborted"], ephemeral=True)
self.stop()
class AddVIPModal(discord.ui.Modal):
"""
Modal mit Eingabefeldern für Spielername / Spieler-ID.
"""
def __init__(self, api_client: APIClient):
# Modal-Titel auch aus der Sprachdatei
super().__init__(title=lang["modal_title"])
self.api_client = api_client
# 1. Feld: Spielernamen (optional)
self.player_name_input = discord.ui.TextInput(
label=lang["label_player_name"],
required=False
)
# 2. Feld: Spieler-ID (optional)
self.player_id_input = discord.ui.TextInput(
label=lang["label_player_id"],
required=False
)
self.add_item(self.player_name_input)
self.add_item(self.player_id_input)
async def on_submit(self, interaction: discord.Interaction):
name_val = self.player_name_input.value.strip()
id_val = self.player_id_input.value.strip()
# Wenigstens eins muss ausgefüllt sein
if not name_val and not id_val:
await interaction.response.send_message(
lang["no_name_or_id"],
ephemeral=True
)
return
try:
if id_val:
data = self.api_client.get_players_history(player_id=id_val)
else:
data = self.api_client.get_players_history(player_name=name_val)
result = data.get("result", {})
players = result.get("players", [])
if not players:
await interaction.response.send_message(
lang["no_player_found"].format(value=(name_val or id_val)),
ephemeral=True
)
return
player_data = players[0]
except Exception as e:
error_text = lang["error_search"].format(error=e)
await interaction.response.send_message(
error_text,
ephemeral=True
)
return
# IDs und Namen
player_id = player_data.get("player_id", lang["unknown"])
names = player_data.get("names", [])
if names:
# Neuesten Namen anhand "last_seen" ermitteln:
most_recent = max(names, key=lambda n: n["last_seen"])
player_name = most_recent.get("name", lang["unknown"])
else:
player_name = lang["unknown"]
total_seconds = player_data.get("total_playtime_seconds", 0)
hours = round(total_seconds / 3600.0, 2)
# Embed erstellen
embed = discord.Embed(
title=lang["review_title"],
description=lang["review_desc"],
color=discord.Color.blue()
)
embed.add_field(
name=lang["embed_field_player_id"],
value=str(player_id),
inline=False
)
embed.add_field(
name=lang["embed_field_player_name"],
value=player_name,
inline=False
)
embed.add_field(
name=lang["embed_field_total_playtime"],
value=str(hours),
inline=False
)
# DM an den Admin
try:
dm = await interaction.user.create_dm()
view = ConfirmVIPView(self.api_client, player_id, player_name)
await dm.send(
content=lang["found_data_pre_dm"],
embed=embed,
view=view
)
await interaction.response.send_message(
lang["dm_sent_info"],
ephemeral=True
)
except Exception as e:
dm_error = lang["dm_send_error"].format(error=e)
await interaction.response.send_message(dm_error, ephemeral=True)
class AddVIPView(discord.ui.View):
"""
View mit dem Button im öffentlichen Kanal.
"""
def __init__(self, api_client: APIClient):
super().__init__(timeout=None)
self.api_client = api_client
@discord.ui.button(
label=lang["public_button_label"],
style=discord.ButtonStyle.green,
custom_id="vip_vergeben_button"
)
async def add_vip_button(
self,
interaction: discord.Interaction,
button: discord.ui.Button
):
await interaction.response.send_modal(AddVIPModal(self.api_client))
class MyBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix="!", intents=intents)
self.api_client = APIClient(API_BASE_URL, API_TOKEN)
async def setup_hook(self):
# Registriere die persistente View
self.add_view(AddVIPView(self.api_client))
async def on_ready(self):
print(f"Bot {self.user} ist online. ID={self.user.id}")
await self.ensure_vip_button_message()
async def ensure_vip_button_message(self):
"""
Sucht im ALLOWED_CHANNEL_ID nach einer vorhandenen Bot-Nachricht,
in der lang["public_button_message"] steht.
Falls vorhanden, editiert sie und hängt (erneut) unsere View an.
Falls nicht, sendet der Bot eine neue Nachricht.
"""
channel = self.get_channel(ALLOWED_CHANNEL_ID)
if not channel:
return
target_text = lang["public_button_message"]
existing_message = None
async for msg in channel.history(limit=50):
if (
msg.author.id == self.user.id
and msg.content.startswith(target_text)
):
existing_message = msg
break
if existing_message:
# Aktualisiere die vorhandene Nachricht
await existing_message.edit(
content=target_text,
view=AddVIPView(self.api_client)
)
print("Existierende VIP-Button-Nachricht aktualisiert.")
else:
# Falls keine passende Nachricht gefunden
await channel.send(
target_text,
view=AddVIPView(self.api_client)
)
print("Neue VIP-Button-Nachricht gesendet.")
if __name__ == "__main__":
bot = MyBot()
bot.run(DISCORD_BOT_TOKEN)