Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rework /close command #144

Merged
merged 6 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BOT_HELP_CHANNEL_ID=7635317444894061557
BOT_LOG_CHANNEL_ID=1305317411752042821
BOT_MEDIA_CHANNEL_ID=1245859287631622769
BOT_SHOWCASE_CHANNEL_ID=1245859287634524358
BOT_HELP_CHANNEL_TAG_IDS=github:3838252033636238646,solved:2332458794421338148
BOT_HELP_CHANNEL_TAG_IDS=moved:8101333548117981934,solved:1910683006289466725,stale:4776007530863128542,duplicate:5313753553802740706
BOT_MOD_ROLE_ID=1245855561015099402
BOT_HELPER_ROLE_ID=1245855736495049483
BOT_TOKEN=CdwafxF9MTRvxKKtXpeqKdI4qE.P5PKdC.ke2veU7KdecW5jxgTu-5fgCUssNlPAdNf3tsG7
Expand Down
127 changes: 88 additions & 39 deletions app/components/close_help_post.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,85 @@
import re
from types import SimpleNamespace
from typing import cast
from typing import Literal, cast

import discord
from discord import app_commands

from app.components.docs import get_docs_link
from app.components.entity_mentions import entity_message
from app.setup import bot, config
from app.utils import is_dm, is_helper, is_mod

TAG_PATTERN = re.compile(r"\[(?:SOLVED|MOVED: #\d+)\]", re.IGNORECASE)

async def mention_entity(entity_id: int, owner_id: int) -> str:
msg, _ = await entity_message(
# Forging a message to use the entity_mentions logic
cast(
discord.Message,
SimpleNamespace(
content=f"#{entity_id}",
author=SimpleNamespace(id=owner_id),
),
)
)
return msg


class Close(app_commands.Group):
@app_commands.command(name="solved", description="Mark post as solved.")
@app_commands.describe(config_option="Config option name (optional)")
async def solved(
self, interaction: discord.Interaction, config_option: str | None = None
) -> None:
if config_option:
additional_reply = get_docs_link("option", config_option)
title_prefix = f"[SOLVED: {config_option}]"
else:
title_prefix = additional_reply = None
await close_post(interaction, "solved", title_prefix, additional_reply)

@app_commands.command(name="moved", description="Mark post as moved to GitHub.")
@app_commands.describe(entity_id="New GitHub entity number")
async def moved(self, interaction: discord.Interaction, entity_id: int) -> None:
await close_post(
interaction,
"moved",
title_prefix=f"[MOVED: #{entity_id}]",
trag1c marked this conversation as resolved.
Show resolved Hide resolved
additional_reply=await mention_entity(entity_id, interaction.user.id),
)

@app_commands.command(name="duplicate", description="Mark post as duplicate.")
@app_commands.describe(
original="The original GitHub entity (number) or help post (ID or link)"
)
async def duplicate(self, interaction: discord.Interaction, original: str) -> None:
*_, id_ = original.rpartition("/")
if len(id_) < 10:
# GitHub entity number
title_prefix = f"[DUPLICATE: #{id_}]"
trag1c marked this conversation as resolved.
Show resolved Hide resolved
additional_reply = await mention_entity(int(id_), interaction.user.id)
else:
# Help post ID
title_prefix = None
additional_reply = f"Original post: <#{id_}>"
trag1c marked this conversation as resolved.
Show resolved Hide resolved
trag1c marked this conversation as resolved.
Show resolved Hide resolved
await close_post(interaction, "duplicate", title_prefix, additional_reply)

@app_commands.command(name="stale", description="Mark post as stale.")
async def stale(self, interaction: discord.Interaction) -> None:
await close_post(interaction, "stale")

@app_commands.command(name="wontfix", description="Mark post as stale.")
async def wontfix(self, interaction: discord.Interaction) -> None:
await close_post(interaction, "stale", "[WON'T FIX]")


bot.tree.add_command(Close(name="close", description="Mark current post as resolved."))


