Code Duplication    Length = 27-28 lines in 2 locations

sopel/module.py 2 locations

@@ 482-509 (lines=28) @@
479
    return actual_decorator
480
481
482
def require_admin(message=None, reply=False):
483
    """Decorate a function to require the triggering user to be a bot admin.
484
485
    :param str message: optional message said to non-admin user
486
    :param bool reply: use :meth:`~.bot.Sopel.reply` instead of
487
                       :meth:`~.bot.Sopel.say` when ``True``; defaults to
488
                       ``False``
489
490
    When the triggering user is not an admin, the command is not run, and the
491
    bot will say the ``message`` if given. By default, it uses
492
    :meth:`bot.say() <.bot.Sopel.say>`, but when ``reply`` is true, then it
493
    uses :meth:`bot.reply() <.bot.Sopel.reply>` instead.
494
495
    .. versionchanged:: 7.0.0
496
        Added the ``reply`` parameter.
497
    """
498
    def actual_decorator(function):
499
        @functools.wraps(function)
500
        def guarded(bot, trigger, *args, **kwargs):
501
            if not trigger.admin:
502
                if message and not callable(message):
503
                    if reply:
504
                        bot.reply(message)
505
                    else:
506
                        bot.say(message)
507
            else:
508
                return function(bot, trigger, *args, **kwargs)
509
        return guarded
510
511
    # Hack to allow decorator without parens
512
    if callable(message):
@@ 518-544 (lines=27) @@
515
    return actual_decorator
516
517
518
def require_owner(message=None, reply=False):
519
    """Decorate a function to require the triggering user to be the bot owner.
520
521
    :param str message: optional message said to non-owner user
522
    :param bool reply: use :meth:`~.bot.Sopel.reply` instead of
523
                       :meth:`~.bot.Sopel.say` when ``True``; defaults to
524
                       ``False``
525
526
    When the triggering user is not the bot's owner, the command is not run,
527
    and the bot will say ``message`` if given. By default, it uses
528
    :meth:`bot.say() <.bot.Sopel.say>`, but when ``reply`` is true, then it
529
    uses :meth:`bot.reply() <.bot.Sopel.reply>` instead.
530
531
    .. versionchanged:: 7.0.0
532
        Added the ``reply`` parameter.
533
    """
534
    def actual_decorator(function):
535
        @functools.wraps(function)
536
        def guarded(bot, trigger, *args, **kwargs):
537
            if not trigger.owner:
538
                if message and not callable(message):
539
                    if reply:
540
                        bot.reply(message)
541
                    else:
542
                        bot.say(message)
543
            else:
544
                return function(bot, trigger, *args, **kwargs)
545
        return guarded
546
547
    # Hack to allow decorator without parens