-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_cog.py
312 lines (274 loc) · 13.6 KB
/
stats_cog.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
311
312
from datetime import datetime, timedelta
from math import ceil
from time import mktime
import discord
from discord import app_commands
from discord.ext import commands
from custom_logger import Logger
logger = Logger('gwaff.bot.stats')
from predictor import Prediction, xp_to_lvl
from predictor import (NoDataError, ZeroGrowthError,
TargetBoundsError)
from truerank import Truerank
from utils import resolve_member, ordinal
PREDICTION_MAX_DAYS: int = 365 # The maximum days that can be averaged over
# in predictions.
PREDICTION_DEFAULT_DAYS: int = 30
RANK_DEFAULT_THRESHOLD: int = 30
RANK_MAX_PAGE: int = 5
RANK_PAGE_SIZE: int = 25
ACCENT_COLOUR: str = '#ea625e'
class Stats_Cog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.truerank_ctxmenu = app_commands.ContextMenu(
name='True Rank',
callback=self.truerank_ctx
)
self.bot.tree.add_command(self.truerank_ctxmenu)
@app_commands.command(name="predict",
description="Predict when you will pass a given level, "
"xp, or member")
@app_commands.describe(target='Either a level, xp, or member to aim for',
member='The member to do the prediction for'
' (default you)',
period='The period to average your growth over'
' (default 30 days)',
growth='Override the average daily growth calculation',
hidden='Hide from others in this server (default False)')
async def predict(self, interaction: discord.Interaction,
target: str,
member: discord.User = None,
period: app_commands.Range[
int, 1, PREDICTION_MAX_DAYS] = PREDICTION_DEFAULT_DAYS,
growth: int = None,
hidden: bool = False):
await interaction.response.defer(ephemeral=hidden)
member = resolve_member(interaction, member)
if member is False:
await interaction.followup.send(":bust_in_silhouette: "
"That person in not in the server "
"or hasn't reached level 15")
return
elif target == member:
await interaction.followup.send(":mirror: "
"The target cannot be the same as "
"the member")
return
try:
prediction = Prediction(member=member.id,
target=target,
period=period,
growth=growth)
days = prediction.evaluate()
except ValueError:
await interaction.followup.send(":1234: "
"Level or xp must be a whole number")
return
except ZeroGrowthError:
await interaction.followup.send(":chart_with_downwards_trend: "
"That person has earnt no xp recently")
return
except NoDataError:
await interaction.followup.send(":mag: That target does not exist")
return
except TargetBoundsError:
await interaction.followup.send(":telescope: "
"That target is too far away")
return
except Exception as e:
await interaction.followup.send(f":question: An unknown error occurred:\n {e}")
raise e
date = mktime((datetime.now() + timedelta(days=days)).timetuple())
member_name = "You" if member is interaction.user else f"<@{member.id}>"
target: str
if prediction.target_type == 'xp':
target = str(int(prediction.target)) + ' xp'
elif prediction.target_type == 'level':
target = 'level ' + str(prediction.target)
elif prediction.target_type == 'user':
target = str(target)
if not target.startswith("<@"):
target = f"<@{target}>"
if days <= 0:
await interaction.followup.send(f"{member_name} will never reach "
f"{target} at this rate")
return
else:
target = str(target)
await interaction.followup.send(f"{member_name} will reach {target} on "
f"<t:{round(date)}:D> <t:{round(date)}:R> "
f"at a rate of {round(prediction.growth)} "
f"xp per day")
@app_commands.command(name="truerank",
description="Tells you your position out of only active members")
@app_commands.describe(member='The member to check (default you)',
threshold=f"The monthly xp needed to be listed "
f"(default {RANK_DEFAULT_THRESHOLD})",
hidden='Hide from others in this server (default False)')
async def rank_true(self, interaction: discord.Interaction,
member: discord.User = None,
threshold: int = RANK_DEFAULT_THRESHOLD,
hidden: bool = False):
await interaction.response.defer(ephemeral=hidden)
member = resolve_member(interaction, member)
if member is False:
await interaction.followup.send(":bust_in_silhouette: "
"That person in not in the server "
"or hasn't reached level 15")
return
try:
truerank = Truerank(threshold=threshold)
result = truerank.find_index(member.id)
except IndexError:
await interaction.followup.send(":bust_in_silhouette: "
"That person has not been online "
"recently enough")
return
member_name = "You are" if member == interaction.user \
else f"<@{member.id}> is"
rank = result.get('rank', 0)
if rank <= 0:
await interaction.followup.send(f"{member_name} ranked "
f"{ordinal(result['rank'] + 1)} in the server")
else:
xp = result['xp']
other_id = result['other_ID']
other_xp = result['other_xp']
other_name = result['other_name']
await interaction.followup.send(f"{member_name} ranked "
f"{ordinal(rank + 1)} in the server, "
f"{round(other_xp - xp)} behind "
f"<@{str(other_id)}> ({other_name})")
@app_commands.command(name="leaderboard",
description="Shows the leaderboard of active members")
@app_commands.describe(page="The page to display (default 1)",
threshold=f"The monthly xp needed to be listed "
f"(default {RANK_DEFAULT_THRESHOLD})",
hidden="Hide from others in this server (default False)")
async def leaderboard(self, interaction: discord.Interaction,
page: app_commands.Range[int, 1, RANK_MAX_PAGE] = 1,
threshold: app_commands.Range[int, 0] = RANK_DEFAULT_THRESHOLD,
hidden: bool = False):
await interaction.response.defer(ephemeral=hidden)
truerank = Truerank(threshold=threshold)
description = ''
page_start = (page - 1) * RANK_PAGE_SIZE
page_end = page * RANK_PAGE_SIZE
for index, item in enumerate(truerank.values[page_start:page_end]):
description += f"\n**{index + 1 + page_start})**" \
f"<@{item['ID']}> ({item['name']})" \
f"({round(item['xp'])} XP)"
if len(description) <= 0:
await interaction.followup.send(":1234: This page does not exist")
return
description += f"\nPage: {page}/{ceil(len(truerank.values) / RANK_PAGE_SIZE)}"
board = discord.Embed(title='Leaderboard',
description=description,
colour=discord.Colour.from_str(ACCENT_COLOUR))
await interaction.followup.send(embed=board)
@app_commands.command(name="user",
description="Shows details about the specified user")
@app_commands.describe(user="The user to search for",
hidden="Hide from others in this server (default False)")
async def user_info(self, interaction: discord.Interaction,
user: discord.User,
hidden: bool = False):
await interaction.response.defer(ephemeral=hidden)
# name, id, xp, level, rank
member = resolve_member(interaction, user)
if member is False:
await interaction.followup.send(":bust_in_silhouette: "
"That person in not in the server "
"or hasn't reached level 15")
return
try:
truerank = Truerank(threshold=RANK_DEFAULT_THRESHOLD)
result = truerank.find_index(member.id)
except IndexError:
await interaction.followup.send(":bust_in_silhouette: "
"That person has not been online "
"recently enough")
return
id = user.id
name = result['name']
xp = result['xp']
level = xp_to_lvl(xp)
colour = result['colour']
avatar = result['avatar']
rank = result['rank']
embed = discord.Embed(title=name,
colour=discord.Colour.from_str(colour))
embed.add_field(name='User', value=f"<@{id}>")
embed.add_field(name='XP', value=xp)
embed.add_field(name='Level', value=level)
embed.add_field(name='Rank', value=rank)
embed.set_thumbnail(url=avatar)
await interaction.followup.send(embed=embed)
async def truerank_ctx(self, interaction: discord.Interaction,
member: discord.User):
await interaction.response.defer(ephemeral=True)
member = resolve_member(interaction, member)
if member is False:
await interaction.followup.send(":bust_in_silhouette: "
"That person in not in the server "
"or hasn't reached level 15")
return
try:
truerank = Truerank(threshold=RANK_DEFAULT_THRESHOLD)
result = truerank.find_index(member.id)
except IndexError:
await interaction.followup.send(":bust_in_silhouette: "
"That person has not been online "
"recently enough")
return
member_name = "You are" if member == interaction.user \
else f"<@{member.id}> is"
rank = result.get('rank', 0)
if rank <= 0:
await interaction.followup.send(f"{member_name} ranked "
f"{ordinal(result['rank'] + 1)} in the server")
else:
xp = result['xp']
other_id = result['other_ID']
other_xp = result['other_xp']
other_name = result['other_name']
await interaction.followup.send(f"{member_name} ranked "
f"{ordinal(rank + 1)} in the server, "
f"{round(other_xp - xp)} behind "
f"<@{str(other_id)}> ({other_name})")
async def user_ctx(self, interaction: discord.Interaction,
member: discord.Member):
await interaction.response.defer(ephemeral=True)
member = resolve_member(interaction, member)
if member is False:
await interaction.followup.send(":bust_in_silhouette: "
"That person in not in the server "
"or hasn't reached level 15")
return
try:
truerank = Truerank(threshold=RANK_DEFAULT_THRESHOLD)
result = truerank.find_index(member.id)
except IndexError:
await interaction.followup.send(":bust_in_silhouette: "
"That person has not been online "
"recently enough")
return
id = member.id
name = result['name']
xp = result['xp']
level = xp_to_lvl(xp)
colour = result['colour']
avatar = result['avatar']
rank = result['rank']
embed = discord.Embed(title=name,
colour=discord.Colour.from_str(colour))
embed.add_field(name='User', value=f"<@{id}>")
embed.add_field(name='XP', value=xp)
embed.add_field(name='Level', value=level)
embed.add_field(name='Rank', value=rank)
embed.set_thumbnail(url=avatar)
await interaction.followup.send(embed=embed)
async def setup(bot: commands.Bot):
cog = Stats_Cog(bot)
await bot.add_cog(cog, guilds=bot.guilds)