mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-12-05 17:02:32 -05:00
Add buttons to help (#5634)
Co-authored-by: Zephyrkul <23347632+Zephyrkul@users.noreply.github.com> Co-authored-by: Kowlin <10947836+Kowlin@users.noreply.github.com> Co-authored-by: Jakub Kuczys <me@jacken.men>
This commit is contained in:
@@ -31,6 +31,7 @@ import abc
|
||||
import asyncio
|
||||
from collections import namedtuple
|
||||
from dataclasses import dataclass, asdict as dc_asdict
|
||||
from enum import Enum
|
||||
from typing import Union, List, AsyncIterator, Iterable, cast
|
||||
|
||||
import discord
|
||||
@@ -39,6 +40,7 @@ from discord.ext import commands as dpy_commands
|
||||
from . import commands
|
||||
from .context import Context
|
||||
from ..i18n import Translator
|
||||
from ..utils.views import SimpleMenu
|
||||
from ..utils import can_user_react_in, menus
|
||||
from ..utils.mod import mass_purge
|
||||
from ..utils._internal_utils import fuzzy_command_search, format_fuzzy_results
|
||||
@@ -65,6 +67,14 @@ EmbedField = namedtuple("EmbedField", "name value inline")
|
||||
EMPTY_STRING = "\N{ZERO WIDTH SPACE}"
|
||||
|
||||
|
||||
class HelpMenuSetting(Enum):
|
||||
disabled = 0
|
||||
reactions = 1
|
||||
buttons = 2
|
||||
select = 3
|
||||
selectonly = 4
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HelpSettings:
|
||||
"""
|
||||
@@ -78,7 +88,7 @@ class HelpSettings:
|
||||
|
||||
page_char_limit: int = 1000
|
||||
max_pages_in_guild: int = 2
|
||||
use_menus: bool = False
|
||||
use_menus: HelpMenuSetting = HelpMenuSetting(0)
|
||||
show_hidden: bool = False
|
||||
show_aliases: bool = True
|
||||
verify_checks: bool = True
|
||||
@@ -103,7 +113,8 @@ class HelpSettings:
|
||||
Get the HelpSettings for the current context
|
||||
"""
|
||||
settings = await context.bot._config.help.all()
|
||||
return cls(**settings)
|
||||
menus = settings.pop("use_menus", 0)
|
||||
return cls(**settings, use_menus=HelpMenuSetting(menus))
|
||||
|
||||
@property
|
||||
def pretty(self):
|
||||
@@ -129,6 +140,14 @@ class HelpSettings:
|
||||
tagline_info = ""
|
||||
|
||||
data["tagline_info"] = tagline_info
|
||||
menus_str = {
|
||||
HelpMenuSetting.disabled: _("No"),
|
||||
HelpMenuSetting.reactions: _("Yes, reactions"),
|
||||
HelpMenuSetting.buttons: _("Yes, buttons"),
|
||||
HelpMenuSetting.select: _("Yes, buttons with select menu"),
|
||||
HelpMenuSetting.selectonly: _("Yes, select menu only"),
|
||||
}
|
||||
data["use_menus"] = menus_str[self.use_menus]
|
||||
|
||||
return _(
|
||||
"Maximum characters per page: {page_char_limit}"
|
||||
@@ -813,7 +832,31 @@ class RedHelpFormatter(HelpFormatterABC):
|
||||
"""
|
||||
Sends pages based on settings.
|
||||
"""
|
||||
if not (can_user_react_in(ctx.me, ctx.channel) and help_settings.use_menus):
|
||||
if help_settings.use_menus.value >= HelpMenuSetting.buttons.value:
|
||||
use_select = help_settings.use_menus.value == 3
|
||||
select_only = help_settings.use_menus.value == 4
|
||||
await SimpleMenu(
|
||||
pages,
|
||||
timeout=help_settings.react_timeout,
|
||||
use_select_menu=use_select,
|
||||
use_select_only=select_only,
|
||||
).start(ctx)
|
||||
|
||||
elif (
|
||||
can_user_react_in(ctx.me, ctx.channel)
|
||||
and help_settings.use_menus is HelpMenuSetting.reactions
|
||||
):
|
||||
# Specifically ensuring the menu's message is sent prior to returning
|
||||
m = await (ctx.send(embed=pages[0]) if embed else ctx.send(pages[0]))
|
||||
c = menus.DEFAULT_CONTROLS if len(pages) > 1 else {"\N{CROSS MARK}": menus.close_menu}
|
||||
# Allow other things to happen during menu timeout/interaction.
|
||||
asyncio.create_task(
|
||||
menus.menu(ctx, pages, c, message=m, timeout=help_settings.react_timeout)
|
||||
)
|
||||
# menu needs reactions added manually since we fed it a message
|
||||
menus.start_adding_reactions(m, c.keys())
|
||||
|
||||
else:
|
||||
max_pages_in_guild = help_settings.max_pages_in_guild
|
||||
use_DMs = len(pages) > max_pages_in_guild
|
||||
destination = ctx.author if use_DMs else ctx.channel
|
||||
@@ -855,16 +898,6 @@ class RedHelpFormatter(HelpFormatterABC):
|
||||
await mass_purge(messages, channel)
|
||||
|
||||
asyncio.create_task(_delete_delay_help(destination, messages, delete_delay))
|
||||
else:
|
||||
# Specifically ensuring the menu's message is sent prior to returning
|
||||
m = await (ctx.send(embed=pages[0]) if embed else ctx.send(pages[0]))
|
||||
c = menus.DEFAULT_CONTROLS if len(pages) > 1 else {"\N{CROSS MARK}": menus.close_menu}
|
||||
# Allow other things to happen during menu timeout/interaction.
|
||||
asyncio.create_task(
|
||||
menus.menu(ctx, pages, c, message=m, timeout=help_settings.react_timeout)
|
||||
)
|
||||
# menu needs reactions added manually since we fed it a message
|
||||
menus.start_adding_reactions(m, c.keys())
|
||||
|
||||
|
||||
@commands.command(name="help", hidden=True, i18n=_)
|
||||
|
||||
Reference in New Issue
Block a user