Skip to content

Commit

Permalink
Support all Messageables in bot.embed_requested() (#5576)
Browse files Browse the repository at this point in the history
* Support all Messageables in bot.embed_requested

* Update usage in core

* Simplify [p]contact

This couldn't be done before this change.

I have also simplified getting embed color.

* Make `True` the new default for `check_permissions` kwarg
  • Loading branch information
Jackenmen authored Mar 21, 2022
1 parent f763d29 commit 0f299ae
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 51 deletions.
3 changes: 1 addition & 2 deletions redbot/cogs/mutes/mutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,7 @@ async def _send_dm_notification(
if not reason:
reason = _("No reason provided.")

# okay, this is some poor API to require PrivateChannel here...
if await self.bot.embed_requested(await user.create_dm(), user):
if await self.bot.embed_requested(user):
em = discord.Embed(
title=title,
description=reason,
Expand Down
3 changes: 1 addition & 2 deletions redbot/cogs/reports/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ async def discover_guild(
return guild

async def send_report(self, ctx: commands.Context, msg: discord.Message, guild: discord.Guild):

author = guild.get_member(msg.author.id)
report = msg.clean_content

Expand All @@ -207,7 +206,7 @@ async def send_report(self, ctx: commands.Context, msg: discord.Message, guild:
ticket_number = await self.config.guild(guild).next_ticket()
await self.config.guild(guild).next_ticket.set(ticket_number + 1)

if await self.bot.embed_requested(channel, author):
if await self.bot.embed_requested(channel):
em = discord.Embed(description=report, colour=await ctx.embed_colour())
em.set_author(
name=_("Report from {author}{maybe_nick}").format(
Expand Down
35 changes: 21 additions & 14 deletions redbot/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,26 +1205,24 @@ async def send_help_for(

async def embed_requested(
self,
channel: Union[discord.abc.GuildChannel, discord.abc.PrivateChannel],
user: discord.abc.User,
command: Optional[commands.Command] = None,
channel: discord.abc.Messageable,
*,
check_permissions: bool = False,
command: Optional[commands.Command] = None,
check_permissions: bool = True,
) -> bool:
"""
Determine if an embed is requested for a response.
Arguments
---------
channel : `discord.abc.GuildChannel` or `discord.abc.PrivateChannel`
The channel to check embed settings for.
user : `discord.abc.User`
The user to check embed settings for.
command : `redbot.core.commands.Command`, optional
The command ran.
channel : `discord.abc.Messageable`
The target messageable object to check embed settings for.
Keyword Arguments
-----------------
command : `redbot.core.commands.Command`, optional
The command ran.
This is auto-filled when ``channel`` is passed with command context.
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
Expand All @@ -1242,10 +1240,12 @@ async def get_command_setting(guild_id: int) -> Optional[bool]:
scope = self._config.custom(COMMAND_SCOPE, command.qualified_name, guild_id)
return await scope.embeds()

if isinstance(channel, discord.abc.PrivateChannel):
if (user_setting := await self._config.user(user).embeds()) is not None:
return user_setting
else:
# using dpy_commands.Context to keep the Messageable contract in full
if isinstance(channel, dpy_commands.Context):
command = command or channel.command
channel = channel.channel

if isinstance(channel, discord.TextChannel):
if check_permissions and not channel.permissions_for(channel.guild.me).embed_links:
return False

Expand All @@ -1257,6 +1257,13 @@ async def get_command_setting(guild_id: int) -> Optional[bool]:

if (guild_setting := await self._config.guild(channel.guild).embeds()) is not None:
return guild_setting
elif isinstance(channel, discord.GroupChannel):
# this only uses global settings
pass
else:
user = channel.recipient if isinstance(discord.DMChannel) else channel
if (user_setting := await self._config.user(user).embeds()) is not None:
return user_setting

# XXX: maybe this should be checked before guild setting?
if (global_command_setting := await get_command_setting(0)) is not None:
Expand Down
13 changes: 8 additions & 5 deletions redbot/core/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,20 @@ def embed_color(self):

async def embed_requested(self):
"""
Simple helper to call bot.embed_requested
with logic around if embed permissions are available
Short-hand for calling bot.embed_requested with permission checks.
Equivalent to:
.. code:: python
await ctx.bot.embed_requested(ctx)
Returns
-------
bool:
:code:`True` if an embed is requested
"""
return await self.bot.embed_requested(
self.channel, self.author, command=self.command, check_permissions=True
)
return await self.bot.embed_requested(self)

async def maybe_send_embed(self, message: str) -> discord.Message:
"""
Expand Down
4 changes: 1 addition & 3 deletions redbot/core/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,7 @@ async def help_filter_func(
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
)
return await ctx.bot.embed_requested(channel=ctx.channel, command=red_help)

async def command_not_found(self, ctx, help_for, help_settings: HelpSettings):
"""
Expand Down
27 changes: 4 additions & 23 deletions redbot/core/core_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3975,31 +3975,12 @@ async def contact(self, ctx: commands.Context, *, message: str):
successful = False

for destination in destinations:

is_dm = isinstance(destination, discord.User)
send_embed = None

if is_dm:
send_embed = await ctx.bot._config.user(destination).embeds()
else:
if not destination.permissions_for(destination.guild.me).send_messages:
continue
if destination.permissions_for(destination.guild.me).embed_links:
send_embed = await ctx.bot._config.channel(destination).embeds()
if send_embed is None:
send_embed = await ctx.bot._config.guild(destination.guild).embeds()
else:
send_embed = False

if send_embed is None:
send_embed = await ctx.bot._config.embeds()

if send_embed:
if not is_dm and not destination.permissions_for(destination.guild.me).send_messages:
continue

if not is_dm:
color = await ctx.bot.get_embed_color(destination)
else:
color = ctx.bot._color
if await ctx.bot.embed_requested(destination, command=ctx.command):
color = await ctx.bot.get_embed_color(destination)

e = discord.Embed(colour=color, description=message)
if author.avatar_url:
Expand Down
4 changes: 2 additions & 2 deletions redbot/core/modlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ async def edit(self, data: dict):
if not self.message:
return
try:
use_embed = await self.bot.embed_requested(self.message.channel, self.guild.me)
use_embed = await self.bot.embed_requested(self.message.channel)
case_content = await self.message_content(use_embed)
if use_embed:
await self.message.edit(embed=case_content)
Expand Down Expand Up @@ -993,7 +993,7 @@ async def create_case(
bot.dispatch("modlog_case_create", case)
try:
mod_channel = await get_modlog_channel(case.guild)
use_embeds = await case.bot.embed_requested(mod_channel, case.guild.me)
use_embeds = await case.bot.embed_requested(mod_channel)
case_content = await case.message_content(use_embeds)
if use_embeds:
msg = await mod_channel.send(embed=case_content)
Expand Down

0 comments on commit 0f299ae

Please sign in to comment.