Skip to content

Commit

Permalink
Add EmbedListPaginator and fix invitefilter list
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenChen committed Feb 5, 2024
1 parent 7b312f4 commit b9efc41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cogs/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from utils.checks import is_staff
from utils.database import FilterKind
from utils.utils import text_to_discord_file
from utils.views import ConfirmationButtons
from utils.views import ConfirmationButtons, PaginatedEmbedView, EmbedListPaginator

if TYPE_CHECKING:
from kurisu import Kurisu
Expand Down Expand Up @@ -348,9 +348,27 @@ async def delete_invite(self, ctx: KurisuContext, code: str):
async def list_invites(self, ctx: KurisuContext):
"""List invites in the filter whitelist"""
if self.filters.approved_invites:
embed = discord.Embed()
embed.add_field(name='Invites', value='\n'.join(f"name: {invite.alias} code:{invite.code} uses:{invite.uses}" for invite in self.filters.approved_invites.values()))
await ctx.send(embed=embed)
embeds: list[discord.Embed] = []
idx = 0
text = ""
next_line = ""
n_invites = len(self.filters.approved_invites.values())
embeds.append(discord.Embed(title=f"List of invites [{idx+1}]"))
for n, invite in enumerate(self.filters.approved_invites.values(), start=1):
next_line = f"name: {invite.alias} code:{invite.code} uses:{invite.uses}\n"
if len(text) + len(next_line) > 1024:
embeds[idx].add_field(name='Invites', value=text)
text = next_line
if n < n_invites:
idx += 1
embeds.append(discord.Embed(title=f"List of invites [{idx+1}]"))
else:
text += next_line
next_line = ""
if not next_line:
embeds[idx].add_field(name='Invites', value=text)
view = PaginatedEmbedView(EmbedListPaginator(embeds))
await ctx.send(embed=view.paginator.current(), view=view)
else:
await ctx.send("The invite filter is empty!")

Expand Down
9 changes: 9 additions & 0 deletions utils/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ def __init__(self, paginator: BasePaginator, timeout: int = 20,
self.clear_items()


class EmbedListPaginator(BasePaginator):
def __init__(self, embeds: list[Embed]):
super().__init__(n_pages=len(embeds))
self.pages = {n: embed for n, embed in enumerate(embeds)}

def current(self):
return self.pages[self.idx]


class VoteButton(Button['SimpleVoteView']):
label: str

Expand Down

0 comments on commit b9efc41

Please sign in to comment.