Skip to content

Commit

Permalink
Merge pull request #2580 from sopel-irc/implied-chanmsg-bot_priv
Browse files Browse the repository at this point in the history
plugin: `require_bot_privilege()` implies `require_chanmsg()`
  • Loading branch information
dgw authored Dec 9, 2023
2 parents 120477f + ff98f78 commit 6a25d17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 5 additions & 3 deletions sopel/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,16 +1716,18 @@ def require_bot_privilege(
<.bot.Sopel.say>`, but when ``reply`` is ``True`` it uses :meth:`bot.reply()
<.bot.Sopel.reply>` instead.
Privilege requirements are ignored in private messages.
Use of ``require_bot_privilege()`` implies :func:`require_chanmsg`.
.. versionadded:: 7.1
.. versionchanged:: 8.0
Decorated callables no longer run in response to private messages.
"""
def actual_decorator(function):
@functools.wraps(function)
def guarded(bot, trigger, *args, **kwargs):
# If this is a privmsg, ignore privilege requirements
# If this is a privmsg, do not trigger
if trigger.is_privmsg:
return function(bot, trigger, *args, **kwargs)
return

if not bot.has_channel_privilege(trigger.sender, level):
if message and not callable(message):
Expand Down
10 changes: 6 additions & 4 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,19 +511,21 @@ def test_require_bot_privilege_private_message(configfactory,
def mock(bot, trigger):
return True

assert mock(bot, bot._trigger) is True
assert mock(bot, bot._trigger) is not True, (
'Callable requiring bot channel privilege must be ignored in private.')

@plugin.command('ban')
@plugin.require_bot_privilege(plugin.OP)
def mock(bot, trigger):
return True

assert mock(bot, bot._trigger) is True
assert mock(bot, bot._trigger) is not True, (
'Callable requiring bot channel privilege must be ignored in private.')

@plugin.command('ban')
@plugin.require_bot_privilege(plugin.OWNER)
def mock(bot, trigger):
return True

assert mock(bot, bot._trigger) is True, (
'There must not be privilege check for a private message.')
assert mock(bot, bot._trigger) is not True, (
'Callable requiring bot channel privilege must be ignored in private.')

0 comments on commit 6a25d17

Please sign in to comment.