-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
167 lines (135 loc) · 5.13 KB
/
utils.py
File metadata and controls
167 lines (135 loc) · 5.13 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
import discord
import singletons
from econ import user
import constants
import datetime
def GetFilePath(filename : str) -> str:
import os
# Get the directory where the script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
# Construct the full path to the save file
save_path = os.path.join(script_dir, filename)
return save_path
def ToMoney(amount: float) -> str:
"""Returns the amount in money format."""
return f"${amount:,.2f}"
def GetCommand(message : str) -> list[str]:
"""Returns a list of words from the message."""
command = message
command = command.strip(constants.PREFIX) # Removes Prefix from str
command = command.rstrip() # Removes trailing spaces
command = command.split(" ") # Converts into list of words
return command
def FindServer(sid : int) -> list[user.User]:
"""Returns the User List from the User Dictionary."""
# If the server is already in the dictionary, return the list of users.
for i in singletons.user_dict.keys():
if i == sid:
return singletons.user_dict[i]
# If the server is not in the dictionary, add the server to the dictionary and return an empty list.
singletons.user_dict[sid] = []
return singletons.user_dict[sid]
def FindUser(uid : int, sid: int) -> user.User:
"""Returns the User Object from the User Dictionary."""
# If the user is already in the list, return the user.
server = FindServer(sid=sid)
for i in server:
if i.uid == uid:
return i
# If the user is not in the list, add the user to the list and return the user.
singletons.user_dict[sid].append(user.User(uid=uid))
return singletons.user_dict[sid][-1]
async def IsValidMention(mention : str) -> bool:
# Check if the user is valid.
try:
mentioned_user_id = int(mention.strip("<@>"))
await singletons.client.fetch_user(mentioned_user_id)
return True
except:
return False
def StripMention(mention : str) -> int:
"""Returns the user ID from the mention."""
return int(mention.strip("<@>").strip())
async def ReplyWithException(message: discord.Message, exception_msg: str = "Exception!", exception_desc: str = "") -> None:
"""Replies to message that caused an exception with exception detail."""
embed = discord.Embed(
title=exception_msg,
color=constants.EXCEPTION_COLOR,
description=exception_desc
)
await message.reply(embed=embed)
def FindItemInList(name : str, item_list, user = None):# -> econessentials.Item:
"""Returns Item object from item list."""
if user != None:
for page in item_list:
for item in page:
if item.GetName().lower().replace(" ","") == name.lower().replace(" ",""):
return item
return None
else:
for item in item_list:
if item.GetName().lower().replace(" ","") == name.lower().replace(" ",""):
return type(item)() # Returns new instance of the same object type, not the same object.
return None
def FindItem(name : str, user : user.User = None):
for market in [singletons.market, singletons.black_market]:
item = FindItemInList(name=name, item_list=market, user=user)
if item != None:
return item
return None
def StripEmpty(_list : list[str]) -> list[str]:
"""Removes empty elements."""
i = 0
while i < len(_list):
if len(_list[i]) == 0:
_list.pop(i)
else:
i += 1
return _list
def GetTimeDelta(initial_time : datetime.datetime):
"""Returns the time delta between the current time and the given time in game time."""
time_delta = datetime.datetime.now() - initial_time
hours, remainder = divmod(time_delta.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return {
"days": hours,
"hours": minutes,
"minutes": seconds,
}
def GetClockTime(initial_time : datetime.datetime):
"""Returns the time delta between the current time and the given time in game clock time and the day of the week in a dictionary."""
time_delta = datetime.datetime.now() - initial_time
hours, remainder = divmod(time_delta.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
# Shifts every one to the left to convert to in game time.
ingame_days = hours
ingame_hours = minutes % 24
ingame_minutes = seconds % 60
clocktime = datetime.datetime.strptime(f"{ingame_hours}:{ingame_minutes}", "%H:%M")
week_day = GetWeekDay(days=ingame_days)
return {
"clock": clocktime,
"week day":week_day,
"days":ingame_days,
}
def GetWeekDay(days : int) -> str:
"""Returns the day of the week from the given number of days elapsed."""
match days % 7:
case 0:
return "Mon"
case 1:
return "Tue"
case 2:
return "Wed"
case 3:
return "Thu"
case 4:
return "Fri"
case 5:
return "Sat"
case 6:
return "Sun"
case 0:
return "Mon"
case _:
return "Error"