@bot.tree.command(name="close", description="Mark current post as resolved.")
@discord.app_commands.describe(
gh_number="GitHub entity number for #help posts moved there"
)
@discord.app_commands.guild_only()
async def close_post(
interaction: discord.Interaction, gh_number: int | None = None
interaction: discord.Interaction,
tag: Literal["solved", "moved", "duplicate", "stale"],
title_prefix: str | None = None,
additional_reply: str | None = None,
) -> None:
if not (
isinstance(post := interaction.channel, discord.Thread)
Expand All @@ -29,45 +91,32 @@ async def close_post(
)
return

assert not is_dm(interaction.user)
if not (
is_mod(interaction.user)
or is_helper(interaction.user)
or interaction.user.id == post.owner_id
):
user = interaction.user
assert not is_dm(user)
if not (is_mod(user) or is_helper(user) or user.id == post.owner_id):
await interaction.response.send_message(
"You don't have permission to close this post.", ephemeral=True
"You don't have permission to resolve this post.", ephemeral=True
)
return

if post.archived:
help_tags = {tag for tag in cast(discord.ForumChannel, post.parent).available_tags}

if set(post.applied_tags) & help_tags:
await interaction.response.send_message(
"This post is already closed.", ephemeral=True
"This post was already resolved.", ephemeral=True
)
return

help_tags = cast(discord.ForumChannel, post.parent).available_tags
desired_tag_id = config.HELP_CHANNEL_TAG_IDS["github" if gh_number else "solved"]
tag = next(tag for tag in help_tags if tag.id == desired_tag_id)
await post.add_tags(tag)

await interaction.response.defer(ephemeral=True)

if not TAG_PATTERN.search(post.name):
post_name_tag = f"[MOVED: #{gh_number}]" if gh_number else "[SOLVED]"
await post.edit(name=f"{post_name_tag} {post.name}")
desired_tag_id = config.HELP_CHANNEL_TAG_IDS[tag]
await post.add_tags(next(tag for tag in help_tags if tag.id == desired_tag_id))

if gh_number:
# Pretending this is a message to use the entity_mentions logic
message = cast(
discord.Message,
SimpleNamespace(
content=f"#{gh_number}",
author=SimpleNamespace(id=interaction.user.id),
),
)
msg_content, _ = await entity_message(message)
await post.send(msg_content)
if title_prefix is None:
title_prefix = f"[{tag.upper()}]"
await post.edit(name=f"{title_prefix} {post.name}")

if additional_reply:
await post.send(additional_reply)

await post.edit(archived=True)
trag1c marked this conversation as resolved.
Show resolved Hide resolved
await interaction.followup.send("Post marked as resolved.", ephemeral=True)
await interaction.followup.send("Post resolved.", ephemeral=True)
trag1c marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 14 additions & 12 deletions app/components/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,22 @@ async def page_autocomplete(
async def docs(
interaction: discord.Interaction, section: str, page: str, message: str = ""
) -> None:
if section not in SECTIONS:
await interaction.response.send_message(
f"Invalid section {section!r}", ephemeral=True
)
return
if page not in sitemap.get(section, []):
try:
await interaction.response.send_message(
f"Invalid page {page!r}", ephemeral=True
f"{message}\n{get_docs_link(section, page)}"
)
return
except ValueError as exc:
await interaction.response.send_message(str(exc), ephemeral=True)

section_path = SECTIONS[section]
page = page if page != "overview" else ""

await interaction.response.send_message(
f"{message}\n{URL_TEMPLATE.format(section=section_path, page=page)}"
def get_docs_link(section: str, page: str) -> str:
if section not in SECTIONS:
msg = f"Invalid section {section!r}"
raise ValueError(msg)
if page not in sitemap.get(section, []):
msg = f"Invalid page {page!r}"
raise ValueError(msg)
return URL_TEMPLATE.format(
section=SECTIONS[section],
page=page if page != "overview" else "",
)