1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-01 13:02:38 +00:00
modufur/tools/components.py

102 lines
3 KiB
Python
Raw Normal View History

2022-02-21 07:10:57 +00:00
import hikari
import lightbulb
from miru.ext import nav
2022-02-21 20:53:53 +00:00
plugin = lightbulb.Plugin("components")
2022-02-21 07:10:57 +00:00
class Back(nav.PrevButton):
def __init__(self):
2022-02-21 20:53:53 +00:00
super().__init__(style=hikari.ButtonStyle.SECONDARY, label="", emoji=None)
2022-02-21 07:10:57 +00:00
class Forward(nav.NextButton):
def __init__(self):
2022-02-21 20:53:53 +00:00
super().__init__(style=hikari.ButtonStyle.SECONDARY, label="", emoji=None)
2022-02-21 07:10:57 +00:00
class Confirm(nav.StopButton):
def __init__(self):
2022-02-21 20:53:53 +00:00
super().__init__(style=hikari.ButtonStyle.PRIMARY, label="", emoji=None)
2022-02-21 07:10:57 +00:00
async def callback(self, context):
2022-02-21 20:53:53 +00:00
await context.edit_response(content="**Searching...**", components=None)
2022-02-21 07:10:57 +00:00
self.view.stop()
async def before_page_change(self):
self.disabled = False if self.view.selected else True
2022-02-21 20:53:53 +00:00
2022-02-21 07:10:57 +00:00
class Select(nav.NavButton):
def __init__(self):
2022-02-21 20:53:53 +00:00
super().__init__(style=hikari.ButtonStyle.DANGER, label="", emoji=None)
2022-02-21 07:10:57 +00:00
async def callback(self, context):
if self.view.urls[self.view.current_page] not in self.view.selected:
self.view.selected.append(self.view.urls[self.view.current_page])
self._button(selected=True)
else:
self.view.selected.remove(self.view.urls[self.view.current_page])
self._button()
await context.edit_response(components=self.view.build())
async def before_page_change(self):
if self.view.urls[self.view.current_page] not in self.view.selected:
self._button()
else:
self._button(selected=True)
2022-03-04 05:59:46 +00:00
# Flip button state
2022-02-21 07:10:57 +00:00
def _button(self, *, selected=False):
self.style = hikari.ButtonStyle.SUCCESS if selected else hikari.ButtonStyle.DANGER
2022-02-21 20:53:53 +00:00
self.label = "" if selected else ""
2022-02-21 07:10:57 +00:00
try:
confirm = next((child for child in self.view.children if isinstance(child, Confirm)))
confirm.disabled = False if self.view.selected else True
except StopIteration:
pass
class Selector(nav.NavigatorView):
def __init__(self, *, pages=[], buttons=[], timeout=120, urls=[]):
2022-02-21 20:53:53 +00:00
super().__init__(pages=pages, buttons=buttons, timeout=timeout)
2022-02-21 07:10:57 +00:00
self.urls = urls
self.selected = []
self.saved = set()
self.timed_out = False
async def on_timeout(self):
if self._inter:
for button in self.children:
button.disabled = True
await self._inter.edit_initial_response(components=self.build())
self.timed_out = True
2022-03-04 05:59:46 +00:00
# Resend new navigator as edit of previous
2022-02-21 07:10:57 +00:00
async def send_edit(self, interaction):
self._inter = interaction
for button in self.children:
if isinstance(button, nav.NavButton):
await button.before_page_change()
payload = self._get_page_payload(self.pages[0])
await interaction.edit_initial_response(**payload)
self.start(await interaction.fetch_initial_response())
def load(bot):
bot.add_plugin(plugin)
2022-02-21 20:53:53 +00:00
2022-02-21 07:10:57 +00:00
def unload(bot):
bot.remove_plugin(plugin)