Make embeds in help output consistent (#5452)

* Add `check_permissions` kwarg to `bot.embed_requested()`

* Make embeds in help consistent regardless of why it's being sent
This commit is contained in:
jack1142
2021-12-31 02:01:23 +01:00
committed by GitHub
parent faab711ec8
commit ff7c146b62
3 changed files with 27 additions and 10 deletions

View File

@@ -1208,12 +1208,14 @@ class Red(
channel: Union[discord.abc.GuildChannel, discord.abc.PrivateChannel], channel: Union[discord.abc.GuildChannel, discord.abc.PrivateChannel],
user: discord.abc.User, user: discord.abc.User,
command: Optional[commands.Command] = None, command: Optional[commands.Command] = None,
*,
check_permissions: bool = False,
) -> bool: ) -> bool:
""" """
Determine if an embed is requested for a response. Determine if an embed is requested for a response.
Parameters Arguments
---------- ---------
channel : `discord.abc.GuildChannel` or `discord.abc.PrivateChannel` channel : `discord.abc.GuildChannel` or `discord.abc.PrivateChannel`
The channel to check embed settings for. The channel to check embed settings for.
user : `discord.abc.User` user : `discord.abc.User`
@@ -1221,6 +1223,13 @@ class Red(
command : `redbot.core.commands.Command`, optional command : `redbot.core.commands.Command`, optional
The command ran. The command ran.
Keyword Arguments
-----------------
check_permissions : `bool`
If ``True``, this method will also check whether the bot can send embeds
in the given channel and if it can't, it will return ``False`` regardless of
the bot's embed settings.
Returns Returns
------- -------
bool bool
@@ -1237,6 +1246,9 @@ class Red(
if (user_setting := await self._config.user(user).embeds()) is not None: if (user_setting := await self._config.user(user).embeds()) is not None:
return user_setting return user_setting
else: else:
if not channel.permissions_for(channel.guild.me).embed_links:
return False
if (channel_setting := await self._config.channel(channel).embeds()) is not None: if (channel_setting := await self._config.channel(channel).embeds()) is not None:
return channel_setting return channel_setting

View File

@@ -239,9 +239,9 @@ class Context(DPYContext):
bool: bool:
:code:`True` if an embed is requested :code:`True` if an embed is requested
""" """
if self.guild and not self.channel.permissions_for(self.guild.me).embed_links: return await self.bot.embed_requested(
return False self.channel, self.author, command=self.command, check_permissions=True
return await self.bot.embed_requested(self.channel, self.author, command=self.command) )
async def maybe_send_embed(self, message: str) -> discord.Message: async def maybe_send_embed(self, message: str) -> discord.Message:
""" """

View File

@@ -358,7 +358,7 @@ class RedHelpFormatter(HelpFormatterABC):
grp = cast(commands.Group, command) grp = cast(commands.Group, command)
subcommands = await self.get_group_help_mapping(ctx, grp, help_settings=help_settings) subcommands = await self.get_group_help_mapping(ctx, grp, help_settings=help_settings)
if await ctx.embed_requested(): if await self.embed_requested(ctx):
emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []} emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []}
if description: if description:
@@ -539,7 +539,7 @@ class RedHelpFormatter(HelpFormatterABC):
description = obj.format_help_for_context(ctx) description = obj.format_help_for_context(ctx)
tagline = (help_settings.tagline) or self.get_default_tagline(ctx) tagline = (help_settings.tagline) or self.get_default_tagline(ctx)
if await ctx.embed_requested(): if await self.embed_requested(ctx):
emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []} emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []}
emb["footer"]["text"] = tagline emb["footer"]["text"] = tagline
@@ -606,7 +606,7 @@ class RedHelpFormatter(HelpFormatterABC):
description = ctx.bot.description or "" description = ctx.bot.description or ""
tagline = (help_settings.tagline) or self.get_default_tagline(ctx) tagline = (help_settings.tagline) or self.get_default_tagline(ctx)
if await ctx.embed_requested(): if await self.embed_requested(ctx):
emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []} emb = {"embed": {"title": "", "description": ""}, "footer": {"text": ""}, "fields": []}
@@ -705,6 +705,11 @@ class RedHelpFormatter(HelpFormatterABC):
else: else:
yield obj yield obj
async def embed_requested(self, ctx: Context) -> bool:
return await ctx.bot.embed_requested(
channel=ctx.channel, user=ctx.author, command=red_help, check_permissions=True
)
async def command_not_found(self, ctx, help_for, help_settings: HelpSettings): async def command_not_found(self, ctx, help_for, help_settings: HelpSettings):
""" """
Sends an error, fuzzy help, or stays quiet based on settings Sends an error, fuzzy help, or stays quiet based on settings
@@ -717,7 +722,7 @@ class RedHelpFormatter(HelpFormatterABC):
), ),
min_score=75, min_score=75,
) )
use_embeds = await ctx.embed_requested() use_embeds = await self.embed_requested(ctx)
if fuzzy_commands: if fuzzy_commands:
ret = await format_fuzzy_results(ctx, fuzzy_commands, embed=use_embeds) ret = await format_fuzzy_results(ctx, fuzzy_commands, embed=use_embeds)
if use_embeds: if use_embeds:
@@ -751,7 +756,7 @@ class RedHelpFormatter(HelpFormatterABC):
ret = _("Command *{command_name}* has no subcommand named *{not_found}*.").format( ret = _("Command *{command_name}* has no subcommand named *{not_found}*.").format(
command_name=command.qualified_name, not_found=not_found[0] command_name=command.qualified_name, not_found=not_found[0]
) )
if await ctx.embed_requested(): if await self.embed_requested(ctx):
ret = discord.Embed(color=(await ctx.embed_color()), description=ret) ret = discord.Embed(color=(await ctx.embed_color()), description=ret)
ret.set_author( ret.set_author(
name=_("{ctx.me.display_name} Help Menu").format(ctx=ctx), name=_("{ctx.me.display_name} Help Menu").format(ctx=ctx),