8000 Add close command for threads/posts (#19) · EuroPython/internal-bot@ab6e005 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab6e005

Browse files
authored
Add close command for threads/posts (#19)
The command will close the thread (if on a text channel), and archive the post (if on a forum channel). When in a forum channel, the command will look for a tag from that specific forum called 'done' (each tag will have different IDs depending on the forum) and apply to the post if found. The bot will send a message notifying who closed it. * Add Test Cases * Add test case when the command doesn't work
1 parent 3512a27 commit ab6e005

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

intbot/core/bot/main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,46 @@ async def wiki(ctx):
3838
suppress_embeds=True,
3939
)
4040

41+
@bot.command()
42+
async def close(ctx):
43+
channel = ctx.channel
44+
parent = channel.parent
45+
author = ctx.message.author
46+
47+
# Check if it's a public or private post (thread)
48+
if channel.type in (discord.ChannelType.public_thread, discord.ChannelType.private_thread):
49+
50+
# Check if the post (thread) was sent in a forum,
51+
# so we can add a tag
52+
if parent.type == discord.ChannelType.forum:
53+
54+
# Get tag from forum
55+
tag = None
56+
for _tag in parent.available_tags:
57+
if _tag.name.lower() == "done":
58+
tag = _tag
59+
break
60+
61+
if tag is not None:
62+
await channel.add_tags(tag)
63+
64+
# Remove command message
65+
await ctx.message.delete()
66+
67+
# Send notification to the thread
68+
await channel.send(f"# This was marked as done by {author.mention}", suppress_embeds=True)
69+
70+
# We need to archive after adding tags in case it was a forum.
71+
await channel.edit(archived=True)
72+
else:
73+
# Remove command message
74+
await ctx.message.delete()
75+
76+
await channel.send("The !close command is intended to be used inside a thread/post",
77+
suppress_embeds=True,
78+
delete_after=5)
79+
80+
4181

4282
@bot.command()
4383
async def version(ctx):

intbot/tests/test_bot/test_main.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from unittest import mock
22
from unittest.mock import AsyncMock, patch
33
import contextlib
4+
import discord
45

56
from django.db import connections
67

78
import pytest
89
from asgiref.sync import sync_to_async
9-
from core.bot.main import ping, poll_database, qlen, source, version, wiki
10+
from core.bot.main import ping, poll_database, qlen, source, version, wiki, close
1011
from core.models import DiscordMessage
1112
from django.utils import timezone
1213

@@ -100,6 +101,40 @@ async def test_wiki_command():
100101
suppress_embeds=True,
101102
)
102103

104+
@pytest.mark.asyncio
105+
async def test_close_command_working():
106+
# Mock context
107+
ctx = AsyncMock()
108+
ctx.channel = AsyncMock()
109+
ctx.message.author = AsyncMock()
110+
ctx.channel.type = discord.ChannelType.public_thread
111+
112+
# Call the command
113+
await close(ctx)
114+
115+
# Assert that the command sent the expected message
116+
ctx.channel.send.assert_called_once_with(
117+
f"# This was marked as done by {ctx.message.author.mention}",
118+
suppress_embeds=True,
119+
)
120+
121+
@pytest.mark.asyncio
122+
async def test_close_command_notworking():
123+
# Mock context
124+
ctx = AsyncMock()
125+
ctx.channel = AsyncMock()
126+
ctx.message.author = AsyncMock()
127+
128+
# Call the command
129+
await close(ctx)
130+
131+
# Assert that the command sent the expected message
132+
ctx.channel.send.assert_called_once_with(
133+
"The !close command is intended to be used inside a thread/post",
134+
suppress_embeds=True,
135+
delete_after=5
136+
)
137+
103138

104139
@pytest.mark.asyncio
105140
async def test_version_command():

0 commit comments

Comments
 (0)
0