Skip to content

Commit 6bb1486

Browse files
authored
7 enhancement 30 day leaderbord (#131)
1 parent 8d75312 commit 6bb1486

15 files changed

Lines changed: 476 additions & 108 deletions

button_views/leaderboard_buttons.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import discord
22
from enum import Enum
33

4+
from datahandler.tempLeaderboard import SortBy
5+
46

57
class SortedByEnum(Enum):
68
XP = 0
@@ -9,18 +11,17 @@ class SortedByEnum(Enum):
911

1012

1113
class LeaderboardButtons(discord.ui.View):
12-
def __init__(self, utils):
14+
def __init__(self, utils, timeFrame=None):
1315
self.sorted_by = SortedByEnum.XP
16+
self.timeFrame = timeFrame
1417
self.utils = utils
1518
super().__init__(timeout=None)
1619

1720
# @discord.ui.button(label="Page:", row=0, disabled=True, style=discord.ButtonStyle.secondary)
1821
# async def page_spacer(self, interaction: discord.Interaction, button: discord.ui.Button):
1922
# await interaction.response.defer(ephemeral=False)
2023

21-
@discord.ui.button(
22-
label="TOP", row=0, style=discord.ButtonStyle.primary, emoji="⏫"
23-
)
24+
@discord.ui.button(label="TOP", row=0, style=discord.ButtonStyle.primary, emoji="⏫")
2425
async def go_to_page_one(
2526
self, interaction: discord.Interaction, button: discord.ui.Button
2627
):
@@ -41,9 +42,7 @@ async def go_to_page_one(
4142
print("Error: Leaderboard button should have have a message")
4243
await interaction.response.defer(ephemeral=False)
4344

44-
@discord.ui.button(
45-
label="UP", row=0, style=discord.ButtonStyle.primary, emoji="⬆"
46-
)
45+
@discord.ui.button(label="UP", row=0, style=discord.ButtonStyle.primary, emoji="⬆")
4746
async def previous_page(
4847
self, interaction: discord.Interaction, button: discord.ui.Button
4948
):
@@ -98,9 +97,7 @@ async def next_page(
9897
# async def sort_by_spacer(self, interaction: discord.Interaction, button: discord.ui.Button):
9998
# await interaction.response.defer(ephemeral=False)
10099

101-
@discord.ui.button(
102-
label="EXP", row=1, style=discord.ButtonStyle.primary, emoji="🌟"
103-
)
100+
@discord.ui.button(label="EXP", row=1, style=discord.ButtonStyle.primary, emoji="🌟")
104101
async def sort_by_xp(
105102
self, interaction: discord.Interaction, button: discord.ui.Button
106103
):
@@ -171,5 +168,25 @@ async def sort_by_messages(
171168
await interaction.response.defer(ephemeral=False)
172169

173170
def _getLeaderBoard(self, page: int, sortBy: SortedByEnum) -> str:
171+
if self.timeFrame:
172+
return self._getTempLeaderBoard(page, sortBy)
173+
else:
174+
return self._getLeaderBoard(page, sortBy)
175+
176+
def _getTotalLeaderBoard(self, page: int, sortBy: SortedByEnum) -> str:
174177
leaderboard_text = self.utils.getLeaderboardPageBy(page, sortBy.value)
175178
return leaderboard_text
179+
180+
def _getTempLeaderBoard(self, page: int, sortByEnum: SortedByEnum) -> str:
181+
if sortByEnum == SortedByEnum.XP:
182+
sortBy = SortBy.VOICE_TEXT
183+
elif sortByEnum == SortedByEnum.VOICE:
184+
sortBy = SortBy.VOICE
185+
elif sortByEnum == SortedByEnum.MESSAGES:
186+
sortBy = SortBy.TEXTCOUNT
187+
else:
188+
sortBy = SortBy.NULL
189+
leaderboard_text = self.utils.getTempLeaderboardPageBy(
190+
page, sortBy, self.timeFrame
191+
)
192+
return leaderboard_text

button_views/poll_buttons.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class PollView(discord.ui.View):
1212
1313
Note that after a bot restart the polls need to be reopened.
1414
"""
15+
1516
def __init__(self, button_amount: int, poll: Poll, pollID: int):
1617
self.button_amount: int = button_amount
1718
self.poll: Poll = poll
@@ -22,7 +23,12 @@ def __init__(self, button_amount: int, poll: Poll, pollID: int):
2223

2324
def _create_button(self, vote_for_option: int) -> Button:
2425
button = VoteForButton(
25-
vote_for_option, self.poll, self.pollID, style=discord.ButtonStyle.primary, emoji=POLL_EMOJIS[vote_for_option])
26+
vote_for_option,
27+
self.poll,
28+
self.pollID,
29+
style=discord.ButtonStyle.primary,
30+
emoji=POLL_EMOJIS[vote_for_option],
31+
)
2632
return button
2733

2834

@@ -39,7 +45,7 @@ async def callback(self, interaction: discord.Interaction):
3945
await interaction.response.send_message(
4046
f"You voted for option {self.option + 1}: '{option_text}'.",
4147
ephemeral=True,
42-
delete_after=60
48+
delete_after=60,
4349
)
4450

4551
async def _vote_for_option(self, user, option, message):

cogs/commandgiverole.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ def search_table(self, channel_id, message_id):
344344
table_temp = self.get_table_content(table_name)
345345
table_mid = table_temp["messageid"]
346346
table_cid = table_temp["channelid"]
347-
if str(channel_id) == str(table_cid) and str(message_id) == str(
348-
table_mid
349-
):
347+
if str(channel_id) == str(table_cid) and str(message_id) == str(table_mid):
350348
table_content = table_temp
351349
break
352350
return table_content

cogs/commandlistener.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ def __init__(self, bot):
2525

2626
@commands.Cog.listener()
2727
async def on_command_error(self, ctx, error):
28-
print(
29-
"Ignoring exception in command {}:".format(ctx.command), file=sys.stderr
30-
)
28+
print("Ignoring exception in command {}:".format(ctx.command), file=sys.stderr)
3129
traceback.print_exception(
3230
type(error), error, error.__traceback__, file=sys.stderr
3331
)
@@ -260,10 +258,7 @@ async def on_voice_state_update(self, member, before, after):
260258
and before.channel.name[-1].isdigit()
261259
):
262260
# Member left first channel
263-
if (
264-
before.channel.name[-1] == "1"
265-
and not before.channel.name[-2].isdigit()
266-
):
261+
if before.channel.name[-1] == "1" and not before.channel.name[-2].isdigit():
267262
# Delete last channel, which has no user in it
268263

269264
channelWithoutNumber = before.channel.name[:-1]
@@ -375,9 +370,9 @@ async def on_message(self, message):
375370
a = "" + message.content
376371

377372
# Stops user from writting in levelchannel none command messages
378-
if str(message.channel.id) == str(
379-
self.ch.getFromConfig("levelchannel")
380-
) and a[0] != self.ch.getFromConfig("command_prefix"):
373+
if str(message.channel.id) == str(self.ch.getFromConfig("levelchannel")) and a[
374+
0
375+
] != self.ch.getFromConfig("command_prefix"):
381376
await message.delete()
382377
return
383378

cogs/commandowner.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ async def stoplog(self, ctx):
110110
guildName = str(self.bot.get_guild(guildID))
111111
self.ch.config["log"] = "False"
112112
self.ch.saveConfig()
113-
await self.utils.log(
114-
f"Stopped to log users from Server:\n\t{guildName}", 2
115-
)
113+
await self.utils.log(f"Stopped to log users from Server:\n\t{guildName}", 2)
116114
# Sets the bot's presence to "Do not Disturb" to indicate it's not logging.
117115
await self.bot.change_presence(
118116
status=discord.Status.dnd,
@@ -200,9 +198,7 @@ async def reloadExtensions(self, ctx, *extensions):
200198
await self.bot.unload_extension(ext)
201199
await self.bot.load_extension(ext)
202200
reloadedExtensions.append(ext)
203-
await self.utils.log(
204-
f"Reloaded extensions: {', '.join(reloadedExtensions)}", 2
205-
)
201+
await self.utils.log(f"Reloaded extensions: {', '.join(reloadedExtensions)}", 2)
206202

207203

208204
class TestButton(discord.ui.View):

cogs/commandpoll.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,10 @@ async def poll_open(self, ctx, pollID):
461461
text = self.poll.pollString(pollID)
462462
button_amount = len(self.poll.getOptions(pollID))
463463
pollView = PollView(button_amount, self.poll, pollID)
464-
messageSend = await ctx.send(content=f"{text}{ctx.author.mention}", view=pollView)
465-
self.poll.setMessageID(
466-
pollID, messageSend.id, messageSend.channel.id)
464+
messageSend = await ctx.send(
465+
content=f"{text}{ctx.author.mention}", view=pollView
466+
)
467+
self.poll.setMessageID(pollID, messageSend.id, messageSend.channel.id)
467468
log_message = (
468469
f"User {ctx.author.mention} opened the poll {pollID}"
469470
+ f" in channel {ctx.channel.name}."

cogs/commandsubroutine.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ async def removeRoleSubroutineFunction(self, currentTime, bufferTime, guild):
138138
log_message,
139139
2,
140140
)
141-
if (
142-
currentTime - offset
143-
) % interval < bufferTime and currentTime > offset:
141+
if (currentTime - offset) % interval < bufferTime and currentTime > offset:
144142
role = guild.get_role(int(toRemove))
145143
if role is None:
146144
log_message = (
@@ -239,19 +237,15 @@ def addMembersOnlineVoiceXp(self, serverid):
239237
]
240238
# Total all connected members
241239
for channel in voiceChanels:
242-
membersInChannel = [
243-
member for member in channel.members if not member.bot
244-
]
240+
membersInChannel = [member for member in channel.members if not member.bot]
245241
# Check if more than one person is in channel
246242
if len(membersInChannel) >= 2:
247243
membersNotMutedOrBot = [
248244
member
249245
for member in membersInChannel
250246
if not (member.voice.self_mute or member.bot)
251247
]
252-
self.uh.addAllUserVoice(
253-
[member.id for member in membersNotMutedOrBot]
254-
)
248+
self.uh.addAllUserVoice([member.id for member in membersNotMutedOrBot])
255249
# Extra XP
256250
membersStreamOrVideo = [
257251
member

cogs/commandsubserver.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,7 @@ def get_all_subserver_roles(self):
659659
]
660660
# Sort by subserver name. '+ r.name[1]' is for sorting sub roles on top of
661661
# subway roles, since '+ r.name[1]' is 'u' or 'w'.
662-
sorted_roles = sorted(
663-
sub_roles, key=lambda r: r.name.split("-")[1] + r.name[1]
664-
)
662+
sorted_roles = sorted(sub_roles, key=lambda r: r.name.split("-")[1] + r.name[1])
665663
if not sorted_roles:
666664
return []
667665
# Data pattern: [(sub-a, sw-a), ...]

cogs/commanduser.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from datahandler.configHandle import ConfigHandle
1212
from datahandler.userHandle import UserHandle
1313
from datahandler.commandrights import read_rights_of
14-
from button_views.leaderboard_buttons import LeaderboardButtons
14+
from button_views.leaderboard_buttons import LeaderboardButtons, SortBy
1515

1616
import datetime
1717
import time
@@ -344,9 +344,7 @@ async def decVoiceXP(self, ctx, userID, amount):
344344
amount = voiceXP
345345
voiceXP -= int(amount)
346346
self.uh.setUserVoice(userID, voiceXP)
347-
message += (
348-
f"Decreases user {str(self.bot.get_user(int(userID)))} voiceXP by {amount}. VoiceXP is set to {voiceXP}."
349-
)
347+
message += f"Decreases user {str(self.bot.get_user(int(userID)))} voiceXP by {amount}. VoiceXP is set to {voiceXP}."
350348
log_message = (
351349
f"User {ctx.author} decreases user "
352350
+ f"{str(self.bot.get_user(int(userID)))} voiceXP by {amount} to {voiceXP}."
@@ -384,9 +382,7 @@ async def incVoiceXP(self, ctx, userID, amount):
384382
voiceXP = self.uh.getUserVoice(userID)
385383
voiceXP += int(amount)
386384
self.uh.setUserVoice(userID, voiceXP)
387-
message += (
388-
f"Increases user {str(self.bot.get_user(int(userID)))} voiceXP by {amount}. VoiceXP is set to {voiceXP}."
389-
)
385+
message += f"Increases user {str(self.bot.get_user(int(userID)))} voiceXP by {amount}. VoiceXP is set to {voiceXP}."
390386
log_message = (
391387
f"User {ctx.author} increases user "
392388
+ f"{str(self.bot.get_user(int(userID)))} voiceXP by {amount} to {voiceXP}."
@@ -423,8 +419,7 @@ async def setTextXP(self, ctx, userID, amount):
423419
self.uh.addNewDataEntry(userID)
424420
self.uh.setUserText(userID, amount)
425421
message += (
426-
f"Set user {str(self.bot.get_user(int(userID)))} "
427-
+ f"textXP to {amount}."
422+
f"Set user {str(self.bot.get_user(int(userID)))} " + f"textXP to {amount}."
428423
)
429424
log_message = (
430425
f"User {ctx.author} set user "
@@ -463,7 +458,7 @@ async def decTextXP(self, ctx, userID, amount):
463458
else:
464459
textXP = self.uh.getUserText(userID)
465460
if int(amount) > textXP:
466-
amount = textXP
461+
amount = textXP
467462
textXP -= int(amount)
468463
self.uh.setUserText(userID, textXP)
469464
message += (
@@ -505,7 +500,7 @@ async def incTextXP(self, ctx, userID, amount):
505500
)
506501
self.uh.addNewDataEntry(userID)
507502
textXP = self.uh.getUserText(userID)
508-
textXP += int(amount)
503+
textXP += int(amount)
509504
self.uh.setUserText(userID, textXP)
510505
message += (
511506
f"Increase user {str(self.bot.get_user(int(userID)))} "
@@ -589,9 +584,7 @@ async def decTextCount(self, ctx, userID, amount):
589584
amount = textCount
590585
textCount -= int(amount)
591586
self.uh.setUserTextCount(userID, textCount)
592-
message += (
593-
f"Decreases user {str(self.bot.get_user(int(userID)))} TextCount by {amount}. TextCount is set to {textCount}."
594-
)
587+
message += f"Decreases user {str(self.bot.get_user(int(userID)))} TextCount by {amount}. TextCount is set to {textCount}."
595588
log_message = (
596589
f"User {ctx.author} decreases user "
597590
+ f"{str(self.bot.get_user(int(userID)))} textCount by {amount} to {textCount}."
@@ -629,9 +622,7 @@ async def incTextCount(self, ctx, userID, amount):
629622
textCount = self.uh.getUserTextCount(userID)
630623
textCount += int(amount)
631624
self.uh.setUserTextCount(userID, textCount)
632-
message += (
633-
f"Increases user {str(self.bot.get_user(int(userID)))} TextCount by {amount}. TextCount is set to {textCount}."
634-
)
625+
message += f"Increases user {str(self.bot.get_user(int(userID)))} TextCount by {amount}. TextCount is set to {textCount}."
635626
log_message = (
636627
f"User {ctx.author} increases user "
637628
+ f"{str(self.bot.get_user(int(userID)))} textCount by {amount} to {textCount}."
@@ -838,9 +829,7 @@ async def textunban(self, ctx, userID):
838829
f"User {ctx.author.mention} textunbaned {user.mention}"
839830
)
840831
else:
841-
await ctx.send(
842-
content="ERROR: User has no textban.", delete_after=3600
843-
)
832+
await ctx.send(content="ERROR: User has no textban.", delete_after=3600)
844833

845834
"""
846835
# When give star of the week should be queued
@@ -993,9 +982,7 @@ async def getLevel(self, ctx, *inputs):
993982
title=f"{member.nick} ({member.name})", color=12008408
994983
)
995984
embed.set_thumbnail(url=avatar_url)
996-
embed.add_field(
997-
name="TIME", value=f"{round(int(voiceXP)/30.0,1)}", inline=True
998-
)
985+
embed.add_field(name="TIME", value=f"{round(int(voiceXP)/30.0,1)}", inline=True)
999986
embed.add_field(name="TEXT", value=f"{str(textCount)}", inline=True)
1000987
embed.add_field(
1001988
name="EXP",
@@ -1012,7 +999,7 @@ async def getLevel(self, ctx, *inputs):
1012999

10131000
@commands.command(name="top", brief="Sends an interactive rank list.")
10141001
@isInChannelCommand(*channel_userLeaderboard)
1015-
async def leaderboard(self, ctx):
1002+
async def leaderboard(self, ctx, *args):
10161003
"""
10171004
Spawns an interactive leaderboard in the "⏫level" via the command 'top'.
10181005
Displays the first 10 member with the highest XP total.
@@ -1032,15 +1019,22 @@ async def leaderboard(self, ctx):
10321019
10331020
Creates a leaderboard and posts it with the emojis to manipulate it.
10341021
"""
1035-
await self.utils.log(f"+top by {ctx.author}", 1) # Notify Mods
1022+
await self.utils.log(f"+top {args} by {ctx.author}", 1) # Notify Mods
10361023
# Create leaderboard
1037-
text = f"{self.utils.getLeaderboardPageBy(0,0)}{ctx.author.mention}"
1038-
message = await ctx.send(
1039-
text, view=LeaderboardButtons(self.utils), delete_after=86400
1040-
)
1041-
# reactionsarr = ["⏫", "⬅", "➡", "⏰", "💌"]
1042-
# for emoji in reactionsarr:
1043-
# await message.add_reaction(emoji)
1024+
if len(args) > 0 and str(args[0]).isdigit():
1025+
timeFrame = int(args[0])
1026+
text = self.utils.getTempLeaderboardPageBy(
1027+
0, SortBy.VOICE_TEXT, timeFrame=timeFrame
1028+
)
1029+
else:
1030+
timeFrame = None
1031+
text = self.utils.getLeaderboardPageBy(0, 0)
1032+
if text:
1033+
await ctx.send(
1034+
text + str(ctx.author.mention),
1035+
view=LeaderboardButtons(self.utils, timeFrame=timeFrame),
1036+
delete_after=86400,
1037+
)
10441038
await ctx.message.delete()
10451039

10461040
@commands.command(name="quote", brief="Sends an unique inspirational quote.")

0 commit comments

Comments
 (0)