Lots more event debugging

This commit is contained in:
Zeva Rose 2022-03-26 21:32:24 -06:00
parent 002bf5b150
commit 029743f977

View file

@ -102,6 +102,7 @@ class Jarvis(Snake):
self, ctx: Context, error: Exception, *args: list, **kwargs: dict
) -> None:
"""Lepton on_command_error override."""
self.logger.debug(f"Handling error in {ctx.invoked_name}: {error}")
if isinstance(error, CommandOnCooldown):
await ctx.send(str(error), ephemeral=True)
return
@ -191,7 +192,8 @@ class Jarvis(Snake):
guild = user.guild
unverified = await Setting.find_one(q(guild=guild.id, setting="unverified"))
if unverified:
role = guild.get_role(unverified.value)
self.logger.debug(f"Applying unverified role to {user.id} in {guild.id}")
role = await guild.fetch_role(unverified.value)
if role not in user.roles:
await user.add_role(role, reason="User just joined and is unverified")
@ -199,6 +201,9 @@ class Jarvis(Snake):
"""Handle autopurge events."""
autopurge = await Autopurge.find_one(q(guild=message.guild.id, channel=message.channel.id))
if autopurge:
self.logger.debug(
f"Autopurging message {message.guild.id}/{message.channel.id}/{message.id}"
)
await message.delete(delay=autopurge.delay)
async def autoreact(self, message: Message) -> None:
@ -210,6 +215,9 @@ class Jarvis(Snake):
)
)
if autoreact:
self.logger.debug(
f"Autoreacting to message {message.guild.id}/{message.channel.id}/{message.id}"
)
for reaction in autoreact.reactions:
await message.add_reaction(reaction)
if autoreact.thread:
@ -240,17 +248,17 @@ class Jarvis(Snake):
"VtgZntXcnZ",
"gPfYGbvTCE",
]
if match.group(1) not in allowed and setting.value:
if (m := match.group(1)) not in allowed and setting.value:
self.logger.debug(f"Removing non-allowed invite {m} from {message.guild.id}")
await message.delete()
w = Warning(
await Warning(
active=True,
admin=self.user.id,
duration=24,
guild=message.guild.id,
reason="Sent an invite link",
user=message.author.id,
)
await w.commit()
).commit()
embed = warning_embed(message.author, "Sent an invite link")
await message.channel.send(embed=embed)
@ -270,15 +278,17 @@ class Jarvis(Snake):
- (1 if message.author.id in message._mention_ids else 0) # noqa: W503
> massmention.value # noqa: W503
):
w = Warning(
self.logger.debug(
f"Massmention threshold on {message.guild.id}/{message.channel.id}/{message.id}"
)
await Warning(
active=True,
admin=self.user.id,
duration=24,
guild=message.guild.id,
reason="Mass Mention",
user=message.author.id,
)
await w.commit()
).commit()
embed = warning_embed(message.author, "Mass Mention")
await message.channel.send(embed=embed)
@ -322,31 +332,35 @@ class Jarvis(Snake):
break
if role_in_rolepings and user_missing_role and not user_is_admin and not user_has_bypass:
w = Warning(
self.logger.debug(
f"Rolepinged role in {message.guild.id}/{message.channel.id}/{message.id}"
)
await Warning(
active=True,
admin=self.user.id,
duration=24,
guild=message.guild.id,
reason="Pinged a blocked role/user with a blocked role",
user=message.author.id,
)
await w.commit()
).commit()
embed = warning_embed(message.author, "Pinged a blocked role/user with a blocked role")
await message.channel.send(embed=embed)
async def phishing(self, message: Message) -> None:
"""Check if the message contains any known phishing domains."""
for match in url.finditer(message.content):
if match.group("domain") in self.phishing_domains:
w = Warning(
if (m := match.group("domain")) in self.phishing_domains:
self.logger.debug(
f"Phishing url {m} detected in {message.guild.id}/{message.channel.id}/{message.id}"
)
await Warning(
active=True,
admin=self.user.id,
duration=24,
guild=message.guild.id,
reason="Phishing URL",
user=message.author.id,
)
await w.commit()
).commit()
embed = warning_embed(message.author, "Phishing URL")
await message.channel.send(embed=embed)
await message.delete()
@ -365,15 +379,17 @@ class Jarvis(Snake):
data = await resp.json()
for item in data["processed"]["urls"].values():
if not item["safe"]:
w = Warning(
self.logger.debug(
f"Phishing url {match.string} detected in {message.guild.id}/{message.channel.id}/{message.id}"
)
await Warning(
active=True,
admin=self.user.id,
duration=24,
guild=message.guild.id,
reason="Unsafe URL",
user=message.author.id,
)
await w.commit()
).commit()
reasons = ", ".join(item["not_safe_reasons"])
embed = warning_embed(message.author, reasons)
await message.channel.send(embed=embed)
@ -404,36 +420,41 @@ class Jarvis(Snake):
if modlog:
if not before or before.content == after.content or before.content is None:
return
channel = before.guild.get_channel(modlog.value)
fields = [
EmbedField(
"Original Message",
before.content if before.content else "N/A",
False,
),
EmbedField(
"New Message",
after.content if after.content else "N/A",
False,
),
]
embed = build_embed(
title="Message Edited",
description=f"{after.author.mention} edited a message",
fields=fields,
color="#fc9e3f",
timestamp=after.edited_timestamp,
url=after.jump_url,
)
embed.set_author(
name=after.author.username,
icon_url=after.author.display_avatar.url,
url=after.jump_url,
)
embed.set_footer(
text=f"{after.author.username}#{after.author.discriminator} | {after.author.id}"
)
await channel.send(embed=embed)
try:
channel = before.guild.get_channel(modlog.value)
fields = [
EmbedField(
"Original Message",
before.content if before.content else "N/A",
False,
),
EmbedField(
"New Message",
after.content if after.content else "N/A",
False,
),
]
embed = build_embed(
title="Message Edited",
description=f"{after.author.mention} edited a message",
fields=fields,
color="#fc9e3f",
timestamp=after.edited_timestamp,
url=after.jump_url,
)
embed.set_author(
name=after.author.username,
icon_url=after.author.display_avatar.url,
url=after.jump_url,
)
embed.set_footer(
text=f"{after.author.username}#{after.author.discriminator} | {after.author.id}"
)
await channel.send(embed=embed)
except Exception as e:
self.logger.warn(
f"Failed to process edit {before.guild.id}/{before.channel.id}/{before.id}: {e}"
)
if not isinstance(after.channel, DMChannel) and not after.author.bot:
await self.massmention(after)
await self.roleping(after)
@ -455,53 +476,58 @@ class Jarvis(Snake):
content = "N/A"
fields = [EmbedField("Original Message", content, False)]
if message.attachments:
value = "\n".join([f"[{x.filename}]({x.url})" for x in message.attachments])
fields.append(
EmbedField(
name="Attachments",
value=value,
inline=False,
try:
if message.attachments:
value = "\n".join([f"[{x.filename}]({x.url})" for x in message.attachments])
fields.append(
EmbedField(
name="Attachments",
value=value,
inline=False,
)
)
if message.sticker_items:
value = "\n".join([f"Sticker: {x.name}" for x in message.sticker_items])
fields.append(
EmbedField(
name="Stickers",
value=value,
inline=False,
)
)
if message.embeds:
value = str(len(message.embeds)) + " embeds"
fields.append(
EmbedField(
name="Embeds",
value=value,
inline=False,
)
)
channel = message.guild.get_channel(modlog.value)
embed = build_embed(
title="Message Deleted",
description=f"{message.author.mention}'s message was deleted",
fields=fields,
color="#fc9e3f",
)
embed.set_author(
name=message.author.username,
icon_url=message.author.display_avatar.url,
url=message.jump_url,
)
embed.set_footer(
text=(
f"{message.author.username}#{message.author.discriminator} | "
f"{message.author.id}"
)
)
if message.sticker_items:
value = "\n".join([f"Sticker: {x.name}" for x in message.sticker_items])
fields.append(
EmbedField(
name="Stickers",
value=value,
inline=False,
)
await channel.send(embed=embed)
except Exception as e:
self.logger.warn(
f"Failed to process edit {message.guild.id}/{message.channel.id}/{message.id}: {e}"
)
if message.embeds:
value = str(len(message.embeds)) + " embeds"
fields.append(
EmbedField(
name="Embeds",
value=value,
inline=False,
)
)
channel = message.guild.get_channel(modlog.value)
embed = build_embed(
title="Message Deleted",
description=f"{message.author.mention}'s message was deleted",
fields=fields,
color="#fc9e3f",
)
embed.set_author(
name=message.author.username,
icon_url=message.author.display_avatar.url,
url=message.jump_url,
)
embed.set_footer(
text=(
f"{message.author.username}#{message.author.discriminator} | "
f"{message.author.id}"
)
)
await channel.send(embed=embed)