当前位置: 首页>>代码示例>>Python>>正文


Python UtilBot.get_last_recorded方法代码示例

本文整理汇总了Python中Core.Util.UtilBot.get_last_recorded方法的典型用法代码示例。如果您正苦于以下问题:Python UtilBot.get_last_recorded方法的具体用法?Python UtilBot.get_last_recorded怎么用?Python UtilBot.get_last_recorded使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Core.Util.UtilBot的用法示例。


在下文中一共展示了UtilBot.get_last_recorded方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: record

# 需要导入模块: from Core.Util import UtilBot [as 别名]
# 或者: from Core.Util.UtilBot import get_last_recorded [as 别名]
def record(bot, event, *args):
    """
    **Record:**
    Usage: /record <text to record>
    Usage: /record date <date to show records from>
    Usage: /record list
    Usage: /record search <search term>
    Usage: /record strike
    Usage: /record
    Purpose: Store/Show records of conversations. Note: All records will be prepended by: "On the day of <date>," automatically.
    """

    import datetime

    directory = "Records" + os.sep + str(event.conv_id)
    if not os.path.exists(directory):
        os.makedirs(directory)
    filename = str(datetime.date.today()) + ".txt"
    filepath = os.path.join(directory, filename)
    file = None

    # Deletes the record for the day.
    if ''.join(args) == "clear":
        file = open(filepath, "a+")
        file.seek(0)
        file.truncate()

    # Shows the record for the day.
    elif ''.join(args) == '':
        file = open(filepath, "a+")
        # If the mode is r+, it won't create the file. If it's a+, I have to seek to the beginning.
        file.seek(0)
        segments = [hangups.ChatMessageSegment(
            'On the day of ' + datetime.date.today().strftime('%B %d, %Y') + ':', is_bold=True),
                    hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK)]
        for line in file:
            segments.append(
                hangups.ChatMessageSegment(line))
            segments.append(hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK))
            segments.append(hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK))
        bot.send_message_segments(event.conv, segments)

    # Removes the last line recorded, iff the user striking is the same as the person who recorded last.
    elif args[0] == "strike":
        last_recorder = UtilBot.get_last_recorder(event.conv_id)
        last_recorded = UtilBot.get_last_recorded(event.conv_id)
        if event.user.id_ == last_recorder:
            file = open(filepath, "a+")
            file.seek(0)
            file_lines = file.readlines()
            if last_recorded is not None and last_recorded in file_lines:
                file_lines.remove(last_recorded)
            file.seek(0)
            file.truncate()
            file.writelines(file_lines)
            UtilBot.set_last_recorded(event.conv_id, None)
            UtilBot.set_last_recorder(event.conv_id, None)
        else:
            bot.send_message(event.conv, "You do not have the authority to strike from the Record.")

    # Lists every record available. TODO Paginate this?
    elif args[0] == "list":
        files = os.listdir(directory)
        segments = []
        for name in files:
            segments.append(hangups.ChatMessageSegment(name.replace(".txt", "")))
            segments.append(hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK))
        bot.send_message_segments(event.conv, segments)

    # Shows a list of records that match the search criteria.
    elif args[0] == "search":
        args = args[1:]
        searched_term = ' '.join(args)
        escaped_args = []
        for item in args:
            escaped_args.append(re.escape(item))
        term = '.*'.join(escaped_args)
        term = term.replace(' ', '.*')
        if len(args) > 1:
            term = '.*' + term
        else:
            term = '.*' + term + '.*'
        foundin = []
        for name in glob.glob(directory + os.sep + '*.txt'):
            with open(name) as f:
                contents = f.read()
            if re.match(term, contents, re.IGNORECASE | re.DOTALL):
                foundin.append(name.replace(directory, "").replace(".txt", "").replace("\\", ""))
        if len(foundin) > 0:
            segments = [hangups.ChatMessageSegment("Found "),
                        hangups.ChatMessageSegment(searched_term, is_bold=True),
                        hangups.ChatMessageSegment(" in:"),
                        hangups.ChatMessageSegment("\n", hangups.SegmentType.LINE_BREAK)]
            for filename in foundin:
                segments.append(hangups.ChatMessageSegment(filename))
                segments.append(hangups.ChatMessageSegment("\n", hangups.SegmentType.LINE_BREAK))
            bot.send_message_segments(event.conv, segments)
        else:
            segments = [hangups.ChatMessageSegment("Couldn't find  "),
                        hangups.ChatMessageSegment(searched_term, is_bold=True),
#.........这里部分代码省略.........
开发者ID:Der-Eddy,项目名称:HangoutsBot,代码行数:103,代码来源:ExtraCommands.py


注:本文中的Core.Util.UtilBot.get_last_recorded方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。