rewrite: Refactor ui utils, add pagers.

This commit is contained in:
2022-11-30 16:57:26 +02:00
parent dd8609fac0
commit 5bd05a84a9
9 changed files with 1282 additions and 526 deletions

46
bot/utils/ui/hooked.py Normal file
View File

@@ -0,0 +1,46 @@
from discord.ui.button import Button
from .leo import LeoUI
__all__ = (
'HookedItem',
'AButton',
'AsComponents'
)
class HookedItem:
"""
Mixin for Item classes allowing an instance to be used as a callback decorator.
"""
def __init__(self, *args, pass_kwargs={}, **kwargs):
super().__init__(*args, **kwargs)
self.pass_kwargs = pass_kwargs
def __call__(self, coro):
async def wrapped(view, interaction, **kwargs):
return await coro(view, interaction, self, **kwargs, **self.pass_kwargs)
self.callback = wrapped
return self
class AButton(HookedItem, Button):
...
class AsComponents(LeoUI):
"""
Simple container class to accept a number of Items and turn them into an attachable View.
"""
def __init__(self, *items, pass_kwargs={}, **kwargs):
super().__init__(**kwargs)
self.pass_kwargs = pass_kwargs
for item in items:
item.callback = self.wrap_callback(item.callback)
self.add_item(item)
def wrap_callback(self, coro):
async def wrapped(*args, **kwargs):
return await coro(self, *args, **kwargs, **self.pass_kwargs)
return wrapped