Add clean-category command

This commit is contained in:
BreadTube 2026-05-27 00:56:14 +09:00
commit 9e6390ab3b

View file

@ -1,6 +1,6 @@
from __future__ import annotations from __future__ import annotations
from enum import Enum from enum import Enum, StrEnum
import html import html
import http.client import http.client
import logging import logging
@ -21,10 +21,21 @@ from .youtube_subscription import (
SUBSCRIPTION_FILE_COLUMNS, SubscriptionHelper, SubscriptionInfo, Subscriptions, VideoInfo) SUBSCRIPTION_FILE_COLUMNS, SubscriptionHelper, SubscriptionInfo, Subscriptions, VideoInfo)
class BotCommand(StrEnum):
UNKNOWN = ''
CATEGORY_CLEAN = 'clean-category'
@classmethod
def _missing_(cls, value): # noqa: ARG003
return cls.UNKNOWN
class Bot: class Bot:
DEFAULT_MESSAGE_LIST_LIMIT: int = 50 DEFAULT_MESSAGE_LIST_LIMIT: int = 50
INIT_MESSAGE: str = ('Bot initialized.\nThis is the current configuration used.\n' INIT_MESSAGE: str = (
'You can upload a new one to update the configuration.') 'Bot initialized.\nThis is the current configuration used.\n'
'You can upload a new one to update the configuration.\n'
f"Available commands: {', '.join(['`' + c.value + '`' for c in BotCommand if c != BotCommand.UNKNOWN])}")
MAX_DOWNLOAD_SIZE: int = 50_000 MAX_DOWNLOAD_SIZE: int = 50_000
YT_CHANNEL_NAME_URL = 'https://www.youtube.com/@' YT_CHANNEL_NAME_URL = 'https://www.youtube.com/@'
@ -210,6 +221,28 @@ class Bot:
delayed_delete[bot_message.id] = bot_message delayed_delete[bot_message.id] = bot_message
return delayed_delete return delayed_delete
def _clean_category(self, category: ChannelCategory):
self.logger.info('Cleaning category "%s" (%d)', category.name, category.id)
for channel in self.guild_categories:
if channel.parent_id == category.id:
self.logger.debug('Cleaning category -> deleting channel "%s"', channel.name)
self.discord_manager.delete_text_channel(channel.id, request_timeout=self.config.request_timeout)
def _parse_command(self, message: Message) -> bool:
match BotCommand(message.content[:100].split(' ')[0]) if message.content else BotCommand.UNKNOWN:
case BotCommand.CATEGORY_CLEAN:
arguments = message.content.split(' ')[1:]
if not arguments:
self.logger.info('Category clean command received with no arguments')
return True
for name in arguments:
for category in self.guild_categories:
if category.name == name:
self._clean_category(category)
break
return True
return False
def _scan_bot_channel(self): # noqa: PLR0915 def _scan_bot_channel(self): # noqa: PLR0915
self.logger.debug('Starting scanning bot channel') self.logger.debug('Starting scanning bot channel')
messages = self._get_all_channel_messages(self.bot_channel) messages = self._get_all_channel_messages(self.bot_channel)
@ -277,6 +310,9 @@ class Bot:
init_message_found = True init_message_found = True
continue continue
if self._parse_command(message):
continue
self.logger.debug('Reading attachment') self.logger.debug('Reading attachment')
attachment = message.attachments[0] attachment = message.attachments[0]
if attachment.size > self.MAX_DOWNLOAD_SIZE: if attachment.size > self.MAX_DOWNLOAD_SIZE: