上大学时为了能在群里背单词写的

from qqbot import _bot as bot
from qqbot import QQBotSlot as qqbotslot,RunBot
import random
import _thread
import time
import string
import re
import threading
import math

bot.Login(['-q','xxx'])

bot.Update('buddy')
bot.Update('group')



class Cons:
    
    Count=0
    def __init__(self):
        Cons.Count+=1

    def getCount(self):
        return str(Cons.Count)

class LockManager:
    lock=0
    lock1=0
    
    def createLock(self):
        LockManager.lock=threading.Lock()
        return True

    def acquireLock(self):
        return LockManager.lock.acquire()

    def releaseLock(self):
        return LockManager.lock.release()

    def createLock1(self):
        LockManager.lock1=threading.Lock()
        return True

    def acquireLock1(self):
        return LockManager.lock1.acquire()

    def releaseLock1(self):
        return LockManager.lock1.release()

    
class questionManager:

    cap=0 #总容量/每天
    max_cap=1000
    state_cleanManager=1   #1为关闭,2为开启
    
    max_wait_question_count=5
    waitting_question_count=0
    
    questions=[]          #{'id':'','question':'','answer':'',time:0,'contact':contact}

    def searchID(self,ids):
        indexs=0
        for question in questionManager.questions:
            if question.get('id')!=ids:
                indexs+=1
                continue
            else:
                return indexs

        return "NOT"

    def putID(self,ids,contact):
        if self.searchID(ids)!="NOT":
            return "in"
        if questionManager.waitting_question_count>=questionManager.max_wait_question_count:
            return "full"
        tmp_question={'id':ids,'question':'','answer':'','time':time.time(),'contact':contact}
        questionManager.questions.append(tmp_question)

        questionManager.waitting_question_count+=1
        return True

    def putQuestion(self,ids,question=''):
        id_index_in_questions=self.searchID(ids)
        if id_index_in_questions==-1:
            return "notin"
        questionManager.questions[id_index_in_questions]['question']=question
        return True

    def putAnswer(self,contact,ids,answer):
        id_index_in_questions=self.searchID(ids)
        
        if id_index_in_questions=="NOT":
            return "NOT"
        #对answer进行核对
        tmp_question=questionManager.questions[id_index_in_questions]['question']
        if tmp_question=='':
            return 'noques'
        
        word_list=re.split('\|',answer)
        tmp_words=""
        
        if len(word_list)<=1:
            return "orign"
        
        for word in word_list:
            #调试用
            bot.SendTo(contact,word)
            if word in tmp_question:
                tmp_words=tmp_words+word
                continue
            else:
                return "nomatch"

        if len(tmp_words)!=len(tmp_question):
            return "nomatch"
        
        questionManager.questions[id_index_in_questions]['answer']=answer

        return id_index_in_questions
        
    def delQuestionById(self,ids):
        id_index_in_questions=self.searchID(ids)
        if id_index_in_questions=="NOT":
            return True
        del questionManager.questions[id_index_in_questions]
        questionManager.waitting_question_count-=1
        return True

    def getQuestionDataByIndex(self,index,name):
        return str(questionManager.questions[index][name])

    def getQuestions(self):
        return questionManager.questions

    def changeCleanManagerState(slef,state):
        questionManager.state_cleanManager=state
        return True

    def getCleanManagerState(self):
        return questionManager.state_cleanManager
    
    def solved(self):
        questionManager.cap+=1

    def getHugRate(self):
       return round(questionManager.cap/questionManager.max_cap,3)


class wordManager:
    words_list=[]

    def add_words_list(self):
        fo=open('D:\开发项目\智能机器人开发\wordlist.txt','r',encoding='utf-8',errors="ignore")
        wordManager.words_list=fo.readlines()
        fo.close()
        return True

    def searchWord(self,contact,word):
        words_count=len(wordManager.words_list)
        searchMan_count=4
        average_searchMan_task=math.ceil(words_count/searchMan_count)
        
        def searchMan(manID,contact,start,end,word):
            have_finded=0
            for num in range(start,end):
                if word in wordManager.words_list[num]:
                    have_finded=1
                    bot.SendTo(contact,"搜索引擎:"+str(manID)+"号:"+wordManager.words_list[num])
            if have_finded!=1:
                bot.SendTo(contact,"搜索引擎"+str(manID)+"号:没有找到关于["+word+"]的相关信息")
                

        if wordManager.words_list==None:
            bot.SendTo(contact,"字典还未初始化")
        else:
            searchMan_task=[]
            given_task=0
            left_task=words_count
            #分发搜索任务
            for i in range(1,searchMan_count+1):
                if average_searchMan_task>left_task:
                     _thread.start_new_thread(searchMan,(i,contact,given_task,left_task-1,word))
                     given_task+=left_task
                     left_task-=left_task
                else:
                    _thread.start_new_thread(searchMan,(i,contact,given_task,average_searchMan_task-1,word))
                    given_task+=average_searchMan_task
                    left_task-=average_searchMan_task
                    
            

