python类check()的实例源码

Permissions.py 文件源码 项目:Sparcli 作者: 4Kaylum 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def requiredEnabled(**kwargs):
    '''Checks to see if a certain token type's secret exists

    Parameters :: 
        token : str
            The type of token whose secret needs to exist
    '''

    def predicate(ctx):
        token = kwargs.get('enable')
        serverSettings = getServerJson(ctx.message.server.id)
        enabled = serverSettings['Toggles'][token]
        if enabled:
            return True
        else:
            raise CommandDisabled
            return False

    return commands.check(predicate)
Permissions.py 文件源码 项目:Sparcli 作者: 4Kaylum 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def needsToken(**kwargs):
    '''Checks to see if a certain token type's secret exists

    Parameters :: 
        token : str
            The type of token whose secret needs to exist
    '''

    def predicate(ctx):
        token = kwargs.get('token')
        v = getTokens()[token]

        for i, o in v.items():
            if not o:
                raise ThisNeedsAToken
        return True        
    return commands.check(predicate)
registry.py 文件源码 项目:PomodoroBot 作者: VicenteRD 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def check_last(self, ctx: commands.Context, name):
        """ Shows you how long other users' last session lasted.

            :param name: The name (not the nick) of the person to check.
            Must use the name#discriminator format.
        """
        time_str = printable_time(db_manager.get_user_last_session(name))
        if time_str is None:
            time_str = "None found."

        lib.log("{} queried for {}'s last session time. Result: {}"
                .format(lib.get_author_name(ctx, True),
                        "their" if name == str(ctx.message.author)
                        else (name + "'s"), time_str))

        await self.bot.say("```{}```".format(time_str),
                           delete_after=self.bot.ans_lifespan * 3)
registry.py 文件源码 项目:PomodoroBot 作者: VicenteRD 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def total(self, ctx: commands.Context, name=None):
        """ Shows you the total time a user has used the timer for.

            :param name: The name (not the nick) of the person to check.
            Must use the name#discriminator format. If none is provided, it will
            check your own record.
        """
        if name is None:
            name = ctx.message.author

        time_str = printable_time(db_manager.get_user_total(name))
        if time_str is None:
            time_str = "None found."

        name = str(name)
        lib.log("{} queried for {}'s last session time. Result: {}"
                .format(lib.get_author_name(ctx, True),
                        "their" if name == str(ctx.message.author)
                        else (name + "'s"), time_str))

        await self.bot.say("```{}```".format(time_str),
                           delete_after=self.bot.ans_lifespan * 3)
utility.py 文件源码 项目:aryas 作者: lattkkthxbbye 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def in_channel(name):
    """
    Checks if command is sent in a given channel ignores high roles.
    To be used as a decorator
    :param name: name of the channel
    """
    def predicate(ctx):
        if ctx.message.channel.is_private:
            return False

        # Adapted from discord.ext.core.has_any_role
        user_roles = functools.partial(discordutils.get, ctx.message.author.roles)
        if any(user_roles(name=role) is not None for role in ['Admin', 'Moderator', 'Support']):
            return True

        return ctx.message.channel.name == name

    return commands.check(predicate)
discutils.py 文件源码 项目:cryopodbot 作者: TGWaffles 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def permcheck(**kwargs):
    def predicate(ctx):
        owners_ = kwargs.get('owners', [])
        check = kwargs.get('check', '')

        if type(owners_) is not list:
            owners_ = [owners_]

        if ctx.message.author.id in [owners[x] for x in owners_]:
            return True

        if check == 'dm_only' and ctx.message.server is None:
            return True

        return False

    return commands.check(predicate)
