mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-23 15:35:47 +02:00
Create command to list custom commands, add shortcuts to memegen commands
This commit is contained in:
parent
8922489a41
commit
bf32a5ef47
8 changed files with 120 additions and 62 deletions
|
|
@ -1,5 +1,4 @@
|
|||
import discord
|
||||
from discord.ext import commands
|
||||
from overrides import overrides
|
||||
|
||||
from database.schemas import Bookmark
|
||||
|
|
@ -14,16 +13,16 @@ class BookmarkSource(PageSource[Bookmark]):
|
|||
"""PageSource for the Bookmark commands"""
|
||||
|
||||
@overrides
|
||||
def create_embeds(self, ctx: commands.Context):
|
||||
def create_embeds(self):
|
||||
for page in range(self.page_count):
|
||||
embed = discord.Embed(title="Bookmarks", colour=discord.Colour.blue())
|
||||
avatar_url = get_author_avatar(ctx).url
|
||||
embed.set_author(name=ctx.author.display_name, icon_url=avatar_url)
|
||||
avatar_url = get_author_avatar(self.ctx).url
|
||||
embed.set_author(name=self.ctx.author.display_name, icon_url=avatar_url)
|
||||
|
||||
description = ""
|
||||
description_data = []
|
||||
|
||||
for bookmark in self.get_page_data(page):
|
||||
description += f"`#{bookmark.bookmark_id}`: [{bookmark.label}]({bookmark.jump_url})\n"
|
||||
description_data.append(f"`#{bookmark.bookmark_id}`: [{bookmark.label}]({bookmark.jump_url})")
|
||||
|
||||
embed.description = description.strip()
|
||||
embed.description = "\n".join(description_data)
|
||||
self.embeds.append(embed)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Generic, Optional, TypeVar, cast
|
||||
|
||||
|
|
@ -13,50 +15,6 @@ __all__ = ["Menu", "PageSource"]
|
|||
T = TypeVar("T")
|
||||
|
||||
|
||||
class PageSource(ABC, Generic[T]):
|
||||
"""Base class that handles the embeds displayed in a menu"""
|
||||
|
||||
dataset: list[T]
|
||||
embeds: list[discord.Embed]
|
||||
page_count: int
|
||||
per_page: int
|
||||
|
||||
def __init__(self, ctx: commands.Context, dataset: list[T], *, per_page: int = 10):
|
||||
self.embeds = []
|
||||
self.dataset = dataset
|
||||
self.per_page = per_page
|
||||
self.page_count = self._get_page_count()
|
||||
self.create_embeds(ctx)
|
||||
self._add_embed_page_footers()
|
||||
|
||||
def _get_page_count(self) -> int:
|
||||
"""Calculate the amount of pages required"""
|
||||
if len(self.dataset) % self.per_page == 0:
|
||||
return len(self.dataset) // self.per_page
|
||||
|
||||
return (len(self.dataset) // self.per_page) + 1
|
||||
|
||||
def __getitem__(self, index: int) -> discord.Embed:
|
||||
return self.embeds[index]
|
||||
|
||||
def __len__(self):
|
||||
return self.page_count
|
||||
|
||||
def _add_embed_page_footers(self):
|
||||
"""Add the current page in the footer of every embed"""
|
||||
for i, embed in enumerate(self.embeds):
|
||||
embed.set_footer(text=f"{i + 1}/{self.page_count}")
|
||||
|
||||
@abstractmethod
|
||||
def create_embeds(self, ctx: commands.Context):
|
||||
"""Method that builds the list of embeds from the input data"""
|
||||
raise NotImplementedError
|
||||
|
||||
def get_page_data(self, page: int) -> list[T]:
|
||||
"""Get the chunk of the dataset for page [page]"""
|
||||
return self.dataset[page : page + self.per_page]
|
||||
|
||||
|
||||
class Menu(discord.ui.View):
|
||||
"""Base class for a menu"""
|
||||
|
||||
|
|
@ -166,3 +124,58 @@ class Menu(discord.ui.View):
|
|||
"""Button to show the last page"""
|
||||
self.current_page = len(self.source) - 1
|
||||
await self.display_current_state(interaction)
|
||||
|
||||
|
||||
class PageSource(ABC, Generic[T]):
|
||||
"""Base class that handles the embeds displayed in a menu"""
|
||||
|
||||
ctx: commands.Context
|
||||
dataset: list[T]
|
||||
embeds: list[discord.Embed]
|
||||
page_count: int
|
||||
per_page: int
|
||||
|
||||
def __init__(self, ctx: commands.Context, dataset: list[T], *, per_page: int = 10):
|
||||
self.ctx = ctx
|
||||
self.embeds = []
|
||||
self.dataset = dataset
|
||||
self.per_page = per_page
|
||||
self.page_count = self._get_page_count()
|
||||
self.create_embeds()
|
||||
self._add_embed_page_footers()
|
||||
|
||||
def _get_page_count(self) -> int:
|
||||
"""Calculate the amount of pages required"""
|
||||
if len(self.dataset) % self.per_page == 0:
|
||||
return len(self.dataset) // self.per_page
|
||||
|
||||
return (len(self.dataset) // self.per_page) + 1
|
||||
|
||||
def __getitem__(self, index: int) -> discord.Embed:
|
||||
return self.embeds[index]
|
||||
|
||||
def __len__(self):
|
||||
return self.page_count
|
||||
|
||||
def _add_embed_page_footers(self):
|
||||
"""Add the current page in the footer of every embed"""
|
||||
for i, embed in enumerate(self.embeds):
|
||||
embed.set_footer(text=f"{i + 1}/{self.page_count}")
|
||||
|
||||
@abstractmethod
|
||||
def create_embeds(self):
|
||||
"""Method that builds the list of embeds from the input data"""
|
||||
raise NotImplementedError
|
||||
|
||||
def get_page_data(self, page: int) -> list[T]:
|
||||
"""Get the chunk of the dataset for page [page]"""
|
||||
return self.dataset[page : page + self.per_page]
|
||||
|
||||
async def start(self, *, ephemeral: bool = False, timeout: Optional[int] = None) -> Menu:
|
||||
"""Shortcut to creating (and starting) a Menu with this source
|
||||
|
||||
This returns the created menu
|
||||
"""
|
||||
menu = Menu(self, ephemeral=ephemeral, timeout=timeout)
|
||||
await menu.start(self.ctx)
|
||||
return menu
|
||||
|
|
|
|||
24
didier/menus/custom_commands.py
Normal file
24
didier/menus/custom_commands.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import discord
|
||||
from overrides import overrides
|
||||
|
||||
from database.schemas import CustomCommand
|
||||
from didier.menus.common import PageSource
|
||||
|
||||
__all__ = ["CustomCommandSource"]
|
||||
|
||||
|
||||
class CustomCommandSource(PageSource[CustomCommand]):
|
||||
"""PageSource for custom commands"""
|
||||
|
||||
@overrides
|
||||
def create_embeds(self):
|
||||
for page in range(self.page_count):
|
||||
embed = discord.Embed(colour=discord.Colour.blue(), title="Custom Commands")
|
||||
|
||||
description_data = []
|
||||
|
||||
for command in self.get_page_data(page):
|
||||
description_data.append(command.name.title())
|
||||
|
||||
embed.description = "\n".join(description_data)
|
||||
self.embeds.append(embed)
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
import discord
|
||||
from discord.ext import commands
|
||||
from overrides import overrides
|
||||
|
||||
from database.schemas import MemeTemplate
|
||||
|
|
@ -12,7 +11,7 @@ class MemeSource(PageSource[MemeTemplate]):
|
|||
"""PageSource for meme templates"""
|
||||
|
||||
@overrides
|
||||
def create_embeds(self, ctx: commands.Context):
|
||||
def create_embeds(self):
|
||||
for page in range(self.page_count):
|
||||
# The colour of the embed is (69,4,20) with the values +100 because they were too dark
|
||||
embed = discord.Embed(title="Meme Templates", colour=discord.Colour.from_rgb(169, 14, 120))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue