chatroom quiz bot:如何在空闲时间重复测验?

时间:2016-12-27 15:08:30

标签: python python-2.7 python-3.x

我正在研究Python中的quit bot。现在我想知道如何在一段空闲时间之后重复这个问题。

这些是我的全局变量:

QUIZ_FILE = 'static/questions.txt'
QUIZ_TOTAL_LINES = 29
QUIZ_TIME_LIMIT = 40
QUIZ_IDLE_LIMIT = 3000000
QUIZ_RECURSIVE_MAX = 3000000
QUIZ_CURRENT_ANSWER = {}
QUIZ_CURRENT_HINT = {}
QUIZ_CURRENT_HINT_NEW = {}
QUIZ_CURRENT_TIME = {}
QUIZ_IDLENESS = {}
QUIZ_IDLE_ANSWER = {}
QUIZ_START = {}
QUIZ_IDLE_ANSWER_FIRSR = {}
QUIZ_NOWORD = '*'
MODE = 'M1'
PTS = 'P2'
ACC = 'A2'

Bot时间设置

import threading

HELP = u'help of command > "!quiz"'

def sectomin(time):
    m = 0
    s = 0
    if time >= 60:
        m = time / 60
        if (m * 60) != 0:
            s = time - (m * 60)
        else:
            s = 0
    else:
        m = 0
        s = time
    return str(m)+u'min. in '+str(s)+u'sec.'

def quiz_timer(groupchat, start_time):
    global QUIZ_TIME_LIMIT
    global QUIZ_CURRENT_TIME

time.sleep(QUIZ_TIME_LIMIT)
if QUIZ_CURRENT_TIME.has_key(groupchat) and QUIZ_CURRENT_ANSWER.has_key(groupchat) and start_time == QUIZ_CURRENT_TIME[groupchat]:
    QUIZ_CURRENT_ANSWER[groupchat]
    msg(groupchat, u'(!) time out! ' + sectomin(QUIZ_TIME_LIMIT) + u' passed.\nCorrect answer: ' + QUIZ_CURRENT_ANSWER[groupchat])
    if QUIZ_IDLENESS.has_key(groupchat):
        QUIZ_IDLENESS[groupchat] += 1
    else:
        QUIZ_IDLENESS[groupchat] = 1
    if QUIZ_IDLENESS[groupchat] >= QUIZ_IDLE_LIMIT:
        msg(groupchat, u'(!) quiz will be automatically completed for inaction! ' + str(QUIZ_IDLE_LIMIT) + ' unanswered questions.')
        del QUIZ_CURRENT_ANSWER[groupchat]
        quiz_list_scores(groupchat)
    else:
        quiz_ask_question(groupchat)

提问的方法

def quiz_ask_question(groupchat):
    global answer
    global QUIZ_CURRENT_TIME
    global question
    global QUIZ_IDLE_ANSWER
    global QUIZ_IDLE_ANSWER_FIRSR
    QUIZ_IDLE_ANSWER = {groupchat:{}}
(question, answer) = quiz_new_question()
QUIZ_CURRENT_ANSWER[groupchat] = answer
QUIZ_CURRENT_HINT[groupchat] = None
QUIZ_CURRENT_HINT_NEW[groupchat] = None
QUIZ_CURRENT_TIME[groupchat] = time.time()
threading.Thread(None, quiz_timer, 'gch'+str(random.randrange(0,9999)), (groupchat, QUIZ_CURRENT_TIME[groupchat])).start()
msg(groupchat, u'(?) question: \n' + question)

我希望在睡眠/闲暇时间内每隔几秒自动回忆一下这个问题。

我无法让它工作,因此机器人可以自动在QUIZ_TIME_LIMIT = 40之间提问。

根据要求回复问题的代码

def handler_quiz_resend(type, source, body):
    global question
    groupchat = source[1]
    if QUIZ_CURRENT_ANSWER.has_key(groupchat):
        res = u'(*) current question: \n'+question
        reply(type, source, res)
    else:
        reply(type, source, u'no quiz, '+HELP)

0 个答案:

没有答案