Optimize config calls in few places (#3766)

* Just a tiny PR improving config call in a lot of places (Specially events and Help)

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* missed this one

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* welp

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* welp

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* welp

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* jack

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* Update redbot/cogs/mod/kickban.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* jack

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Draper
2020-04-20 18:29:36 +01:00
committed by GitHub
parent e4018ec677
commit f59e77002b
7 changed files with 153 additions and 116 deletions

View File

@@ -74,6 +74,7 @@ class HelpSettings:
verify_checks: bool = True
verify_exists: bool = False
tagline: str = ""
delete_delay: int = 0
# Contrib Note: This is intentional to not accept the bot object
# There are plans to allow guild and user specific help settings
@@ -123,40 +124,55 @@ class RedHelpFormatter:
For most cases, you should use this and only this directly.
"""
help_settings = await HelpSettings.from_context(ctx)
if help_for is None or isinstance(help_for, dpy_commands.bot.BotBase):
await self.format_bot_help(ctx)
await self.format_bot_help(ctx, help_settings=help_settings)
return
if isinstance(help_for, str):
try:
help_for = self.parse_command(ctx, help_for)
except NoCommand:
await self.command_not_found(ctx, help_for)
await self.command_not_found(ctx, help_for, help_settings=help_settings)
return
except NoSubCommand as exc:
if await ctx.bot._config.help.verify_exists():
await self.subcommand_not_found(ctx, exc.last, exc.not_found)
if help_settings.verify_exists:
await self.subcommand_not_found(
ctx, exc.last, exc.not_found, help_settings=help_settings
)
return
help_for = exc.last
if isinstance(help_for, commands.Cog):
await self.format_cog_help(ctx, help_for)
await self.format_cog_help(ctx, help_for, help_settings=help_settings)
else:
await self.format_command_help(ctx, help_for)
await self.format_command_help(ctx, help_for, help_settings=help_settings)
async def get_cog_help_mapping(self, ctx: Context, obj: commands.Cog):
async def get_cog_help_mapping(
self, ctx: Context, obj: commands.Cog, help_settings: HelpSettings
):
iterator = filter(lambda c: c.parent is None and c.cog is obj, ctx.bot.commands)
return {com.name: com async for com in self.help_filter_func(ctx, iterator)}
async def get_group_help_mapping(self, ctx: Context, obj: commands.Group):
return {
com.name: com async for com in self.help_filter_func(ctx, obj.all_commands.values())
com.name: com
async for com in self.help_filter_func(ctx, iterator, help_settings=help_settings)
}
async def get_bot_help_mapping(self, ctx):
async def get_group_help_mapping(
self, ctx: Context, obj: commands.Group, help_settings: HelpSettings
):
return {
com.name: com
async for com in self.help_filter_func(
ctx, obj.all_commands.values(), help_settings=help_settings
)
}
async def get_bot_help_mapping(self, ctx, help_settings: HelpSettings):
sorted_iterable = []
for cogname, cog in (*sorted(ctx.bot.cogs.items()), (None, None)):
cm = await self.get_cog_help_mapping(ctx, cog)
cm = await self.get_cog_help_mapping(ctx, cog, help_settings=help_settings)
if cm:
sorted_iterable.append((cogname, cm))
return sorted_iterable
@@ -168,11 +184,15 @@ class RedHelpFormatter:
"You can also type {ctx.clean_prefix}help <category> for more info on a category."
).format(ctx=ctx)
async def format_command_help(self, ctx: Context, obj: commands.Command):
async def format_command_help(
self, ctx: Context, obj: commands.Command, help_settings: HelpSettings
):
send = await ctx.bot._config.help.verify_exists()
send = help_settings.verify_exists
if not send:
async for _ in self.help_filter_func(ctx, (obj,), bypass_hidden=True):
async for _ in self.help_filter_func(
ctx, (obj,), bypass_hidden=True, help_settings=help_settings
):
# This is a really lazy option for not
# creating a separate single case version.
# It is efficient though
@@ -187,7 +207,8 @@ class RedHelpFormatter:
command = obj
description = command.description or ""
tagline = (await ctx.bot._config.help.tagline()) or self.get_default_tagline(ctx)
tagline = (help_settings.tagline) or self.get_default_tagline(ctx)
signature = (
f"`{T_('Syntax')}: {ctx.clean_prefix}{command.qualified_name} {command.signature}`"
)
@@ -195,7 +216,7 @@ class RedHelpFormatter:
if hasattr(command, "all_commands"):
grp = cast(commands.Group, command)
subcommands = await self.get_group_help_mapping(ctx, grp)
subcommands = await self.get_group_help_mapping(ctx, grp, help_settings=help_settings)
if await ctx.embed_requested():
emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []}
@@ -235,7 +256,7 @@ class RedHelpFormatter:
field = EmbedField(title, page, False)
emb["fields"].append(field)
await self.make_and_send_embeds(ctx, emb)
await self.make_and_send_embeds(ctx, emb, help_settings=help_settings)
else: # Code blocks:
@@ -272,7 +293,7 @@ class RedHelpFormatter:
)
)
pages = [box(p) for p in pagify(to_page)]
await self.send_pages(ctx, pages, embed=False)
await self.send_pages(ctx, pages, embed=False, help_settings=help_settings)
@staticmethod
def group_embed_fields(fields: List[EmbedField], max_chars=1000):
@@ -298,11 +319,11 @@ class RedHelpFormatter:
return ret
async def make_and_send_embeds(self, ctx, embed_dict: dict):
async def make_and_send_embeds(self, ctx, embed_dict: dict, help_settings: HelpSettings):
pages = []
page_char_limit = await ctx.bot._config.help.page_char_limit()
page_char_limit = help_settings.page_char_limit
page_char_limit = min(page_char_limit, 5500) # Just in case someone was manually...
author_info = {
@@ -367,16 +388,16 @@ class RedHelpFormatter:
pages.append(embed)
await self.send_pages(ctx, pages, embed=True)
await self.send_pages(ctx, pages, embed=True, help_settings=help_settings)
async def format_cog_help(self, ctx: Context, obj: commands.Cog):
async def format_cog_help(self, ctx: Context, obj: commands.Cog, help_settings: HelpSettings):
coms = await self.get_cog_help_mapping(ctx, obj)
if not (coms or await ctx.bot._config.help.verify_exists()):
coms = await self.get_cog_help_mapping(ctx, obj, help_settings=help_settings)
if not (coms or help_settings.verify_exists):
return
description = obj.format_help_for_context(ctx)
tagline = (await ctx.bot._config.help.tagline()) or self.get_default_tagline(ctx)
tagline = (help_settings.tagline) or self.get_default_tagline(ctx)
if await ctx.embed_requested():
emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []}
@@ -410,7 +431,7 @@ class RedHelpFormatter:
field = EmbedField(title, page, False)
emb["fields"].append(field)
await self.make_and_send_embeds(ctx, emb)
await self.make_and_send_embeds(ctx, emb, help_settings=help_settings)
else:
subtext = None
@@ -434,16 +455,16 @@ class RedHelpFormatter:
to_page = "\n\n".join(filter(None, (description, subtext_header, subtext)))
pages = [box(p) for p in pagify(to_page)]
await self.send_pages(ctx, pages, embed=False)
await self.send_pages(ctx, pages, embed=False, help_settings=help_settings)
async def format_bot_help(self, ctx: Context):
async def format_bot_help(self, ctx: Context, help_settings: HelpSettings):
coms = await self.get_bot_help_mapping(ctx)
coms = await self.get_bot_help_mapping(ctx, help_settings=help_settings)
if not coms:
return
description = ctx.bot.description or ""
tagline = (await ctx.bot._config.help.tagline()) or self.get_default_tagline(ctx)
tagline = (help_settings.tagline) or self.get_default_tagline(ctx)
if await ctx.embed_requested():
@@ -475,7 +496,7 @@ class RedHelpFormatter:
field = EmbedField(title, page, False)
emb["fields"].append(field)
await self.make_and_send_embeds(ctx, emb)
await self.make_and_send_embeds(ctx, emb, help_settings=help_settings)
else:
to_join = []
@@ -510,18 +531,17 @@ class RedHelpFormatter:
to_join.append(f"\n{tagline}")
to_page = "\n".join(to_join)
pages = [box(p) for p in pagify(to_page)]
await self.send_pages(ctx, pages, embed=False)
await self.send_pages(ctx, pages, embed=False, help_settings=help_settings)
@staticmethod
async def help_filter_func(
ctx, objects: Iterable[SupportsCanSee], bypass_hidden=False
ctx, objects: Iterable[SupportsCanSee], help_settings: HelpSettings, bypass_hidden=False,
) -> AsyncIterator[SupportsCanSee]:
"""
This does most of actual filtering.
"""
show_hidden = bypass_hidden or await ctx.bot._config.help.show_hidden()
verify_checks = await ctx.bot._config.help.verify_checks()
show_hidden = bypass_hidden or help_settings.show_hidden
verify_checks = help_settings.verify_checks
# TODO: Settings for this in core bot db
for obj in objects:
@@ -542,11 +562,16 @@ class RedHelpFormatter:
else:
yield obj
async def command_not_found(self, ctx, help_for):
async def command_not_found(self, ctx, help_for, help_settings: HelpSettings):
"""
Sends an error, fuzzy help, or stays quiet based on settings
"""
coms = {c async for c in self.help_filter_func(ctx, ctx.bot.walk_commands())}
coms = {
c
async for c in self.help_filter_func(
ctx, ctx.bot.walk_commands(), help_settings=help_settings
)
}
fuzzy_commands = await fuzzy_command_search(ctx, help_for, commands=coms, min_score=75)
use_embeds = await ctx.embed_requested()
if fuzzy_commands:
@@ -555,25 +580,25 @@ class RedHelpFormatter:
ret.set_author(
name=f"{ctx.me.display_name} {T_('Help Menu')}", icon_url=ctx.me.avatar_url
)
tagline = (await ctx.bot._config.help.tagline()) or self.get_default_tagline(ctx)
tagline = help_settings.tagline or self.get_default_tagline(ctx)
ret.set_footer(text=tagline)
await ctx.send(embed=ret)
else:
await ctx.send(ret)
elif await ctx.bot._config.help.verify_exists():
elif help_settings.verify_exists:
ret = T_("Help topic for *{command_name}* not found.").format(command_name=help_for)
if use_embeds:
ret = discord.Embed(color=(await ctx.embed_color()), description=ret)
ret.set_author(
name=f"{ctx.me.display_name} {T_('Help Menu')}", icon_url=ctx.me.avatar_url
)
tagline = (await ctx.bot._config.help.tagline()) or self.get_default_tagline(ctx)
tagline = help_settings.tagline or self.get_default_tagline(ctx)
ret.set_footer(text=tagline)
await ctx.send(embed=ret)
else:
await ctx.send(ret)
async def subcommand_not_found(self, ctx, command, not_found):
async def subcommand_not_found(self, ctx, command, not_found, help_settings: HelpSettings):
"""
Sends an error
"""
@@ -585,7 +610,7 @@ class RedHelpFormatter:
ret.set_author(
name=f"{ctx.me.display_name} {T_('Help Menu')}", icon_url=ctx.me.avatar_url
)
tagline = (await ctx.bot._config.help.tagline()) or self.get_default_tagline(ctx)
tagline = help_settings.tagline or self.get_default_tagline(ctx)
ret.set_footer(text=tagline)
await ctx.send(embed=ret)
else:
@@ -622,22 +647,25 @@ class RedHelpFormatter:
return com
async def send_pages(
self, ctx: Context, pages: List[Union[str, discord.Embed]], embed: bool = True
self,
ctx: Context,
pages: List[Union[str, discord.Embed]],
embed: bool = True,
help_settings: HelpSettings = None,
):
"""
Sends pages based on settings.
"""
# save on config calls
config_help = await ctx.bot._config.help()
channel_permissions = ctx.channel.permissions_for(ctx.me)
if not (channel_permissions.add_reactions and config_help["use_menus"]):
if not (channel_permissions.add_reactions and help_settings.use_menus):
max_pages_in_guild = config_help["max_pages_in_guild"]
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
delete_delay = config_help["delete_delay"]
delete_delay = help_settings.delete_delay
messages: List[discord.Message] = []
for page in pages: