diff --git a/redbot/core/modlog.py b/redbot/core/modlog.py index 140f3545d..b57876bdc 100644 --- a/redbot/core/modlog.py +++ b/redbot/core/modlog.py @@ -1020,18 +1020,23 @@ async def create_case( Raises ------ + ValueError + If the action type is not a valid action type. + RuntimeError + If user is the bot itself. TypeError If ``channel`` is of type `discord.PartialMessageable`. """ case_type = await get_casetype(action_type, guild) if case_type is None: - return + raise ValueError(f"{action_type} is not a valid action type.") if not await case_type.is_enabled(): return - if user == bot.user: - return + user_id = user if isinstance(user, int) else user.id + if user_id == bot.user.id: + raise RuntimeError("The bot itself can not be the target of a modlog entry.") if isinstance(channel, discord.PartialMessageable): raise TypeError("Can't use PartialMessageable as the channel for a modlog case.") diff --git a/tests/cogs/test_mod.py b/tests/cogs/test_mod.py index 01c2a8d15..8a2002c78 100644 --- a/tests/cogs/test_mod.py +++ b/tests/cogs/test_mod.py @@ -1,3 +1,4 @@ +from collections import namedtuple import pytest from redbot.pytest.mod import * @@ -9,7 +10,7 @@ async def test_modlog_register_casetype(mod): assert casetype is not None -async def test_modlog_case_create(mod, ctx, member_factory): +async def test_modlog_case_create(mod, ctx, monkeypatch, member_factory, empty_user): from datetime import datetime, timezone # Run casetype register test to register casetype in this test too @@ -22,6 +23,10 @@ async def test_modlog_case_create(mod, ctx, member_factory): moderator = ctx.author reason = "Test 12345" created_at = datetime.now(timezone.utc) + # mod.create_case needs bot to have a user object. Without a connection to discord it would be none by default. + # due to the implementation of bot.user we need to set the user as part of bot._connection + mock_connection = namedtuple("Connection", "user") + monkeypatch.setattr(bot, "_connection", mock_connection(empty_user)) case = await mod.create_case(bot, guild, created_at, case_type, usr, moderator, reason) assert case is not None assert case.user == usr