diff --git a/commands/help/main.py b/commands/help/main.py index bf360fb..abba6ad 100644 --- a/commands/help/main.py +++ b/commands/help/main.py @@ -163,7 +163,7 @@ class HelpCommands(commands.Cog): # Success embed embed = EmbedTemplate.success( - title="✅ Help Topic Created!", + title="Help Topic Created!", description=f"The help topic `/help {help_cmd.name}` has been created successfully." ) diff --git a/services/custom_commands_service.py b/services/custom_commands_service.py index dc70c4b..a830d20 100644 --- a/services/custom_commands_service.py +++ b/services/custom_commands_service.py @@ -455,14 +455,23 @@ class CustomCommandsService(BaseService[CustomCommand]): ('sort', '-use_count'), ('limit', limit) ] - + commands_data = await self.get_items_with_params(params) - + commands = [] for cmd_data in commands_data: - creator = await self.get_creator_by_id(cmd_data.creator_id) - commands.append(CustomCommand(**cmd_data.model_dump(), creator=creator)) - + try: + creator = await self.get_creator_by_id(cmd_data.creator_id) + commands.append(CustomCommand(**cmd_data.model_dump(), creator=creator)) + except BotException as e: + # Handle missing creator gracefully + self.logger.warning("Skipping popular command with missing creator", + command_id=cmd_data.id, + command_name=cmd_data.name, + creator_id=cmd_data.creator_id, + error=str(e)) + continue + return commands async def get_command_names_for_autocomplete( @@ -633,38 +642,56 @@ class CustomCommandsService(BaseService[CustomCommand]): async def get_commands_needing_warning(self) -> List[CustomCommand]: """Get commands that need deletion warning (60+ days unused).""" cutoff_date = datetime.now() - timedelta(days=60) - + params = [ ('last_used__lt', cutoff_date.isoformat()), ('warning_sent', False), ('is_active', True) ] - + commands_data = await self.get_items_with_params(params) - + commands = [] for cmd_data in commands_data: - creator = await self.get_creator_by_id(cmd_data.creator_id) - commands.append(CustomCommand(**cmd_data.model_dump(), creator=creator)) - + try: + creator = await self.get_creator_by_id(cmd_data.creator_id) + commands.append(CustomCommand(**cmd_data.model_dump(), creator=creator)) + except BotException as e: + # Handle missing creator gracefully + self.logger.warning("Skipping command with missing creator", + command_id=cmd_data.id, + command_name=cmd_data.name, + creator_id=cmd_data.creator_id, + error=str(e)) + continue + return commands async def get_commands_eligible_for_deletion(self) -> List[CustomCommand]: """Get commands eligible for deletion (90+ days unused).""" cutoff_date = datetime.now() - timedelta(days=90) - + params = [ ('last_used__lt', cutoff_date.isoformat()), ('is_active', True) ] - + commands_data = await self.get_items_with_params(params) - + commands = [] for cmd_data in commands_data: - creator = await self.get_creator_by_id(cmd_data.creator_id) - commands.append(CustomCommand(**cmd_data.model_dump(), creator=creator)) - + try: + creator = await self.get_creator_by_id(cmd_data.creator_id) + commands.append(CustomCommand(**cmd_data.model_dump(), creator=creator)) + except BotException as e: + # Handle missing creator gracefully + self.logger.warning("Skipping command with missing creator", + command_id=cmd_data.id, + command_name=cmd_data.name, + creator_id=cmd_data.creator_id, + error=str(e)) + continue + return commands async def mark_warning_sent(self, command_name: str) -> bool: diff --git a/tests/test_models_help_command.py b/tests/test_models_help_command.py index df4486d..c717c47 100644 --- a/tests/test_models_help_command.py +++ b/tests/test_models_help_command.py @@ -25,7 +25,7 @@ class TestHelpCommandModel: name='test-topic', title='Test Topic', content='This is test content', - created_by_discord_id=123456789, + created_by_discord_id='123456789', created_at=datetime.now() ) @@ -33,7 +33,7 @@ class TestHelpCommandModel: assert help_cmd.name == 'test-topic' assert help_cmd.title == 'Test Topic' assert help_cmd.content == 'This is test content' - assert help_cmd.created_by_discord_id == 123456789 + assert help_cmd.created_by_discord_id == '123456789' assert help_cmd.is_active is True assert help_cmd.view_count == 0 @@ -46,10 +46,10 @@ class TestHelpCommandModel: title='Trading Rules & Guidelines', content='Complete trading rules...', category='rules', - created_by_discord_id=123456789, + created_by_discord_id='123456789', created_at=now, updated_at=now, - last_modified_by=987654321, + last_modified_by='987654321', is_active=True, view_count=100, display_order=10 @@ -57,7 +57,7 @@ class TestHelpCommandModel: assert help_cmd.category == 'rules' assert help_cmd.updated_at == now - assert help_cmd.last_modified_by == 987654321 + assert help_cmd.last_modified_by == '987654321' assert help_cmd.view_count == 100 assert help_cmd.display_order == 10 @@ -67,7 +67,7 @@ class TestHelpCommandModel: 'id': 3, 'title': 'Test', 'content': 'Content', - 'created_by_discord_id': 123, + 'created_by_discord_id': '123', 'created_at': datetime.now() } @@ -98,7 +98,7 @@ class TestHelpCommandModel: 'id': 4, 'name': 'test', 'content': 'Content', - 'created_by_discord_id': 123, + 'created_by_discord_id': '123', 'created_at': datetime.now() } @@ -120,7 +120,7 @@ class TestHelpCommandModel: 'id': 5, 'name': 'test', 'title': 'Test', - 'created_by_discord_id': 123, + 'created_by_discord_id': '123', 'created_at': datetime.now() } @@ -143,7 +143,7 @@ class TestHelpCommandModel: 'name': 'test', 'title': 'Test', 'content': 'Content', - 'created_by_discord_id': 123, + 'created_by_discord_id': '123', 'created_at': datetime.now() } @@ -168,7 +168,7 @@ class TestHelpCommandModel: name='active', title='Active Topic', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now(), is_active=True ) @@ -178,7 +178,7 @@ class TestHelpCommandModel: name='deleted', title='Deleted Topic', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now(), is_active=False ) @@ -194,7 +194,7 @@ class TestHelpCommandModel: name='test', title='Test', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now(), updated_at=None ) @@ -206,7 +206,7 @@ class TestHelpCommandModel: name='test', title='Test', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now(), updated_at=datetime.now() - timedelta(days=5) ) @@ -219,7 +219,7 @@ class TestHelpCommandModel: name='test', title='Test', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now() - timedelta(days=30) ) assert old.days_since_creation == 30 @@ -232,7 +232,7 @@ class TestHelpCommandModel: name='test', title='Test', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now(), view_count=0 ) @@ -244,7 +244,7 @@ class TestHelpCommandModel: name='test', title='Test', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now() - timedelta(days=5), view_count=50 ) @@ -257,7 +257,7 @@ class TestHelpCommandModel: name='test', title='Test', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now() - timedelta(days=100), view_count=50 ) @@ -356,7 +356,7 @@ class TestHelpCommandSearchResult: name=f'topic-{i}', title=f'Topic {i}', content=f'Content {i}', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now() ) for i in range(1, 11) @@ -444,7 +444,7 @@ class TestHelpCommandStats: name='popular-topic', title='Popular Topic', content='Content', - created_by_discord_id=123, + created_by_discord_id='123', created_at=datetime.now(), view_count=500 ) @@ -497,10 +497,10 @@ class TestHelpCommandFromAPIData: 'title': 'Trading Rules & Guidelines', 'content': 'Complete trading rules...', 'category': 'rules', - 'created_by_discord_id': 123456789, + 'created_by_discord_id': '123456789', 'created_at': '2025-01-01T12:00:00', 'updated_at': '2025-01-10T15:30:00', - 'last_modified_by': 987654321, + 'last_modified_by': '987654321', 'is_active': True, 'view_count': 100, 'display_order': 10 @@ -522,7 +522,7 @@ class TestHelpCommandFromAPIData: 'name': 'simple-topic', 'title': 'Simple Topic', 'content': 'Simple content', - 'created_by_discord_id': 123456789, + 'created_by_discord_id': '123456789', 'created_at': '2025-01-01T12:00:00' } diff --git a/tests/test_services_help_commands.py b/tests/test_services_help_commands.py index 5704922..849c3d7 100644 --- a/tests/test_services_help_commands.py +++ b/tests/test_services_help_commands.py @@ -30,7 +30,7 @@ def sample_help_command() -> HelpCommand: title='Trading Rules & Guidelines', content='Complete trading rules for the league...', category='rules', - created_by_discord_id=123456789, + created_by_discord_id='123456789', created_at=now, updated_at=None, last_modified_by=None, @@ -130,7 +130,7 @@ class TestHelpCommandsServiceCRUD: name="test-topic", title="Test Topic", content="This is test content for the help topic.", - creator_discord_id=123456789, + creator_discord_id='123456789', category="info" ) @@ -154,7 +154,7 @@ class TestHelpCommandsServiceCRUD: name="trading-rules", title="Trading Rules", content="Rules content", - creator_discord_id=123456789 + creator_discord_id='123456789' ) @pytest.mark.asyncio @@ -220,7 +220,7 @@ class TestHelpCommandsServiceCRUD: created_by_discord_id=sample_help_command.created_by_discord_id, created_at=sample_help_command.created_at, updated_at=datetime.now(timezone.utc), - last_modified_by=987654321, + last_modified_by='987654321', is_active=sample_help_command.is_active, view_count=sample_help_command.view_count, display_order=sample_help_command.display_order @@ -242,7 +242,7 @@ class TestHelpCommandsServiceCRUD: name="trading-rules", new_title="Updated Trading Rules", new_content="Updated content", - updater_discord_id=987654321 + updater_discord_id='987654321' ) assert isinstance(result, HelpCommand) @@ -275,7 +275,7 @@ class TestHelpCommandsServiceCRUD: name='deleted-topic', title='Deleted Topic', content='Content', - created_by_discord_id=123456789, + created_by_discord_id='123456789', created_at=datetime.now(timezone.utc), is_active=False ) @@ -327,7 +327,7 @@ class TestHelpCommandsServiceSearch: 'title': 'Trading Rules', 'content': 'Content', 'category': 'rules', - 'created_by_discord_id': 123, + 'created_by_discord_id': '123', 'created_at': datetime.now(timezone.utc).isoformat(), 'is_active': True, 'view_count': 100, @@ -362,7 +362,7 @@ class TestHelpCommandsServiceSearch: 'title': f'Topic {i}', 'content': f'Content {i}', 'category': 'rules' if i % 2 == 0 else 'guides', - 'created_by_discord_id': 123, + 'created_by_discord_id': '123', 'created_at': datetime.now(timezone.utc).isoformat(), 'is_active': True, 'view_count': i * 10, @@ -467,7 +467,7 @@ class TestHelpCommandsServiceStatistics: 'name': 'popular-topic', 'title': 'Popular Topic', 'content': 'Content', - 'created_by_discord_id': 123, + 'created_by_discord_id': '123', 'created_at': datetime.now(timezone.utc).isoformat(), 'is_active': True, 'view_count': 500,