Skip to content

Commit 4c09e55

Browse files
add new check and error handler for the solved command (#28)
1 parent 59cba25 commit 4c09e55

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

modules/help.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
""".strip()
5151

5252

53+
class NotThreadOwner(commands.CheckFailure):
54+
pass
55+
56+
5357
def is_forum_thread() -> Check[Context]:
5458
def predicate(ctx: Context) -> bool:
5559
channel = ctx.channel
@@ -67,6 +71,21 @@ def predicate(ctx: Context) -> bool:
6771
return commands.check(predicate)
6872

6973

74+
def can_close_thread() -> Check[Context]:
75+
def predicate(ctx: core.GuildContext) -> bool:
76+
assert isinstance(ctx.channel, discord.Thread)
77+
78+
if ctx.author_is_mod():
79+
return True
80+
81+
if ctx.author == ctx.channel.owner:
82+
return True
83+
84+
raise NotThreadOwner()
85+
86+
return commands.check(predicate)
87+
88+
7089
class Help(commands.Cog):
7190
"""Commands relating to the help channels/forum of Pythonista."""
7291

@@ -86,6 +105,7 @@ async def forum_post_created(self, thread: discord.Thread) -> None:
86105

87106
await thread.send(FORUM_BLURB)
88107

108+
@can_close_thread()
89109
@is_forum_thread()
90110
@commands.command("solved", brief="Closes a forum post", short_doc="Closes a forum post in the help channels")
91111
async def solved(self, ctx: core.GuildContext) -> None:
@@ -97,10 +117,6 @@ async def solved(self, ctx: core.GuildContext) -> None:
97117
assert isinstance(ctx.channel, discord.Thread) # guarded by the check
98118
assert isinstance(ctx.channel.parent, discord.ForumChannel) # guarded by the check
99119

100-
if not ctx.author_is_mod() or ctx.author != ctx.channel.owner:
101-
await ctx.send("You can only mark your own posts as solved")
102-
return
103-
104120
try:
105121
emoji = discord.utils.get(ctx.guild.emojis, id=578575442383208468)
106122
if emoji:
@@ -128,6 +144,16 @@ async def solved(self, ctx: core.GuildContext) -> None:
128144
msg: str = f"{ctx.author} ({ctx.author.id}) marked thread '{ctx.channel.name}' ({ctx.channel.id}) as solved."
129145
await channel.send(msg)
130146

147+
@solved.error
148+
async def solved_error(self, ctx: Context, error: commands.CommandError) -> None:
149+
error = getattr(error, "original", error)
150+
151+
if isinstance(error, NotThreadOwner):
152+
await ctx.send("Sorry, you must be the owner of this thread to close it!")
153+
154+
elif isinstance(error, commands.CheckFailure):
155+
await ctx.send("Sorry, this doesn't appear to be a forum thread?")
156+
131157

132158
async def setup(bot: core.Bot) -> None:
133159
await bot.add_cog(Help(bot))

0 commit comments

Comments
 (0)