基于Nonebot框架的QQ机器人词库生成器

基于Nonebot框架的QQ机器人词库生成器

废话不说直接上代码~~


ck_list = []    
successful_num = 0

# 导入词库
raw_file = str(input("请输入词库文件路径:"))
with open(raw_file, 'r', encoding='utf-8') as fp:
    raw_ck_list = fp.readlines() # 获取文件内容
    for line in raw_ck_list:
        ck_list.append(line.strip())    # 去掉首尾空格

moudle = str(input("请输入写入模式(a追加 w覆盖):"))
# 创建ciku.py文件并写入代码
with open("ciku.py", moudle, encoding='utf-8') as fp:
    # 判断模式
    if moudle == 'w':
        content = 'from nonebot import on_keyword\n' \
                  'from nonebot.typing import T_State\n' \
                  'from nonebot.adapters.cqhttp import Message, Bot, Event, MessageSegment\n' \
                  'import random\n'
        fp.write(content)
    for index, raw_ck in enumerate(ck_list):
        try:

            kw = '{'
            rp = ''
            fc = ''

            ls = raw_ck.split("-")  # 将未处理的词库通过-分割成一个len为3的列表

            name = str(ls[0])
            kw_ls = ls[1].split("^")    # 将item通过^分割为列表
            rp_ls = ls[2].split("^")
            if ls[3]:   # 判断是否输入CQ码
                fc_ls = ls[3].split("^")
            else:
                fc_ls = []  # false

            for item in kw_ls:
                if item != kw_ls[-1]:
                    kw = kw + '"' + item + '", '
                else:
                    kw = kw + '"' + item + '"}'
            # print(kw)

            if fc_ls:   # 含有cq码
                content = '\n\n########################################################################\n' \
                      + name + '= on_keyword(' + kw + ')\n' \
                      '@' + name + '.handle()\n' \
                      'async def ' + name + '_handle(bot: Bot, event: Event, state: T_State):\n\t' \
                      'id = str(event.get_user_id())\n\t' \
                      'fc_ls = ' + str(fc_ls) + '\n\t' \
                      'rp_ls = ' + str(rp_ls) + '\n\t' \
                      'face = [{"type": "face","data": {"id": random.choice(fc_ls)}}]\n\t' \
                      'rp = random.choice(rp_ls)\n\t' \
                      'await ' + name + '.send(MessageSegment.at(id) + face + rp)\n'
            else:
                content = '\n\n########################################################################\n' \
                      + name + '= on_keyword(' + kw + ')\n' \
                      '@' + name + '.handle()\n' \
                      'async def ' + name + '_handle(bot: Bot, event: Event, state: T_State):\n\t' \
                      'id = str(event.get_user_id())\n\t' \
                      'rp_ls = ' + str(rp_ls) + '\n\t' \
                      'rp = random.choice(rp_ls)\n\t' \
                      'await ' + name + '.send(MessageSegment.at(id) + rp)\n'

            # 写入代码
            fp.write(content)
            print(f"\033[0;32;40m {index+1}.{name}词库写入成功!\033[0m")
            successful_num += 1
        except:
            print(f"\033[1;31;40m Failed:{index + 1}.{name}词库写入失败...www\033[0m")

    print(f"\033[0;36;40m 词库写入完成!共{len(ck_list)}条!成功写入{successful_num}条! \033[0m")



食用方法:
基于Nonebot框架的QQ机器人词库生成器_第1张图片

2021-8-26

你可能感兴趣的:(鸡器人,python,聊天机器人,qq)