music.py 文件源码 项目:Bonfire 作者: Phxntxm 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def progress(self, ctx):
        """Provides the progress of the current song"""

        # Make sure we're playing first
        state = self.get_voice_state(ctx.message.server)
        if not state.is_playing():
            await self.bot.say('Not playing anything.')
        else:
            progress = state.current.progress
            length = state.current.length
            # Another check, just to make sure; this may happen for a very brief amount of time
            # Between when the song was requested, and still downloading to play
            if not progress or not length:
                await self.bot.say('Not playing anything.')
                return

            # Otherwise just format this nicely
            progress = divmod(round(progress, 0), 60)
            length = divmod(round(length, 0), 60)
            fmt = "Current song progress: {0[0]}m {0[1]}s/{1[0]}m {1[1]}s".format(progress, length)
            await self.bot.say(fmt)
music.py 文件源码 项目:Bonfire 作者: Phxntxm 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def summon(self, ctx):
        """Summons the bot to join your voice channel."""
        # This method will be invoked by other commands, so we should return True or False instead of just returning
        # First check if the author is even in a voice_channel
        summoned_channel = ctx.message.author.voice_channel
        if summoned_channel is None:
            await self.bot.say('You are not in a voice channel.')
            return False

        # Then simply create a voice client
        try:
            success = await self.create_voice_client(summoned_channel)
        except (asyncio.TimeoutError, discord.ConnectionClosed):
            await self.bot.say("I failed to connect! This usually happens if I don't have permission to join the"
                               " channel, but can sometimes be caused by your server region being far away."
                               " Otherwise this is an issue on Discord's end, causing the connect to timeout!")
            await self.remove_voice_client(summoned_channel.server)
            return False

        if success:
            try:
                await self.bot.say('Ready to play audio in ' + summoned_channel.name)
            except discord.Forbidden:
                pass
        return success
config.py 文件源码 项目:jose 作者: lnmds 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self, bot):
        super().__init__(bot)

        addr = getattr(bot.config, 'MONGO_LOC', None)
        self.mongo_client = motor.motor_asyncio.AsyncIOMotorClient(addr)
        self.bot.mongo = self.mongo_client

        self.jose_db = self.mongo_client['jose']

        self.config_coll = self.jose_db['config']
        self.block_coll = self.jose_db['block']
        self.bot.block_coll = self.block_coll

        # querying the db every time is not worth it
        self.config_cache = collections.defaultdict(dict)

        # used to check if cache has all defined objects in it
        self.default_keys = None
tags.py 文件源码 项目:PyMiki 作者: TheGrammarJew 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def can_use_box():
    def pred(ctx):
        if ctx.guild is None:
            return True
        if ctx.author.id == ctx.bot.owner_id:
            return True

        has_perms = ctx.channel.permissions_for(ctx.author).manage_messages
        if not has_perms:
            raise UnableToUseBox()

        return True
    return commands.check(pred)

# The tag data is heavily duplicated (denormalized) and heavily indexed to speed up
# retrieval at the expense of making inserts a little bit slower. This is a fine trade-off
# because tags are retrieved much more often than created.
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def is_owner():

    def predicate(ctx):
        if is_owner_check(ctx):
            return True
        else:
            raise errors.NotOwner

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def is_server_owner():

    def predicate(ctx):
        if is_server_owner_check(ctx):
            return True
        else:
            raise errors.NotServerOwner

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def is_voice_connected():

    def predicate(ctx):
        if not is_voice_connected_check(ctx):
            if is_server_owner_check(ctx) or utilities.get_permission(ctx, "join", id = ctx.message.author.id):
                raise errors.PermittedVoiceNotConnected
            else:
                raise errors.NotPermittedVoiceNotConnected
        return True

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def has_permissions(**permissions):

    def predicate(ctx):
        if has_permissions_check(ctx, permissions):
            return True
        else:
            raise errors.MissingPermissions

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def dm_or_has_permissions(**permissions):

    def predicate(ctx):
        if ctx.message.channel.is_private or has_permissions_check(ctx, permissions):
            return True
        else:
            raise errors.MissingPermissions

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def dm_or_has_capability(*permissions):

    def predicate(ctx):
        if ctx.message.channel.is_private or has_capability_check(ctx, permissions):
            return True
        else:
            raise errors.MissingCapability(permissions)

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def has_permissions_and_capability(**permissions):

    def predicate(ctx):
        if ctx.message.channel.is_private:
            return False
        elif not has_permissions_check(ctx, permissions):
            raise errors.MissingPermissions
        elif not has_capability_check(ctx, permissions.keys()):
            raise errors.MissingCapability(permissions.keys())
        else:
            return True

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def dm_or_has_permissions_and_capability(**permissions):

    def predicate(ctx):
        if ctx.message.channel.is_private:
            return True
        elif not has_permissions_check(ctx, permissions):
            raise errors.MissingPermissions
        elif not has_capability_check(ctx, permissions.keys()):
            raise errors.MissingCapability(permissions.keys())
        else:
            return True

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def not_forbidden():

    def predicate(ctx):
        if not_forbidden_check(ctx):
            return True
        else:
            raise errors.NotPermitted

    return commands.check(predicate)
