-
Notifications
You must be signed in to change notification settings - Fork 146
feat(examples): add new examples about CV2 #1419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| websites: list[Website] = await fetch_websites() | ||
| web_components = [ | ||
| ui.Section( | ||
| ui.TextDisplay("### " + website.name), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ui.TextDisplay("### " + website.name), | |
| ui.TextDisplay(f"### {website.name}"), |
| last_page_size = len(data) % TODO_PER_PAGE | ||
| total_pages = len(data) // TODO_PER_PAGE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| last_page_size = len(data) % TODO_PER_PAGE | |
| total_pages = len(data) // TODO_PER_PAGE | |
| total_pages, last_page_size = divmod(len(data), TODO_PER_PAGE) |
| # ideally you don't do this for every button click, you just do it the first time | ||
| # then cache it and reuse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that?
| @bot.listen(disnake.Event.button_click) | ||
| async def back_btn(inter: disnake.MessageInteraction) -> None: | ||
| if not inter.component.custom_id: | ||
| return | ||
|
|
||
| # remember that this is a normal listener and will get called | ||
| # for every global button click so we ignore every other component | ||
| # except for the one we really care (todo_back_btn) | ||
| if not inter.component.custom_id.startswith("todo_back_btn"): | ||
| return | ||
|
|
||
| # we get our data from the button custom id that we built previously | ||
| invoker_id, current_page, total_pages, last_page_size = map( | ||
| int, inter.component.custom_id.split(":")[1:] | ||
| ) | ||
| await interaction_check(inter, invoker_id, inter.author.id) | ||
|
|
||
| # we implement a pac-man like effect, if you are at the first page | ||
| # it will bring you at the last page | ||
| if current_page == 0: | ||
| current_page = total_pages - 1 | ||
| else: | ||
| current_page -= 1 | ||
|
|
||
| await send_page(inter, current_page, total_pages, last_page_size) | ||
|
|
||
|
|
||
| @bot.listen(disnake.Event.button_click) | ||
| async def fast_back_btn(inter: disnake.MessageInteraction) -> None: | ||
| if not inter.component.custom_id: | ||
| return | ||
|
|
||
| if not inter.component.custom_id.startswith("todo_f_back_btn"): | ||
| return | ||
|
|
||
| # remember that this is a normal listener and will get called | ||
| # for every global button click so we ignore every other component | ||
| # except for the one we really care (todo_next_btn) | ||
| invoker_id, current_page, total_pages, last_page_size = map( | ||
| int, inter.component.custom_id.split(":")[1:] | ||
| ) | ||
| await interaction_check(inter, invoker_id, inter.author.id) | ||
|
|
||
| # bring the user to the first page | ||
| current_page = 0 | ||
| await send_page(inter, current_page, total_pages, last_page_size) | ||
|
|
||
|
|
||
| @bot.listen(disnake.Event.button_click) | ||
| async def next_btn(inter: disnake.MessageInteraction) -> None: | ||
| if not inter.component.custom_id: | ||
| return | ||
|
|
||
| if not inter.component.custom_id.startswith("todo_next_btn"): | ||
| return | ||
|
|
||
| # remember that this is a normal listener and will get called | ||
| # for every global button click so we ignore every other component | ||
| # except for the one we really care (todo_next_btn) | ||
| invoker_id, current_page, total_pages, last_page_size = map( | ||
| int, inter.component.custom_id.split(":")[1:] | ||
| ) | ||
| await interaction_check(inter, invoker_id, inter.author.id) | ||
|
|
||
| # we implement a pac-man like effect, if you are at the last page | ||
| # it will bring you at the first page | ||
| if current_page == (total_pages - 1): | ||
| current_page = 0 | ||
| else: | ||
| current_page += 1 | ||
|
|
||
| await send_page(inter, current_page, total_pages, last_page_size) | ||
|
|
||
|
|
||
| @bot.listen(disnake.Event.button_click) | ||
| async def fast_next_btn(inter: disnake.MessageInteraction) -> None: | ||
| if not inter.component.custom_id: | ||
| return | ||
|
|
||
| if not inter.component.custom_id.startswith("todo_f_next_btn"): | ||
| return | ||
|
|
||
| # remember that this is a normal listener and will get called | ||
| # for every global button click so we ignore every other component | ||
| # except for the one we really care (todo_next_btn) | ||
| invoker_id, current_page, total_pages, last_page_size = map( | ||
| int, inter.component.custom_id.split(":")[1:] | ||
| ) | ||
| await interaction_check(inter, invoker_id, inter.author.id) | ||
|
|
||
| # bring the user to the last page | ||
| current_page = total_pages - 1 | ||
| await send_page(inter, current_page, total_pages, last_page_size) | ||
|
|
||
|
|
||
| @bot.listen(disnake.Event.button_click) | ||
| async def options_btn(inter: disnake.MessageInteraction) -> None: | ||
| if not inter.component.custom_id: | ||
| return | ||
|
|
||
| if not inter.component.custom_id.startswith("todo_options"): | ||
| return | ||
|
|
||
| invoker_id, _ = map(int, inter.component.custom_id.split(":")[1:]) | ||
| await interaction_check(inter, invoker_id, inter.author.id) | ||
| await inter.send( | ||
| "Implement this logic yourself! You should now understand how this works.", ephemeral=True | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a point in having a separate listener for each subcomponent
Summary
If someone wonders how the paginator looks like:

Let me know if this is too complex or if I should add more comments or if I should cover even more new components
Checklist
uv run nox -s lintuv run nox -s pyright