[Modlog]Raise error instead of failing silently on invalid arguments (#5386)

Co-authored-by: Dav <dav@mail.stopdavabuse.de>
Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
Co-authored-by: Flame442 <34169552+Flame442@users.noreply.github.com>
This commit is contained in:
Dav 2023-03-18 22:31:28 +01:00 committed by GitHub
parent 2ab204438e
commit 7c7e5edb19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -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.")

View File

@ -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