當前位置: 首頁>>代碼示例>>Python>>正文


Python UtilBot.set_last_recorder方法代碼示例

本文整理匯總了Python中Core.Util.UtilBot.set_last_recorder方法的典型用法代碼示例。如果您正苦於以下問題:Python UtilBot.set_last_recorder方法的具體用法?Python UtilBot.set_last_recorder怎麽用?Python UtilBot.set_last_recorder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Core.Util.UtilBot的用法示例。


在下文中一共展示了UtilBot.set_last_recorder方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: record

# 需要導入模塊: from Core.Util import UtilBot [as 別名]
# 或者: from Core.Util.UtilBot import set_last_recorder [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.set_last_recorder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。