checks.py 文件源码 项目:Harmonbot 作者: Harmon758 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def is_permitted():

    def predicate(ctx):
        if is_permitted_check(ctx):
            return True
        else:
            raise errors.NotPermitted

    return commands.check(predicate)
checks.py 文件源码 项目:youtube 作者: FishyFing 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def is_owner():
    return commands.check(is_owner_check)

# The permission system of the bot is based on a "just works" basis
# You have permissions and the bot has permissions. If you meet the permissions
# required to execute the command (and the bot does as well) then it goes through
# and you can execute the command.
# If these checks fail, then there are two fallbacks.
# A role with the name of Bot Mod and a role with the name of Bot Admin.
# Having these roles provides you access to certain commands without actually having
# the permissions required for them.
# Of course, the owner will always be able to execute commands.
checks.py 文件源码 项目:youtube 作者: FishyFing 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def role_or_permissions(ctx, check, **perms):
    if check_permissions(ctx, perms):
        return True

    ch = ctx.message.channel
    author = ctx.message.author
    if ch.is_private:
        return False # can't have roles in PMs

    role = discord.utils.find(check, author.roles)
    return role is not None
checks.py 文件源码 项目:youtube 作者: FishyFing 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def serverowner_or_permissions(**perms):
    def predicate(ctx):
        if ctx.message.guild is None:
            return False
        guild = ctx.message.guild
        owner = guild.owner

        if ctx.message.author.id == owner.id:
            return True

        return check_permissions(ctx,perms)
    return commands.check(predicate)
checks.py 文件源码 项目:Ruby-Bot 作者: ahuei123456 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def is_owner():
    return commands.check(lambda ctx: is_owner_check(ctx.message))
permissions.py 文件源码 项目:Dwarf 作者: Dwarf-Community 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def owner():
    return commands.check(is_owner_check)
permissions.py 文件源码 项目:Dwarf 作者: Dwarf-Community 项目源码 文件源码 阅读 51 收藏 0 点赞 0 评论 0
def admin():
    return commands.check(is_admin_check)
permissions.py 文件源码 项目:Dwarf 作者: Dwarf-Community 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def serverowner():
    def predicate(ctx):
        if ctx.message.server is None:
            return False

        if ctx.message.author.id == ctx.message.server.owner.id:
            return True

        # return check_permissions(ctx, perms)
        return False
    return commands.check(predicate)
checks.py 文件源码 项目:lagbot 作者: mikevb1 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def need_db(command):
    """Decorator, not check, to mark the command as needing a DB connection."""
    command._db = True
    return command
checks.py 文件源码 项目:lagbot 作者: mikevb1 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def config_attr(attr):
    return commands.check(lambda _: getattr(config, attr, None) is not None)
checks.py 文件源码 项目:lagbot 作者: mikevb1 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def owner_or_permissions(**perms):
    return commands.check(lambda ctx: check_permissions(ctx, perms))


问题


面经


文章

微信
公众号

扫码关注公众号