Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions bot/extensions/threads_cog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import traceback
from logging import info
from pytz import timezone
from datetime import datetime

from discord import Embed, Interaction, TextStyle
from discord.app_commands import Choice, autocomplete
Expand Down Expand Up @@ -197,15 +198,23 @@ async def post_thread(self, thread: Thread):
content = f"<@&{role_id}>" if role_id else None

embed = Embed(
color=self.bot.default_color, title=thread.title, description=thread.content
color=self.bot.default_color,
title=thread.title,
description=thread.content,
timestamp=datetime.now()
)

if channel:
message = await channel.send(content=content, embed=embed)
discord_thread = await message.create_thread(name=thread.title)
new_discord_thread = await message.create_thread(name=thread.title)

if old_discord_thread := self.bot.get_channel(
thread.latest_thread_id
):
await old_discord_thread.send(new_discord_thread.jump_url)
await old_discord_thread.edit(archived=True, locked=True)

if thread.daily_reminder:
thread.update(latest_thread_id=discord_thread.id)
thread.update(latest_thread_id=new_discord_thread.id)

@hybrid_group(name="threads", help="Commands to manage threads")
@has_permissions(administrator=True)
Expand Down
36 changes: 25 additions & 11 deletions tests/extensions/test_threads_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,33 +158,47 @@ async def test_daily_reminder_skips_archived_and_locked(threads_cog, mock_bot):


@pytest.mark.asyncio
async def test_post_thread(threads_cog, mock_bot):
"""Test post_thread creates a thread in the specified channel."""
async def test_post_thread_with_old_thread(threads_cog, mock_bot):
"""Test post_thread archives old thread when one exists."""
thread = Thread.create(
title="Test Thread",
content="This is a test thread.",
recurrence=None,
daily_reminder=False,
latest_thread_id=None,
latest_thread_id=987654321, # Old thread ID
)

mock_channel = MagicMock()
mock_channel.send = AsyncMock()
mock_bot.get_channel.return_value = mock_channel

mock_old_thread = MagicMock()
mock_old_thread.send = AsyncMock()
mock_old_thread.edit = AsyncMock()

mock_thread = MagicMock()
mock_thread.id = 123456789
mock_thread.jump_url = "https://discord.com/channels/123/456/789"

def get_channel_side_effect(channel_id):
if channel_id == threads_cog.threads_channel_id:
return mock_channel
elif channel_id == 987654321:
return mock_old_thread
return None

mock_bot.get_channel.side_effect = get_channel_side_effect

message_mock = MagicMock()
message_mock.create_thread = AsyncMock()
message_mock.create_thread = AsyncMock(return_value=mock_thread)
mock_channel.send.return_value = message_mock

await threads_cog.post_thread(thread)

mock_channel.send.assert_awaited_once()
args, kwargs = mock_channel.send.await_args
embed = kwargs.get("embed")
assert embed is not None
assert embed.title == thread.title
assert embed.description == thread.content
# Verify old thread was archived
mock_old_thread.send.assert_awaited_once_with(mock_thread.jump_url)
mock_old_thread.edit.assert_awaited_once_with(archived=True, locked=True)

# Verify new thread was created
message_mock.create_thread.assert_awaited_once_with(name=thread.title)


Expand Down