def dealQuestion(contact,ids,question):
    #加锁操作
    lock=LockManager()
    
    if lock.acquireLock():
        try:
            qmanager=questionManager()
            code_putID=qmanager.putID(ids,contact)
            if code_putID=="in":
                bot.SendTo(contact,"主人:"+ids+",人家要吃菜嘛,你不是刚才给我喂过饭了没")
                _thread.exit()
            elif code_putID=="full":
                botSendTo(contact,"哼!主人:"+ids+",想撑死奴家么")
                _thread.exit()

            qmanager.putQuestion(ids,question)
            bot.SendTo(contact,"主人:"+ids+",奴家吃好饭了")
        finally:
            lock.releaseLock()
            
    
def dealAnswer(contact,ids,answer):
    #加锁操作
    lock=LockManager()

    if lock.acquireLock():
        try:
            qmanager=questionManager()
            code_putAnswer=qmanager.putAnswer(contact,ids,answer)
            if code_putAnswer=="NOT":
                bot.SendTo(contact,"主人:"+ids+",得先喂奴家饭后才能喂菜哦,么么哒^_^")
                _thread.exit()
            elif code_putAnswer=="noques":
                bot.SendTo(contact,"哼!主人:"+ids+",你竟然欺骗奴家,哼!")
                qmanager.delQuestionById(ids)
                _thread.exit()
            elif code_putAnswer=="orign":
                bot.SendTo(contact,"主人:"+ids+",可以帮奴家分下词吗")
                _thread.exit()
            elif code_putAnswer=="nomatch":
                bot.SendTo(contact,"主人:"+ids+",奴家觉得这菜和饭不匹配呢")
                qmanager.delQuestionById(ids)
                _thread.exit()
        
            #对answer进行核对操作
            qmanager.solved()
            fo=open("word.txt","a+")
            fo.write(qmanager.getQuestionDataByIndex(code_putAnswer,'id')+'\t')
            fo.write(qmanager.getQuestionDataByIndex(code_putAnswer,'question')+'\t')
            fo.write(qmanager.getQuestionDataByIndex(code_putAnswer,'answer')+'\n')
            fo.close()
            bot.SendTo(contact,"主人:"+ids+",奴家奖励你个么么哒")
            bot.SendTo(contact,"奴家的饱度:"+str(qmanager.getHugRate()))
            qmanager.delQuestionById(ids)
    
        finally:
            lock.releaseLock()
    

def cleanManager(contact,ids):
    owner_contact=contact
    questionmanager=questionManager()
    waitting_time=100
    tmp_time=0
    lock=LockManager()
    #得到本问题nickname的contact
    while 1:
        if questionmanager.getCleanManagerState()==1:
            bot.SendTo(contact,"主人:"+ids+",已经关闭问题监视程序了")
            _thread.exit()
        tmp_time=time.time()
        for question in questionmanager.getQuestions():

            if tmp_time-question['time']>waitting_time:
                 if lock.acquireLock():
                     try:
                         questionmanager.delQuestionById(question['id'])
                         bot.SendTo(question['contact'],"主人:"+question['id']+",哼!喂了奴家饭就不给喂菜的吗?哼!:(")
                     finally:
                         lock.releaseLock()
        time.sleep(2)
    

@qqbotslot

