From 9e6390ab3b8b31660f23cdcc410b0efb93b2b500 Mon Sep 17 00:00:00 2001 From: BreadTube Date: Wed, 27 May 2026 00:56:14 +0900 Subject: [PATCH] Add clean-category command --- breadtube_bot/bot.py | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/breadtube_bot/bot.py b/breadtube_bot/bot.py index 0186bac..f08a8a5 100644 --- a/breadtube_bot/bot.py +++ b/breadtube_bot/bot.py @@ -1,6 +1,6 @@ from __future__ import annotations -from enum import Enum +from enum import Enum, StrEnum import html import http.client import logging @@ -21,10 +21,21 @@ from .youtube_subscription import ( 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: DEFAULT_MESSAGE_LIST_LIMIT: int = 50 - INIT_MESSAGE: str = ('Bot initialized.\nThis is the current configuration used.\n' - 'You can upload a new one to update the configuration.') + INIT_MESSAGE: str = ( + '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 YT_CHANNEL_NAME_URL = 'https://www.youtube.com/@' @@ -210,6 +221,28 @@ class Bot: delayed_delete[bot_message.id] = bot_message 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 self.logger.debug('Starting scanning bot channel') messages = self._get_all_channel_messages(self.bot_channel) @@ -277,6 +310,9 @@ class Bot: init_message_found = True continue + if self._parse_command(message): + continue + self.logger.debug('Reading attachment') attachment = message.attachments[0] if attachment.size > self.MAX_DOWNLOAD_SIZE: