Skip to content

Commit

Permalink
[Defender] WD: Fix send-message issue with embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Twentysix26 committed Feb 1, 2022
1 parent 72dd932 commit 9f64510
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
17 changes: 9 additions & 8 deletions defender/core/warden/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,14 +1178,16 @@ async def delete_last_message_sent_after(params: models.IsTimedelta):
@processor(Action.SendMessage)
async def send_message(params: models.SendMessage):
nonlocal last_sent_message
default_values = 0
send_embed = False

for key in params.__fields_set__:
if key not in params._text_only_attrs:
send_embed = True
break

for key in params.dict():
if key == "edit_message_id":
continue
attr = getattr(params, key)
if attr is None:
default_values += 1
if attr is None and key not in params._text_only_attrs:
setattr(params, key, discord.Embed.Empty)
elif isinstance(attr, str):
setattr(params, key, safe_sub(attr))
Expand All @@ -1209,13 +1211,12 @@ async def send_message(params: models.SendMessage):
f"'{params.id}' is not a valid channel name.")

em = None
no_embed = default_values >= 10 # Yuck, maybe I'll think of something better

if no_embed and not params.content:
if send_embed is False and not params.content:
raise ExecutionError(f"[Warden] ({self.name}): I have no content and "
"no embed to send.")

if no_embed is False:
if send_embed:
em = discord.Embed(title=params.title,
description=params.description,
url=params.url)
Expand Down
4 changes: 4 additions & 0 deletions defender/core/warden/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ async def _runtime_check(self, *, cog, author: discord.Member, action_or_cond: U

class SendMessage(BaseModel):
_short_form = ("id", "content")
# Used internally to determine whether an embed has to be sent
# If any key other than these ones is passed an embed will be sent
_text_only_attrs = ("id", "content", "allow_mass_mentions", "ping_on_reply",
"reply_message_id", "edit_message_id", "add_timestamp")
id: str # or context variable
content: Optional[str]=""
description: Optional[str]=None
Expand Down
2 changes: 1 addition & 1 deletion defender/defender.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
class Defender(Commands, AutoModules, Events, commands.Cog, metaclass=CompositeMetaClass):
"""Security tools to protect communities"""

__version__ = "1.10"
__version__ = "1.10.1"

def __init__(self, bot):
self.bot = bot
Expand Down

0 comments on commit 9f64510

Please sign in to comment.