twitch.py 文件源码

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

项目:Bonfire 作者: Phxntxm 项目源码 文件源码
def add_twitch_url(self, ctx, url: str):
        """Saves your user's twitch URL

        EXAMPLE: !twitch add MyTwitchName
        RESULT: Saves your twitch URL; notifications will be sent to this server when you go live"""
        await ctx.message.channel.trigger_typing()

        # This uses a lookbehind to check if twitch.tv exists in the url given
        # If it does, it matches twitch.tv/user and sets the url as that
        # Then (in the else) add https://www. to that
        # Otherwise if it doesn't match, we'll hit an AttributeError due to .group(0)
        # This means that the url was just given as a user (or something complete invalid)
        # So set URL as https://www.twitch.tv/[url]
        # Even if this was invalid such as https://www.twitch.tv/google.com/
        # For example, our next check handles that
        try:
            url = re.search("((?<=://)?twitch.tv/)+(.*)", url).group(0)
        except AttributeError:
            url = "https://www.twitch.tv/{}".format(url)
        else:
            url = "https://www.{}".format(url)

        # Try to find the channel provided, we'll get a 404 response if it does not exist
        status = await utils.request(url, attr='status')
        if not status == 200:
            await self.bot.say("That twitch user does not exist! "
                           "What would be the point of adding a nonexistant twitch user? Silly")
            return

        key = ctx.message.author.id
        entry = {'twitch_url': url,
                 'servers': [ctx.message.server.id],
                 'notifications_on': 1,
                 'live': 0,
                 'member_id': key}
        update = {'twitch_url': url}

        # Check to see if this user has already saved a twitch URL
        # If they have, update the URL, otherwise create a new entry
        # Assuming they're not live, and notifications should be on
        if not await utils.add_content('twitch', entry):
            await utils.update_content('twitch', update, key)
        await self.bot.say("I have just saved your twitch url {}".format(ctx.message.author.mention))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号