Skip to content

Commit

Permalink
Merge pull request #89 from trag1c/repo-cleanup
Browse files Browse the repository at this point in the history
chore: repo cleanup
  • Loading branch information
mitchellh authored Dec 31, 2024
2 parents af507b8 + a6b3dea commit 25855e0
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 533 deletions.
10 changes: 0 additions & 10 deletions app/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import sys
from time import sleep

import sentry_sdk

from app.core import bot, config
Expand All @@ -12,11 +9,4 @@
profiles_sample_rate=1.0,
)

if "--rate-limit-delay" in sys.argv:
print(
"The bot went offline due to a rate limit."
" It will come back online in 25 minutes."
)
sleep(60 * 25)

bot.run(config.BOT_TOKEN)
3 changes: 3 additions & 0 deletions app/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from app.components import docs, entity_mentions, message_filter, move_message

__all__ = ("docs", "entity_mentions", "message_filter", "move_message")
2 changes: 1 addition & 1 deletion app/features/docs.py → app/components/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from discord.app_commands import Choice, autocomplete
from github.ContentFile import ContentFile

from app.features.entity_mentions import REPOSITORIES
from app.components.entity_mentions import REPOSITORIES
from app.setup import bot
from app.utils import SERVER_ONLY

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from app.setup import bot, config, gh
from app.utils import is_dm, try_dm
from app.view import DeleteMention

ENTITY_REGEX = re.compile(r"(?:\b(web|bot|main))?#(\d{1,6})(?!\.\d)\b")
ENTITY_TEMPLATE = "**{kind} #{entity.number}:** {entity.title}\n<{entity.html_url}>\n"
Expand Down Expand Up @@ -41,6 +40,32 @@
EntityKind = Literal["Pull Request", "Issue", "Discussion"]


class DeleteMention(discord.ui.View):
def __init__(self, message: discord.Message, entity_count: int) -> None:
super().__init__()
self.message = message
self.plural = entity_count > 1

@discord.ui.button(
label="Delete",
emoji="🗑️",
style=discord.ButtonStyle.gray,
)
async def delete(
self, interaction: discord.Interaction, but: discord.ui.Button
) -> None:
if interaction.user.id != self.message.author.id:
await interaction.response.send_message(
"Only the person who mentioned "
+ ("these entities" if self.plural else "this entity")
+ " can remove this message.",
ephemeral=True,
)
return
assert interaction.message
await interaction.message.delete()


class Entity(Protocol):
number: int
title: str
Expand Down
File renamed without changes.
58 changes: 55 additions & 3 deletions app/view/move_message.py → app/components/move_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import discord

from app import config
from app.setup import bot
from app.utils import get_or_create_webhook, move_message_via_webhook
from app.setup import bot, config
from app.utils import (
SERVER_ONLY,
get_or_create_webhook,
is_dm,
is_helper,
is_mod,
move_message_via_webhook,
)


class SelectChannel(discord.ui.View):
Expand Down Expand Up @@ -104,3 +110,49 @@ async def on_submit(self, interaction: discord.Interaction) -> None:
await interaction.followup.send(
content=f"Help post created: <#{msg.channel.id}>", ephemeral=True
)


@bot.tree.context_menu(name="Move message")
@discord.app_commands.default_permissions(manage_messages=True)
@SERVER_ONLY
async def move_message(
interaction: discord.Interaction, message: discord.Message
) -> None:
"""
Adds a context menu item to a message to move it to a different channel.
This is used as a moderation tool to make discussion on-topic.
"""
assert not is_dm(interaction.user)

if not (is_mod(interaction.user) or is_helper(interaction.user)):
await interaction.response.send_message(
"You do not have permission to move messages.", ephemeral=True
)
return

await interaction.response.send_message(
"Select a channel to move this message to.",
view=SelectChannel(message, executor=interaction.user),
ephemeral=True,
)


@bot.tree.context_menu(name="Turn into #help post")
@discord.app_commands.default_permissions(manage_messages=True)
@SERVER_ONLY
async def turn_into_help_post(
interaction: discord.Interaction, message: discord.Message
) -> None:
"""
An extension of the move_message function that creates a #help post and then
moves the message to that channel.
"""
assert not is_dm(interaction.user)

if not (is_mod(interaction.user) or is_helper(interaction.user)):
await interaction.response.send_message(
"You do not have permission to use this action.", ephemeral=True
)
return

await interaction.response.send_modal(HelpPostTitle(message))
27 changes: 3 additions & 24 deletions app/core.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from __future__ import annotations

import os
import sys
from pathlib import Path
from traceback import print_tb
from typing import cast

import discord
from discord.ext import commands
from sentry_sdk import capture_exception

from app.features.docs import refresh_sitemap
from app.features.entity_mentions import ENTITY_REGEX, handle_entities
from app.features.message_filter import check_message_filters
from app.components.docs import refresh_sitemap
from app.components.entity_mentions import ENTITY_REGEX, handle_entities
from app.components.message_filter import check_message_filters
from app.setup import bot, config
from app.utils import is_dm, is_mod, try_dm

Expand Down Expand Up @@ -74,19 +72,6 @@ async def sync(bot: commands.Bot, message: discord.Message) -> None:


def handle_error(error: BaseException) -> None:
if _is_ratelimit(error):
# Restart the bot with a delay at startup.
# This effectively replaces the current process.
os.execv(
sys.executable,
(
"python",
Path(__file__).parent / "__main__.py",
*sys.argv[1:],
"--rate-limit-delay",
),
)

if config.SENTRY_DSN is not None:
capture_exception(error)
return
Expand All @@ -95,9 +80,3 @@ def handle_error(error: BaseException) -> None:
print_tb(error.__traceback__)
if isinstance(error, discord.app_commands.CommandInvokeError):
handle_error(error.original)


def _is_ratelimit(error: BaseException) -> bool:
if isinstance(error, discord.app_commands.CommandInvokeError):
error = error.original
return isinstance(error, discord.HTTPException) and error.status == 429
3 changes: 0 additions & 3 deletions app/features/__init__.py

This file was deleted.

52 changes: 0 additions & 52 deletions app/features/move_message.py

This file was deleted.

4 changes: 0 additions & 4 deletions app/view/__init__.py

This file was deleted.

27 changes: 0 additions & 27 deletions app/view/entity_mentions.py

This file was deleted.

Loading

0 comments on commit 25855e0

Please sign in to comment.