playlist.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:ddmbot 作者: Budovi 项目源码 文件源码
def playlist(self, ctx, subcommand: str, *arguments: str):
        # TODO: CHECK FOR THE PLAYLIST NAME
        if not arguments:
            raise dec.UserInputError('Command *playlist* has no subcommand named {}. Please use `{}help playlist` to '
                                     'list all the available subcommands.'.format(subcommand, ctx.prefix))
        # will try to treat subcommand as a playlist name from now on
        playlist_name = subcommand
        if not await self._db.exists(int(ctx.message.author.id), playlist_name):
            raise dec.UserInputError('Command *playlist* has no subcommand named {0}, nor is it a name of your '
                                     'playlist. Please use `{1}help playlist` to list all the available subcommands '
                                     'or `{1}playlist list` to list all your playlists.'
                                     .format(playlist_name, ctx.prefix))

        # now we try to execute a subcommand depending on the input
        #   arguments[0] == subcommand name without postfix
        #   playlist_name + arguments[1:] == subcommand arguments

        # two-step approach -- tackling aliases
        subcommand = ctx.command.get_command(arguments[0])
        if subcommand is None:
            raise dec.UserInputError('Command *playlist* has no subcommand named {}. Please use `{}help playlist` to '
                                     'list all the available subcommands.'.format(arguments[0], ctx.prefix))
        subcommand = subcommand.name
        # now try to call explicit version
        subcommand = ctx.command.get_command('{}_explicit'.format(subcommand))
        if subcommand is None:
            raise dec.UserInputError('The subcommand {} does not support optional playlist specification. Please use '
                                     '`{}help playlist` to list all the available subcommands and their arguments.'
                                     .format(arguments[0], self._bot.config['ddmbot']['delimiter']))

        # replace the string view with the swapped one and invoke the correct subcommand
        swapped_command = '{}{} {} {}'.format(ctx.prefix, ctx.invoked_with, subcommand, playlist_name)
        if arguments[1:]:
            swapped_command += ' ' + ' '.join('"{}"'.format(arg) for arg in arguments[1:])
        ctx.view = decw.StringView(swapped_command)
        ctx.view.index = len(ctx.prefix) + len(ctx.invoked_with) + len(str(subcommand)) + 1
        ctx.view.previous = ctx.view.index
        ctx.invoked_with = arguments[0]

        # now invoke
        return await subcommand.invoke(ctx)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号