Skip to content

Commit

Permalink
refactor report cog
Browse files Browse the repository at this point in the history
  • Loading branch information
solumath committed Mar 23, 2024
1 parent 1ec335a commit b6572ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
15 changes: 10 additions & 5 deletions cogs/report/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def extract_report_id(inter: disnake.MessageInteraction) -> int:
return int(report_id)


async def convert_url(inter, message_url) -> Optional[disnake.Message]:
async def convert_url(
inter: disnake.ApplicationCommandInteraction, message_url: str
) -> Optional[disnake.Message]:
"""converts a message url to a message object"""
try:
message: disnake.Message = await commands.MessageConverter().convert(inter, message_url)
Expand All @@ -34,15 +36,18 @@ async def set_tag(forum: disnake.ForumChannel, forum_thread: disnake.Thread, tag
break


async def embed_resolved(self, author: str, embed: dict, report_type: str, resolved: bool) -> disnake.Embed:
async def embed_resolved(
children: list[disnake.ui.Button], author: str, embed: dict, report_type: str, resolved: bool
) -> disnake.Embed:
"""Changes the embed to a resolved embed or back to a report embed"""
print(children)
if resolved:
embed["color"] = disnake.Color.green()
embed["title"] = "Resolved"
for field in embed["fields"]:
if field["name"] == "Resolved by":
field["value"] = author
for child in self.children:
for child in children:
child.disabled = True
if child.custom_id == "report:resolve":
child.label = "Resolved"
Expand All @@ -55,7 +60,7 @@ async def embed_resolved(self, author: str, embed: dict, report_type: str, resol
for field in embed["fields"]:
if field["name"] == "Resolved by":
field["value"] = "---"
for child in self.children:
for child in children:
child.disabled = False
if child.custom_id == "report:resolve":
child.label = "Resolve"
Expand All @@ -65,7 +70,7 @@ async def embed_resolved(self, author: str, embed: dict, report_type: str, resol
return embed


def answer_embed(title, inter: disnake.ModalInteraction, report: ReportDB, answer: str) -> disnake.Embed:
def answer_embed(title: str, inter: disnake.ModalInteraction, report: ReportDB, answer: str) -> disnake.Embed:
"""creates an embed template for the submitted answer"""
description = MessagesCZ.embed_answered(last_answer=report.last_answer, answer=answer)
embed = disnake.Embed(title=title, description=description, color=disnake.Color.yellow())
Expand Down
12 changes: 6 additions & 6 deletions cogs/report/modals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import utils
from cogs.report.views import ReportGeneralView, ReportMessageView
from config.app_config import config
from config.messages import Messages
from database.report import ReportDB, UserDB

from . import features as report_features
from .messages_cz import MessagesCZ


class Modal(disnake.ui.Modal):
Expand All @@ -27,7 +27,7 @@ def __init__(
components = [
disnake.ui.TextInput(
label="Report reason",
placeholder=Messages.report_modal_placeholder,
placeholder=MessagesCZ.modal_placeholder,
custom_id="reason",
style=disnake.TextInputStyle.long,
required=True,
Expand Down Expand Up @@ -83,7 +83,7 @@ async def edit_or_send(self, inter: disnake.ModalInteraction, embed: disnake.Emb

async def report_general(self, inter: disnake.ModalInteraction) -> None:
"""add general report to db and send it to the report room"""
await inter.send(Messages.report_modal_success, ephemeral=True)
await inter.send(MessagesCZ.modal_success, ephemeral=True)
report_reason = inter.text_values["reason"]
UserDB.add_user(inter.author.id)
report_id = ReportDB.add_report(type="general", author_id=inter.author.id, reason=report_reason)
Expand All @@ -102,8 +102,8 @@ async def report_general(self, inter: disnake.ModalInteraction) -> None:

async def report_message(self, inter: disnake.ModalInteraction) -> None:
"""add message report to db and send it to the report room"""
await inter.send(Messages.report_modal_success, ephemeral=True)
report_reason = Messages.report_message_embed(
await inter.send(MessagesCZ.modal_success, ephemeral=True)
report_reason = MessagesCZ.message_embed(
content=self.message.content, reason=inter.text_values["reason"]
)
UserDB.add_user(inter.author.id)
Expand Down Expand Up @@ -140,7 +140,7 @@ async def report_message(self, inter: disnake.ModalInteraction) -> None:
content = ""

if attachments_too_big:
content = Messages.report_files_too_big(files="\n- ".join(attachments_too_big))
content = MessagesCZ.attachment_too_big(files="\n- ".join(attachments_too_big))

await thread.send(content=content, files=files)

Expand Down
16 changes: 10 additions & 6 deletions cogs/report/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ async def set_view_resolved(self, embed: dict, author_id: int, report_id: int) -
report = ReportDB.get_report(report_id)

if report.resolved:
embed = await report_features.embed_resolved(self, "Anonym", embed, report.type, False)
embed = await report_features.embed_resolved(self.children, "Anonym", embed, report.type, False)
report.set_resolved(report_id, author_id, False)
else:
embed = await report_features.embed_resolved(self, "Anonym", embed, report.type, True)
embed = await report_features.embed_resolved(self.children, "Anonym", embed, report.type, True)
report.set_resolved(report_id, author_id, True)
return embed

Expand All @@ -55,7 +55,7 @@ async def set_spam(
for child in self.children:
if child.custom_id == "report:resolve":
embed = await report_features.embed_resolved(
self, resolved_author, embed, report.type, False
self.children, resolved_author, embed, report.type, False
)
button.label = "Mark spam"
button.style = disnake.ButtonStyle.red
Expand All @@ -68,7 +68,9 @@ async def set_spam(
ReportDB.set_fake_report(report.id, inter.author.id, True)
for child in self.children:
if child.custom_id == "report:resolve":
embed = await report_features.embed_resolved(self, resolved_author, embed, "Spam", True)
embed = await report_features.embed_resolved(
self.children, resolved_author, embed, "Spam", True
)
child.disabled = True
button.disabled = False
button.label = f"Spam marked by @{inter.author.name}"
Expand All @@ -91,7 +93,9 @@ async def resolve(self, button: disnake.ui.Button, inter: disnake.MessageInterac
resolved_by = f"{inter.author.mention} `@{inter.author.name}`"

if report.resolved:
embed = await report_features.embed_resolved(self, resolved_by, embed, report.type, False)
embed = await report_features.embed_resolved(
self.children, resolved_by, embed, report.type, False
)
report.set_resolved(report_id, inter.author.id, False)
content = MessagesCZ.report_unresolved(
id=report_id, author=inter.author.mention, author_name=inter.author.name
Expand All @@ -100,7 +104,7 @@ async def resolve(self, button: disnake.ui.Button, inter: disnake.MessageInterac
await report_author.send(content)
await inter.message.channel.send(content, allowed_mentions=disnake.AllowedMentions.none())
else:
embed = await report_features.embed_resolved(self, resolved_by, embed, report.type, True)
embed = await report_features.embed_resolved(self.children, resolved_by, embed, report.type, True)
report.set_resolved(report_id, inter.author.id, True)
await report_features.set_tag(self.report_channel, inter.message.channel, "resolved")

Expand Down

0 comments on commit b6572ae

Please sign in to comment.