def onQQMessage(bot,contact,member,content):
    
    nickName=contact.nick
    cmd_eat="吃饭:"
    cmd_eat1='吃菜:'
    cmd_search_word="查找单词:"
    cmd_start_cleanManager="start129245start"
    cmd_stop_cleanManager="stop129245stop"
    
    if content=="hello":
        #_thread.start_new_thread(pros,(contact,))
        #bot.SendTo(contact,content)
        bot.SendTo(contact,"主人你好,我是你的女仆:小狐狸")
        
            
    elif content=="-stop":
        bot.SendTo(contact,"亲爱的主人再见了,爱你哦,么么哒")
        bot.Stop()

    elif content==cmd_start_cleanManager:
        questionmanager1=questionManager()
        lock1=LockManager()
        #检查命令是否重复
        if questionmanager1.getCleanManagerState()==2:
            bot.SendTo(conatct,"主人:"+nickName+",你已经开启了检测程序了")
        else:
            if lock1.acquireLock1():
                try:
                    questionmanager1.changeCleanManagerState(2) 
                finally:
                    lock1.releaseLock1()
        
            _thread.start_new_thread(cleanManager,(contact,nickName))
            bot.SendTo(contact,"主人:"+nickName+",已开启问题检测程序")
        

    elif content==cmd_stop_cleanManager:
        #停止检测程序
        questionmanager2=questionManager()
        lock2=LockManager()
        #检查命令是否重复
        if questionmanager2.getCleanManagerState()==1:
            bot.SendTo(contact,"主人:"+nickName+",你已经关闭了检测程序了")
        else:
            if lock2.acquireLock1():
                try:
                    questionmanager2.changeCleanManagerState(1)
                finally:
                    lock2.releaseLock1()
            
    
    elif '@ME' in content:

        tmp_index_in_word=content.find(cmd_eat,6,9)
        #cmd_index_in_word=tmp_index_in_word!=-1 ? tmp_index_in_word : content.find(cmd_eat,7,10)
        if tmp_index_in_word!=-1:
            cmd_index_in_word=tmp_index_in_word
        else:
            cmd_index_in_word=content.find(cmd_eat,7,10)

        if cmd_index_in_word!=-1:
            if len(content)>30:
                bot.SendTo(contact,"主人:"+member.nick+",小狐狸一下吃不下这么多")

        if cmd_index_in_word==-1:
            #处理单词搜索相关的
            wordmanager=wordManager()
            tmp_index_in_word=content.find(cmd_eat1,6,9)
            cmd_index_in_word
            #cmd_index_in_word=tmp_index_in_word!=-1 ? tmp_index_in_word : content.find(cmd_eat1,7,10)
            if tmp_index_in_word!=-1:
                cmd_index_in_word=tmp_index_in_word
            else:
                cmd_index_in_word=content.find(cmd_eat1,7,10)

            if cmd_index_in_word==-1:
                index_cmd_search_word=-1
                tmp_index_cmd_search_word=content.find(cmd_search_word,6,11)
                if tmp_index_cmd_search_word!=-1:
                    index_cmd_search_word=tmp_index_cmd_search_word
                else:
                    index_cmd_search_word=content.find(cmd_search_word,7,12)

                if index_cmd_search_word!=-1:
                    if len(content)>20:
                        bot.SendTo(contact,"超出字数限制")
                    else:
                        bot.SendTo(contact,"正在查询当中。。。")
                        tmp_word=content[index_cmd_search_word+5:]
                        wordmanager.searchWord(contact,tmp_word)
                else:
                    #words=('讨厌吗,人家现在有急事呢','@我干啥!滚一边去','想人家了?','奴家要睡觉,别@奴家','讨厌,走开嘛','奴家现在还不会说话哦')
                    #word=random.choice(words)
                    word="小狐狸快饿死了!!!求求各位小哥哥和小姐姐们喂我吃饭哦。喂饭方法:首先,艾特我吃饭:(一个小句子)然后,艾特我吃菜:(给吃饭后的那个小句子分下词)整个喂饭过程就结束啦!!!例子:首先 艾特我吃饭:我是小狐狸,然后 艾特我吃菜:我|是|小狐狸最后小狐狸会送个么么哒的哦!!!在此再次向各位小哥哥和小姐姐们表示感谢呢!!!"
                    bot.SendTo(contact,word)
            else:
                tmp_id=member.nick
                tmp_answer=content[cmd_index_in_word+3:]
                _thread.start_new_thread(dealAnswer,(contact,tmp_id,tmp_answer))

        else:
           tmp_id=member.nick
           tmp_question=content[cmd_index_in_word+3:]
           _thread.start_new_thread(dealQuestion,(contact,tmp_id,tmp_question))

if __name__=="__main__":
    #配备线程锁
    lockmanager=LockManager()
    wordmanager=wordManager()
    lockmanager.createLock()
    lockmanager.createLock1()
    wordmanager.add_words_list()
    RunBot()

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值