Skip to content

Commit 22942b7

Browse files
committed
feat: enhance accept solution functionality in forum threads with confirmation and logging
1 parent 7edc4a1 commit 22942b7

File tree

1 file changed

+79
-4
lines changed

1 file changed

+79
-4
lines changed

src/cogs/forum/auto_tagging.py

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from logging import Logger
2+
13
from discord import (
24
Forbidden,
5+
ForumChannel,
36
Guild,
47
HTTPException,
58
Interaction,
@@ -14,6 +17,8 @@
1417

1518
from src.data.admin.config_auto import Config
1619
from src.data.forum.post_assist import PostAssistDB
20+
from src.ui.views.dev_help import DevHelpTagDB, DevHelpViewsDB, PersistentSolverView
21+
from src.ui.views.mark_as_solution import MarkAsSolution
1722
from src.ui.views.post_assist import (
1823
ConfigurationPagination,
1924
ConfigurePostAssist,
@@ -40,7 +45,10 @@ def _getter(guild: Guild, entry: dict) -> Member | Role:
4045
class ForumAssist(GroupCog):
4146
def __init__(self, bot: Bot):
4247
self.bot = bot
48+
self.logger: Logger = self.bot.logger # type: ignore
4349
self.db = PostAssistDB(self.bot.pool) # type: ignore
50+
self.dev_help_tag_db = DevHelpTagDB(self.bot.pool) # type: ignore
51+
self.dev_help_views_db = DevHelpViewsDB(self.bot.pool) # type: ignore
4452
self.config = Config(self.bot.pool) # type: ignore
4553
ctx_menu = ContextMenu(
4654
name="Accept Solution",
@@ -127,6 +135,7 @@ async def create_new(self, interaction: Interaction):
127135
reply = view.state.custom_msg
128136
tags = view.state.tag_list
129137
tag_message = view.state.tag_message
138+
enable_accept_solutions = view.state.enable_accept_solutions
130139

131140
if await self.db.config_by_forum(forum):
132141
return await interaction.followup.send(
@@ -147,6 +156,7 @@ async def create_new(self, interaction: Interaction):
147156
entities=tags,
148157
entity_tag_message=tag_message,
149158
reply=reply,
159+
enable_accept_solutions=enable_accept_solutions,
150160
)
151161
return
152162

@@ -269,15 +279,80 @@ async def cog_unload(self) -> None:
269279
) # remove it on unload
270280

271281
async def accept_solution(self, interaction: Interaction, message: Message) -> None:
272-
is_thread = isinstance(message.channel, Thread)
273-
274-
if not is_thread:
282+
thread = interaction.channel
283+
is_thread = isinstance(thread, Thread)
284+
thread_id = thread.id if is_thread else None
285+
message_id = message.id
286+
forum = thread.parent if is_thread else None
287+
staff_roles: list[int] = self.bot.config.guild.staff_roles # type: ignore
288+
user_id = interaction.user.id
289+
290+
if (
291+
not is_thread
292+
or not thread_id
293+
or not forum
294+
or not isinstance(forum, ForumChannel)
295+
):
275296
return await interaction.response.send_message(
276297
"This command can only be used in threads.", ephemeral=True
277298
)
278299

300+
if interaction.user != message.author:
301+
return await interaction.response.send_message(
302+
"Only the thread author can mark solutions.", ephemeral=True
303+
)
304+
305+
if message_id == thread_id:
306+
return await interaction.response.send_message(
307+
"Cannot mark the original post as a solution.", ephemeral=True
308+
)
309+
310+
mark_as_solution_config = await self.db.is_mark_as_solution_enabled(forum.id)
311+
post_assist_config_id = mark_as_solution_config[0]
312+
is_enabled = mark_as_solution_config[1]
313+
314+
if not is_enabled:
315+
return await interaction.response.send_message(
316+
"Accept Solution is not enabled here.", ephemeral=True
317+
)
318+
319+
mark_as_solution_view = MarkAsSolution(thread=thread, message=message)
320+
await interaction.response.send_message(
321+
"Are you sure you want to mark this as a solution?",
322+
view=mark_as_solution_view,
323+
ephemeral=True,
324+
)
325+
await mark_as_solution_view.wait()
326+
327+
if not mark_as_solution_view.confirmed:
328+
return
329+
330+
current_solution = await self.db.get_accepted_solution(thread_id)
331+
if current_solution:
332+
old_message = await thread.fetch_message(current_solution.message_id)
333+
await old_message.remove_reaction("✅", interaction.user)
334+
await old_message.unpin()
335+
279336
await message.add_reaction("✅")
280-
await interaction.response.send_message("HELLO", ephemeral=True)
337+
await message.pin()
338+
await self.db.mark_as_solution(
339+
post_assist_config_id, thread_id, message.id, user_id
340+
)
341+
342+
mark_as_solved_button = PersistentSolverView(
343+
thread.id,
344+
thread.owner_id,
345+
self.dev_help_views_db,
346+
self.dev_help_tag_db,
347+
forum,
348+
staff_roles,
349+
self.logger,
350+
)
351+
352+
await interaction.followup.send(
353+
f"{interaction.user.mention} marked this as a solution.",
354+
view=mark_as_solved_button,
355+
)
281356

282357

283358
async def setup(bot: Bot):

0 commit comments

Comments
 (0)