mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Support all Messageables in bot.embed_requested() (#5576)
* 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
This commit is contained in:
parent
f763d29fd4
commit
0f299ae195
@ -543,8 +543,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass):
|
|||||||
if not reason:
|
if not reason:
|
||||||
reason = _("No reason provided.")
|
reason = _("No reason provided.")
|
||||||
|
|
||||||
# okay, this is some poor API to require PrivateChannel here...
|
if await self.bot.embed_requested(user):
|
||||||
if await self.bot.embed_requested(await user.create_dm(), user):
|
|
||||||
em = discord.Embed(
|
em = discord.Embed(
|
||||||
title=title,
|
title=title,
|
||||||
description=reason,
|
description=reason,
|
||||||
|
|||||||
@ -193,7 +193,6 @@ class Reports(commands.Cog):
|
|||||||
return guild
|
return guild
|
||||||
|
|
||||||
async def send_report(self, ctx: commands.Context, msg: discord.Message, guild: discord.Guild):
|
async def send_report(self, ctx: commands.Context, msg: discord.Message, guild: discord.Guild):
|
||||||
|
|
||||||
author = guild.get_member(msg.author.id)
|
author = guild.get_member(msg.author.id)
|
||||||
report = msg.clean_content
|
report = msg.clean_content
|
||||||
|
|
||||||
@ -207,7 +206,7 @@ class Reports(commands.Cog):
|
|||||||
ticket_number = await self.config.guild(guild).next_ticket()
|
ticket_number = await self.config.guild(guild).next_ticket()
|
||||||
await self.config.guild(guild).next_ticket.set(ticket_number + 1)
|
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 = discord.Embed(description=report, colour=await ctx.embed_colour())
|
||||||
em.set_author(
|
em.set_author(
|
||||||
name=_("Report from {author}{maybe_nick}").format(
|
name=_("Report from {author}{maybe_nick}").format(
|
||||||
|
|||||||
@ -1205,26 +1205,24 @@ class Red(
|
|||||||
|
|
||||||
async def embed_requested(
|
async def embed_requested(
|
||||||
self,
|
self,
|
||||||
channel: Union[discord.abc.GuildChannel, discord.abc.PrivateChannel],
|
channel: discord.abc.Messageable,
|
||||||
user: discord.abc.User,
|
|
||||||
command: Optional[commands.Command] = None,
|
|
||||||
*,
|
*,
|
||||||
check_permissions: bool = False,
|
command: Optional[commands.Command] = None,
|
||||||
|
check_permissions: bool = True,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Determine if an embed is requested for a response.
|
Determine if an embed is requested for a response.
|
||||||
|
|
||||||
Arguments
|
Arguments
|
||||||
---------
|
---------
|
||||||
channel : `discord.abc.GuildChannel` or `discord.abc.PrivateChannel`
|
channel : `discord.abc.Messageable`
|
||||||
The channel to check embed settings for.
|
The target messageable object 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.
|
|
||||||
|
|
||||||
Keyword Arguments
|
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`
|
check_permissions : `bool`
|
||||||
If ``True``, this method will also check whether the bot can send embeds
|
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
|
in the given channel and if it can't, it will return ``False`` regardless of
|
||||||
@ -1242,10 +1240,12 @@ class Red(
|
|||||||
scope = self._config.custom(COMMAND_SCOPE, command.qualified_name, guild_id)
|
scope = self._config.custom(COMMAND_SCOPE, command.qualified_name, guild_id)
|
||||||
return await scope.embeds()
|
return await scope.embeds()
|
||||||
|
|
||||||
if isinstance(channel, discord.abc.PrivateChannel):
|
# using dpy_commands.Context to keep the Messageable contract in full
|
||||||
if (user_setting := await self._config.user(user).embeds()) is not None:
|
if isinstance(channel, dpy_commands.Context):
|
||||||
return user_setting
|
command = command or channel.command
|
||||||
else:
|
channel = channel.channel
|
||||||
|
|
||||||
|
if isinstance(channel, discord.TextChannel):
|
||||||
if check_permissions and not channel.permissions_for(channel.guild.me).embed_links:
|
if check_permissions and not channel.permissions_for(channel.guild.me).embed_links:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -1257,6 +1257,13 @@ class Red(
|
|||||||
|
|
||||||
if (guild_setting := await self._config.guild(channel.guild).embeds()) is not None:
|
if (guild_setting := await self._config.guild(channel.guild).embeds()) is not None:
|
||||||
return guild_setting
|
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?
|
# XXX: maybe this should be checked before guild setting?
|
||||||
if (global_command_setting := await get_command_setting(0)) is not None:
|
if (global_command_setting := await get_command_setting(0)) is not None:
|
||||||
|
|||||||
@ -231,17 +231,20 @@ class Context(DPYContext):
|
|||||||
|
|
||||||
async def embed_requested(self):
|
async def embed_requested(self):
|
||||||
"""
|
"""
|
||||||
Simple helper to call bot.embed_requested
|
Short-hand for calling bot.embed_requested with permission checks.
|
||||||
with logic around if embed permissions are available
|
|
||||||
|
Equivalent to:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
await ctx.bot.embed_requested(ctx)
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
bool:
|
bool:
|
||||||
:code:`True` if an embed is requested
|
:code:`True` if an embed is requested
|
||||||
"""
|
"""
|
||||||
return await self.bot.embed_requested(
|
return await self.bot.embed_requested(self)
|
||||||
self.channel, self.author, command=self.command, check_permissions=True
|
|
||||||
)
|
|
||||||
|
|
||||||
async def maybe_send_embed(self, message: str) -> discord.Message:
|
async def maybe_send_embed(self, message: str) -> discord.Message:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -721,9 +721,7 @@ class RedHelpFormatter(HelpFormatterABC):
|
|||||||
yield obj
|
yield obj
|
||||||
|
|
||||||
async def embed_requested(self, ctx: Context) -> bool:
|
async def embed_requested(self, ctx: Context) -> bool:
|
||||||
return await ctx.bot.embed_requested(
|
return await ctx.bot.embed_requested(channel=ctx.channel, command=red_help)
|
||||||
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):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -3975,31 +3975,12 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
|
|||||||
successful = False
|
successful = False
|
||||||
|
|
||||||
for destination in destinations:
|
for destination in destinations:
|
||||||
|
|
||||||
is_dm = isinstance(destination, discord.User)
|
is_dm = isinstance(destination, discord.User)
|
||||||
send_embed = None
|
if not is_dm and not destination.permissions_for(destination.guild.me).send_messages:
|
||||||
|
|
||||||
if is_dm:
|
|
||||||
send_embed = await ctx.bot._config.user(destination).embeds()
|
|
||||||
else:
|
|
||||||
if not destination.permissions_for(destination.guild.me).send_messages:
|
|
||||||
continue
|
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:
|
if await ctx.bot.embed_requested(destination, command=ctx.command):
|
||||||
send_embed = await ctx.bot._config.embeds()
|
|
||||||
|
|
||||||
if send_embed:
|
|
||||||
|
|
||||||
if not is_dm:
|
|
||||||
color = await ctx.bot.get_embed_color(destination)
|
color = await ctx.bot.get_embed_color(destination)
|
||||||
else:
|
|
||||||
color = ctx.bot._color
|
|
||||||
|
|
||||||
e = discord.Embed(colour=color, description=message)
|
e = discord.Embed(colour=color, description=message)
|
||||||
if author.avatar_url:
|
if author.avatar_url:
|
||||||
|
|||||||
@ -375,7 +375,7 @@ class Case:
|
|||||||
if not self.message:
|
if not self.message:
|
||||||
return
|
return
|
||||||
try:
|
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)
|
case_content = await self.message_content(use_embed)
|
||||||
if use_embed:
|
if use_embed:
|
||||||
await self.message.edit(embed=case_content)
|
await self.message.edit(embed=case_content)
|
||||||
@ -993,7 +993,7 @@ async def create_case(
|
|||||||
bot.dispatch("modlog_case_create", case)
|
bot.dispatch("modlog_case_create", case)
|
||||||
try:
|
try:
|
||||||
mod_channel = await get_modlog_channel(case.guild)
|
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)
|
case_content = await case.message_content(use_embeds)
|
||||||
if use_embeds:
|
if use_embeds:
|
||||||
msg = await mod_channel.send(embed=case_content)
|
msg = await mod_channel.send(embed=case_content)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user