tags.py 文件源码

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

项目:Bonfire 作者: Phxntxm 项目源码 文件源码
def add_tag(self, ctx, *, result: str):
        """Use this to add a new tag that can be used in this server
        Format to add a tag is !tag add <tag> - <result>

        EXAMPLE: !tag add this is my new tag - This is what it will be
        RESULT: A tag that can be called by '!tag this is my new tag' and will output 'This is what it will be'"""
        try:
            # Use regex to get the matche for everything before and after a -
            match = re.search("(.*) - (.*)", result)
            tag = match.group(1).strip()
            tag_result = match.group(2).strip()
        # Next two checks are just to ensure there was a valid match found
        except AttributeError:
            await self.bot.say(
                "Please provide the format for the tag in: {}tag add <tag> - <result>".format(ctx.prefix))
            return
        # If our regex failed to find the content (aka they provided the wrong format)
        if len(tag) == 0 or len(tag_result) == 0:
            await self.bot.say(
                "Please provide the format for the tag in: {}tag add <tag> - <result>".format(ctx.prefix))
            return

        # Make sure the tag created does not mention everyone/here
        if '@everyone' in tag_result or '@here' in tag_result:
            await self.bot.say("You cannot create a tag that mentions everyone!")
            return
        entry = {'server_id': ctx.message.server.id, 'tag': tag, 'result': tag_result}
        r_filter = lambda row: (row['server_id'] == ctx.message.server.id) & (row['tag'] == tag)
        # Try to create new entry first, if that fails (it already exists) then we update it
        if not await config.update_content('tags', entry, r_filter):
            await config.add_content('tags', entry)
        await self.bot.say(
            "I have just updated the tag `{0}`! You can call this tag by entering !tag {0}".format(tag))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号