feat(bot):
This commit is contained in:
+183
-2
@@ -1,8 +1,13 @@
|
||||
import discord
|
||||
from discord.ext import pages
|
||||
from discord.utils import find
|
||||
|
||||
from tortoise import transactions
|
||||
|
||||
from payphone import bot
|
||||
from payphone.db import models
|
||||
from payphone.cogs.mods import modals
|
||||
from payphone.core import constants
|
||||
from payphone.cogs.mods import modals, views, embeds
|
||||
|
||||
|
||||
class BanNotificationView(discord.ui.View):
|
||||
@@ -19,7 +24,7 @@ class BanNotificationView(discord.ui.View):
|
||||
ban (models.Ban): The ban to appeal.
|
||||
"""
|
||||
|
||||
super().__init__()
|
||||
super().__init__(timeout=None)
|
||||
|
||||
self.bot = bot
|
||||
|
||||
@@ -45,3 +50,179 @@ class BanNotificationView(discord.ui.View):
|
||||
"""Sends the ban appeal modal."""
|
||||
|
||||
await interaction.response.send_modal(modals.BanNotificationModal(self.bot))
|
||||
|
||||
|
||||
class ReportView(discord.ui.View):
|
||||
"""The view sent together with the report embed."""
|
||||
|
||||
def __init__(self, bot: bot.Payphone, report: models.Report) -> None:
|
||||
"""Initializes the view.
|
||||
|
||||
Args:
|
||||
bot (bot.Payphone): The bot.
|
||||
report (models.Report): The report.
|
||||
"""
|
||||
|
||||
super().__init__(timeout=300, disable_on_timeout=True)
|
||||
|
||||
self.bot = bot
|
||||
self.report = report
|
||||
|
||||
option_labels = set(
|
||||
f"Ban {rs.profile.name} [ID: {rs.user_id}]" for rs in report.call.call_rss
|
||||
)
|
||||
|
||||
actions = discord.ui.Select(
|
||||
placeholder="Actions",
|
||||
options=[discord.SelectOption(label=label) for label in option_labels],
|
||||
)
|
||||
|
||||
async def actions_callback(interaction: discord.Interaction) -> None:
|
||||
# interaction.data = {'values': ['Ban Nyeki (JPP)'], 'custom_id': 'staff_call_report:actions', 'component_type': 3}
|
||||
value = interaction.data["values"][0]
|
||||
|
||||
user_id = int(value.rsplit("[ID: ", 1)[1][:-1])
|
||||
|
||||
await interaction.response.send_modal(
|
||||
modals.BanUserModal(
|
||||
bot,
|
||||
find(
|
||||
lambda rs: rs.user_id == user_id, report.call.call_rss
|
||||
).profile,
|
||||
report.id,
|
||||
)
|
||||
)
|
||||
|
||||
actions.callback = actions_callback
|
||||
|
||||
self.add_item(actions)
|
||||
|
||||
@discord.ui.button(label="Message Logs", style=discord.ButtonStyle.gray, row=2)
|
||||
async def message_logs(
|
||||
self, _button: discord.ui.Button, interaction: discord.Interaction
|
||||
) -> None:
|
||||
report = self.report
|
||||
|
||||
messages = (
|
||||
await models.Message.filter(call=report.call)
|
||||
.order_by("id")
|
||||
.prefetch_related("contents")
|
||||
)
|
||||
|
||||
logs = []
|
||||
|
||||
reporter_channel_id = find(
|
||||
lambda rs: rs.user_id == report.reported_by_id, report.call.call_rss
|
||||
).channel_id
|
||||
|
||||
profiles_mapping = {rs.profile_id: rs.profile for rs in report.call.call_rss}
|
||||
|
||||
last_user_id = None
|
||||
last_channel_id = None
|
||||
|
||||
current_block = ""
|
||||
|
||||
for message in messages:
|
||||
is_new_block = (
|
||||
message.user_id != last_user_id or last_channel_id != message.channel_id
|
||||
)
|
||||
|
||||
if is_new_block:
|
||||
logs.append(current_block)
|
||||
current_block = ""
|
||||
|
||||
current_block += f"{"+" if message.channel_id == reporter_channel_id else "-"} {profiles_mapping[message.profile_id].name}:"
|
||||
|
||||
for content in message.contents:
|
||||
current_block += (
|
||||
f"\n{"!" if not content.was_propagated else " "} {content.content}"
|
||||
)
|
||||
|
||||
last_user_id = message.user_id
|
||||
last_channel_id = message.channel_id
|
||||
|
||||
log_pages = []
|
||||
|
||||
for current_block in logs:
|
||||
if len(log_pages) > 0 and len(log_pages[-1]) + len(current_block) < 1000:
|
||||
log_pages[-1] += "\n\n" + current_block.replace(
|
||||
"```",
|
||||
f"`{constants.INVISIBLE_CHARACTER}`{constants.INVISIBLE_CHARACTER}`",
|
||||
)
|
||||
|
||||
else:
|
||||
log_pages.append(
|
||||
current_block.replace(
|
||||
"```",
|
||||
f"`{constants.INVISIBLE_CHARACTER}`{constants.INVISIBLE_CHARACTER}`",
|
||||
)
|
||||
)
|
||||
|
||||
await pages.Paginator(
|
||||
[f"```diff\n{page}\n```" for page in log_pages],
|
||||
timeout=300,
|
||||
).respond(interaction, ephemeral=True)
|
||||
|
||||
@discord.ui.button(label="Accept", style=discord.ButtonStyle.green, row=2)
|
||||
@transactions.atomic()
|
||||
async def accept(
|
||||
self, _button: discord.ui.Button, interaction: discord.Interaction
|
||||
) -> None:
|
||||
report = self.report
|
||||
|
||||
report.status = models.ReportStatus.ACCEPTED
|
||||
await report.save()
|
||||
|
||||
report = (
|
||||
await models.Report.filter(status=models.ReportStatus.ON_REVIEW)
|
||||
.order_by("id")
|
||||
.limit(1)
|
||||
.first()
|
||||
.select_related("call")
|
||||
.prefetch_related("call__call_rss", "call__call_rss__profile")
|
||||
)
|
||||
|
||||
if report is None:
|
||||
await interaction.response.edit_message(
|
||||
content="✅ You're up to date! No reports left to check.",
|
||||
embed=None,
|
||||
view=None
|
||||
)
|
||||
return
|
||||
|
||||
await interaction.response.edit_message(
|
||||
embed=embeds.ReportEmbed(self.bot, report),
|
||||
view=views.ReportView(self.bot, report),
|
||||
)
|
||||
|
||||
@discord.ui.button(label="Decline", style=discord.ButtonStyle.red, row=2)
|
||||
@transactions.atomic()
|
||||
async def decline(
|
||||
self, _button: discord.ui.Button, interaction: discord.Interaction
|
||||
) -> None:
|
||||
report = self.report
|
||||
|
||||
report.status = models.ReportStatus.DECLINED
|
||||
await report.save()
|
||||
|
||||
report = (
|
||||
await models.Report.filter(status=models.ReportStatus.ON_REVIEW)
|
||||
.order_by("id")
|
||||
.limit(1)
|
||||
.first()
|
||||
.select_related("call")
|
||||
.prefetch_related("call__call_rss", "call__call_rss__profile")
|
||||
)
|
||||
|
||||
if report is None:
|
||||
await interaction.response.edit_message(
|
||||
content="✅ You're up to date! No reports left to check.",
|
||||
embed=None,
|
||||
view=None
|
||||
)
|
||||
return
|
||||
|
||||
await interaction.response.edit_message(
|
||||
embed=embeds.ReportEmbed(self.bot, report),
|
||||
view=views.ReportView(self.bot, report),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user