Skip to content

Commit

Permalink
Paste never fails, paste respected automod
Browse files Browse the repository at this point in the history
  • Loading branch information
ajax146 committed Jul 25, 2024
1 parent e0c2b6b commit 706e676
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
1 change: 1 addition & 0 deletions techsupport_bot/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Functions are commandless cogs"""

from .automod import *
from .nickname import *
5 changes: 4 additions & 1 deletion techsupport_bot/functions/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ async def on_raw_message_edit(
def process_automod_violations(

Check notice on line 320 in techsupport_bot/functions/automod.py

View check run for this annotation

codefactor.io / CodeFactor

techsupport_bot/functions/automod.py#L320

Missing function or method docstring (missing-function-docstring)
all_punishments: list[AutoModPunishment],
) -> AutoModAction:
if len(all_punishments) == 0:
return None

should_delete = False
should_warn = False
mute_duration = 0
Expand Down Expand Up @@ -396,7 +399,7 @@ def generate_automod_alert_embed(
embed.add_field(name="Actions Taken", value=action_taken)
embed.add_field(name="Channel", value=f"{ctx.channel.mention} ({ctx.channel.name})")
embed.add_field(name="User", value=f"{ctx.author.mention} ({ctx.author.name})")
embed.add_field(name="Message", value=ctx.message.content, inline=False)
embed.add_field(name="Message", value=ctx.message.content[:1024], inline=False)
embed.add_field(name="URL", value=ctx.message.jump_url, inline=False)

embed.set_thumbnail(url=ALERT_ICON_URL)
Expand Down
72 changes: 35 additions & 37 deletions techsupport_bot/functions/paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from botlogging import LogContext, LogLevel
from core import cogs, extensionconfig
from discord.ext import commands
from functions import automod

if TYPE_CHECKING:
import bot
Expand Down Expand Up @@ -114,7 +115,12 @@ async def response(
if len(content) > config.extensions.paste.length_limit.value or content.count(
"\n"
) > self.max_newlines(config.extensions.paste.length_limit.value):
await self.handle_length_alert(config, ctx, content)
if "automod" in config.get("enabled_extensions", []):
automod_actions = automod.run_all_checks(config, ctx.message)
automod_final = automod.process_automod_violations(automod_actions)
if automod_final and automod_final.delete_message:
return
await self.paste_message(config, ctx, content)

def max_newlines(self: Self, max_length: int) -> int:
"""Gets a theoretical maximum number of new lines in a given message
Expand Down Expand Up @@ -164,7 +170,7 @@ async def on_raw_message_edit(

await self.response(config, ctx, message.content, None)

async def handle_length_alert(
async def paste_message(
self: Self, config: munch.Munch, ctx: commands.Context, content: str
) -> None:
"""Moves message into a linx paste if it's too long
Expand All @@ -174,6 +180,30 @@ async def handle_length_alert(
ctx (commands.Context): The context where the original message was sent
content (str): The string content of the flagged message
"""
if not self.bot.file_config.api.api_url.linx:
await self.bot.logger.send_log(
message=(
f"Would have pasted message {ctx.message.id} but no linx url has been configured."

Check notice on line 186 in techsupport_bot/functions/paste.py

View check run for this annotation

codefactor.io / CodeFactor

techsupport_bot/functions/paste.py#L186

Line too long (102/100) (line-too-long)
),
level=LogLevel.WARNING,
channel=log_channel,

Check notice on line 189 in techsupport_bot/functions/paste.py

View check run for this annotation

codefactor.io / CodeFactor

techsupport_bot/functions/paste.py#L189

Using variable 'log_channel' before assignment (used-before-assignment)
context=LogContext(guild=ctx.guild, channel=ctx.channel),
)
return

linx_embed = await self.create_linx_embed(config, ctx, content)

if not linx_embed:
await self.bot.logger.send_log(
message=(
f"Would have pasted message {ctx.message.id} but uploading the file to linx failed."

Check notice on line 199 in techsupport_bot/functions/paste.py

View check run for this annotation

codefactor.io / CodeFactor

techsupport_bot/functions/paste.py#L199

Line too long (104/100) (line-too-long)
),
level=LogLevel.WARNING,
channel=log_channel,
context=LogContext(guild=ctx.guild, channel=ctx.channel),
)
return

attachments: list[discord.File] = []
if ctx.message.attachments:
total_attachment_size = 0
Expand All @@ -192,45 +222,13 @@ async def handle_length_alert(
channel=log_channel,
context=LogContext(guild=ctx.guild, channel=ctx.channel),
)
await ctx.message.delete()

reason = "message too long (too many newlines or characters)"

if not self.bot.file_config.api.api_url.linx:
await self.send_default_delete_response(config, ctx, content, reason)
return

linx_embed = await self.create_linx_embed(config, ctx, content)
if not linx_embed:
await self.send_default_delete_response(config, ctx, content, reason)
# await self.send_alert(config, ctx, "Could not convert text to Linx paste")
return

await ctx.send(
message = await ctx.send(
ctx.message.author.mention, embed=linx_embed, files=attachments[:10]
)

async def send_default_delete_response(
self: Self,
config: munch.Munch,
ctx: commands.Context,
content: str,
reason: str,
) -> None:
"""Sends a DM to a user containing a message that was deleted
Args:
config (munch.Munch): The config of the guild where the message was sent
ctx (commands.Context): The context of the deleted message
content (str): The context of the deleted message
reason (str): The reason the message was deleted
"""
embed = discord.Embed(
title="Chat Protection", description=f"Message deleted. Reason: *{reason}*"
)
embed.color = discord.Color.gold()
await ctx.send(ctx.message.author.mention, embed=embed)
await ctx.author.send(f"Deleted message: ```{content[:1994]}```")
if message:
await ctx.message.delete()

async def create_linx_embed(
self: Self, config: munch.Munch, ctx: commands.Context, content: str
Expand Down

0 comments on commit 706e676

Please sign in to comment.