-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
288 lines (229 loc) · 9.4 KB
/
utils.py
File metadata and controls
288 lines (229 loc) · 9.4 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
import os
import json
import discord
import datetime
import requests
from dotenv import load_dotenv
from typing import Dict, Any, List, Optional
class PokemonUtils:
def __init__(self, pokedex_file: str = "PokeDex.json"):
self.pokedex_file = pokedex_file
self._pokedex_data = {}
self.load_pokedex()
def load_pokedex(self) -> None:
try:
with open(self.pokedex_file, "r") as file:
self._pokedex_data = json.load(file)
except FileNotFoundError:
print(f"PokeDex File {self.pokedex_file} Not Found")
self._pokedex_data = {}
except json.JSONDecodeError:
print(f"Invalid JSON In {self.pokedex_file}.")
self._pokedex_data = {}
def get_pokemon_by_number(self, number: int) -> Optional[Dict[str, Any]]:
if isinstance(self._pokedex_data, list):
if 0 <= number < len(self._pokedex_data):
pokemon_data = self._pokedex_data[number]
if pokemon_data and len(pokemon_data) > 0:
return pokemon_data
return None
return None
else:
return self._pokedex_data.get(number)
def search_pokemon_by_name(self, name: str) -> Optional[Dict[str, Any]]:
name_lower = name.lower()
if isinstance(self._pokedex_data, list):
for pokemon_data in self._pokedex_data:
if pokemon_data and pokemon_data.get("title", ""):
if name_lower in pokemon_data.get("title", "").lower():
return pokemon_data
else:
for pokemon_data in self._pokedex_data.values():
if name_lower in pokemon_data.get("title", "").lower():
return pokemon_data
return None
def get_pokemon_stats(self, pokemon_data: Dict[str, Any]) -> List[str]:
stats = []
for field in pokemon_data.get("fields", []):
if field["name"] == "Base Stats":
value = field["value"]
hp = self._extract_stat(value, "HP")
attack = self._extract_stat(value, "Attack")
defense = self._extract_stat(value, "Defense")
sp_atk = self._extract_stat(value, "Sp. Atk")
sp_def = self._extract_stat(value, "Sp. Def")
speed = self._extract_stat(value, "Speed")
total = hp + attack + defense + sp_atk + sp_def + speed
stats.extend(
[
f"**HP:** {hp}",
f"**Attack:** {attack}",
f"**Defense:** {defense}",
f"--------------",
f"**Sp. Atk:** {sp_atk}",
f"**Sp. Def:** {sp_def}",
f"--------------",
f"**Speed:** {speed}",
f"**Total:** {total}",
]
)
break
return stats
def _extract_stat(self, text: str, stat_name: str) -> int:
try:
start = text.find(f"**{stat_name}:** ") + len(f"**{stat_name}:** ")
if start == len(f"**{stat_name}:** ") - 1:
return 0
end = text.find("\n", start)
if end == -1:
end = len(text)
return int(text[start:end].strip())
except (ValueError, AttributeError):
return 0
def create_pokemon_embed(
self, pokemon_data: Dict[str, Any], include_stats: bool = True
) -> discord.Embed:
embed = discord.Embed(
title=pokemon_data.get("title", "Unknown Pokemon"),
color=pokemon_data.get("color", 0x2F3136),
)
image_url = pokemon_data.get("image", {}).get("url")
if image_url:
embed.set_thumbnail(url=image_url)
if include_stats:
stats = self.get_pokemon_stats(pokemon_data)
if stats:
embed.add_field(name="Stats", value="\n".join(stats), inline=False)
return embed
def save_pokemon_to_dex(self, pokemon_entry: Dict[str, Any]) -> bool:
try:
title = pokemon_entry.get("title", "")
if not title or "—" not in title:
print("Invalid Pokemon Entry: Missing Title Format")
return False
number_part = title.split("—")[0].strip().replace("#", "")
try:
pokemon_number = int(number_part)
except ValueError:
print(f"Invalid Pokemon Number: {number_part}")
return False
try:
with open(self.pokedex_file, "r") as file:
pokedex_data = json.load(file)
except FileNotFoundError:
pokedex_data = []
except json.JSONDecodeError:
print(f"Invalid JSON In {self.pokedex_file}")
return False
while len(pokedex_data) <= pokemon_number:
pokedex_data.append({})
pokedex_data[pokemon_number] = pokemon_entry
with open(self.pokedex_file, "w") as file:
json.dump(pokedex_data, file, indent=2)
self._pokedex_data = pokedex_data
print(f"Successfully Saved Pokemon #{pokemon_number} To PokeDex")
return True
except Exception as e:
print(f"Error saving Pokemon To PokeDex: {e}")
return False
class EmbedUtils:
@staticmethod
def create_success_embed(title: str, description: str) -> discord.Embed:
return discord.Embed(
title=f"✅ {title}", description=description, color=0x00FF00
)
@staticmethod
def create_error_embed(title: str, description: str) -> discord.Embed:
return discord.Embed(
title=f"❌ {title}", description=description, color=0xFF0000
)
@staticmethod
def create_info_embed(
title: str, description: str, color: int = 0x2F3136
) -> discord.Embed:
return discord.Embed(title=title, description=description, color=color)
@staticmethod
def create_profile_embed(user, user_data: tuple) -> discord.Embed:
net_total = "{:,}".format(user_data[1])
max_gambled = "{:,}".format(user_data[2])
gamble_wins = user_data[3]
gamble_losses = user_data[4]
gamble_wins_streak = user_data[5]
total_gambles = gamble_wins + gamble_losses
win_rate = (
round((gamble_wins / total_gambles) * 100, 2) if total_gambles > 0 else 0
)
embed = discord.Embed(
title=f"{user.display_name}'s Profile",
description=(
f"**Net Total**: {net_total}\n"
f"**Highest Gamble**: {max_gambled}\n\n"
f"**Gambles Done**: {total_gambles}\n"
f"**Wins**: {gamble_wins}\n"
f"**Losses**: {gamble_losses}\n\n"
f"**Win Rate**: {win_rate}%\n"
f"**Win Streak**: {gamble_wins_streak}"
),
color=0xF98D2F,
)
embed.timestamp = datetime.datetime.now()
if user.avatar:
embed.set_thumbnail(url=user.avatar.url)
else:
embed.set_thumbnail(url="https://cdn.discordapp.com/embed/avatars/0.png")
return embed
class TradeUtils:
@staticmethod
def save_trade_data(
participant1: str, participant2: str, message_link: str
) -> None:
trade_data = {
"participant1": participant1,
"participant2": participant2,
"message_link": message_link,
"timestamp": datetime.datetime.now().isoformat(),
}
try:
with open("Trades.json", "r") as file:
trades = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
trades = {"trades": []}
trades["trades"].append(trade_data)
with open("Trades.json", "w") as file:
json.dump(trades, file, indent=4)
def format_number(number: int) -> str:
return "{:,}".format(number)
def get_ordinal_suffix(position: int) -> str:
if 10 <= position % 100 <= 20:
suffix = "th"
else:
suffix = {1: "st", 2: "nd", 3: "rd"}.get(position % 10, "th")
return f"{position}{suffix}"
class Logutils:
@staticmethod
def log(ctx, command) -> None:
print(f"Info : {ctx.author} : {command} : {ctx.guild.id}")
webhook_url = os.getenv("WEBHOOK_URL")
if webhook_url:
timestamp = int(datetime.datetime.now().timestamp())
embed_data = {
"title": "🔍 Command Execution Log",
"description": f"**Timestamp :** <t:{timestamp}:t>\n### Command : `{command}`\n\n\n**User :** {ctx.author.mention}\n**Guild :** {ctx.guild.name} [ {ctx.guild.id}]",
"color": 0x5865F2,
"thumbnail": {
"url": (
ctx.author.avatar.url
if ctx.author.avatar
else "https://cdn.discordapp.com/embed/avatars/0.png"
)
},
}
payload = {"embeds": [embed_data]}
try:
requests.post(webhook_url, json=payload)
except requests.RequestException as e:
print(f"Failed To Send Webhook Log : {e}")
pokemon_utils = PokemonUtils()
embed_utils = EmbedUtils()
trade_utils = TradeUtils()
log_utils = Logutils()