重构代码结构

This commit is contained in:
SlyAimer 2025-01-14 18:57:59 +08:00
parent 9d715f2fb7
commit 6bd163a15b
105 changed files with 206 additions and 152 deletions

2
bot.py
View file

@ -1,6 +1,6 @@
import nonebot import nonebot
from nonebot.adapters.qq import Adapter as QQAdapter from nonebot.adapters.qq import Adapter as QQAdapter
from src.qq_plugins.data_init import data_init # 导入QQ插件 from src.my_sqlite.data_init import data_init
nonebot.init() nonebot.init()
driver = nonebot.get_driver() driver = nonebot.get_driver()

View file

Before

Width:  |  Height:  |  Size: 3.4 MiB

After

Width:  |  Height:  |  Size: 3.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View file

Before

Width:  |  Height:  |  Size: 799 KiB

After

Width:  |  Height:  |  Size: 799 KiB

View file

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View file

Before

Width:  |  Height:  |  Size: 386 KiB

After

Width:  |  Height:  |  Size: 386 KiB

View file

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View file

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View file

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

Before

Width:  |  Height:  |  Size: 686 KiB

After

Width:  |  Height:  |  Size: 686 KiB

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View file

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

View file

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View file

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View file

Before

Width:  |  Height:  |  Size: 1.8 MiB

After

Width:  |  Height:  |  Size: 1.8 MiB

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View file

Before

Width:  |  Height:  |  Size: 409 KiB

After

Width:  |  Height:  |  Size: 409 KiB

View file

Before

Width:  |  Height:  |  Size: 734 KiB

After

Width:  |  Height:  |  Size: 734 KiB

View file

Before

Width:  |  Height:  |  Size: 913 KiB

After

Width:  |  Height:  |  Size: 913 KiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View file

Before

Width:  |  Height:  |  Size: 765 KiB

After

Width:  |  Height:  |  Size: 765 KiB

View file

Before

Width:  |  Height:  |  Size: 806 KiB

After

Width:  |  Height:  |  Size: 806 KiB

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View file

Before

Width:  |  Height:  |  Size: 891 KiB

After

Width:  |  Height:  |  Size: 891 KiB

View file

@ -1,5 +1,7 @@
image: image:
image_local_path: "src/img_resource/MaoYuNa" app_id: "<KEY>"
image_local_qq_image_path: "src/image/qq_image"
image_local_path: "src/image/MaoYuNa"
#SMMS图床相关配置 #SMMS图床相关配置
smms_token: "<KEY>" # sm.ms图床的token smms_token: "<KEY>" # sm.ms图床的token
smms_image_upload_history: "https://sm.ms/api/v2/upload_history" # sm.ms图床获取上传图片历史API地址 smms_image_upload_history: "https://sm.ms/api/v2/upload_history" # sm.ms图床获取上传图片历史API地址

View file

@ -4,13 +4,15 @@ import random
import requests import requests
with open(os.getcwd() +'/src/common_plugins/config/common.yaml', 'r', encoding='utf-8') as f: with open(os.getcwd() +'/src/image/config/image.yaml', 'r', encoding='utf-8') as f:
image = yaml.load(f.read(), Loader=yaml.FullLoader).get('image') image = yaml.load(f.read(), Loader=yaml.FullLoader).get('image')
image_local_path = image.get('image_local_path') image_local_path = image.get('image_local_path')
image_local_qq_image_path = image.get('image_local_qq_image_path')
smms_token = image.get('smms_token') smms_token = image.get('smms_token')
smms_image_upload_history = image.get('smms_image_upload_history') smms_image_upload_history = image.get('smms_image_upload_history')
ju_he_token = image.get('ju_he_token') ju_he_token = image.get('ju_he_token')
ju_he_image_list = image.get('ju_he_image_list') ju_he_image_list = image.get('ju_he_image_list')
app_id = image.get('app_id')
"""本地图片""" """本地图片"""
def get_image_names(): def get_image_names():
@ -24,6 +26,24 @@ def get_image_names():
local_image_path = os.getcwd() + '/' + image_local_path + '/' + random.choice(image_names) # 随机选取一张图片的路径 local_image_path = os.getcwd() + '/' + image_local_path + '/' + random.choice(image_names) # 随机选取一张图片的路径
return local_image_path return local_image_path
"""获取QQ头像"""
def download_qq_image(member_open_id):
save_path = os.getcwd() + '/' + image_local_qq_image_path + '/' + member_open_id + '.jpg'
size = 640 #尺寸 40、100、140、640
url = f"https://q.qlogo.cn/qqapp/{app_id}/{member_open_id}/{size}"
response = requests.get(url) # 发送 GET 请求获取图片资源
if response.status_code == 200: # 判断请求是否成功
with open(save_path, 'wb') as file: # 以二进制写入模式打开文件
file.write(response.content) # 将响应内容写入文件
return save_path
"""删除QQ头像"""
def qq_image_delete():
for root, dirs, files in os.walk(image_local_qq_image_path):
for file in files:
file_path = os.path.join(root, file)
os.remove(file_path)
""" sm.ms 图床""" """ sm.ms 图床"""
def get_smms_image_url(): def get_smms_image_url():
# 定义请求的参数 # 定义请求的参数

View file

Before

Width:  |  Height:  |  Size: 1 MiB

After

Width:  |  Height:  |  Size: 1 MiB

View file

Before

Width:  |  Height:  |  Size: 1 MiB

After

Width:  |  Height:  |  Size: 1 MiB

View file

Before

Width:  |  Height:  |  Size: 1 MiB

After

Width:  |  Height:  |  Size: 1 MiB

View file

Before

Width:  |  Height:  |  Size: 1 MiB

After

Width:  |  Height:  |  Size: 1 MiB

View file

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View file

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View file

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View file

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View file

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

View file

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View file

Before

Width:  |  Height:  |  Size: 2.9 MiB

After

Width:  |  Height:  |  Size: 2.9 MiB

View file

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View file

Before

Width:  |  Height:  |  Size: 2.5 MiB

After

Width:  |  Height:  |  Size: 2.5 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

View file

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

View file

Before

Width:  |  Height:  |  Size: 370 KiB

After

Width:  |  Height:  |  Size: 370 KiB

View file

Before

Width:  |  Height:  |  Size: 354 KiB

After

Width:  |  Height:  |  Size: 354 KiB

View file

Before

Width:  |  Height:  |  Size: 377 KiB

After

Width:  |  Height:  |  Size: 377 KiB

View file

Before

Width:  |  Height:  |  Size: 353 KiB

After

Width:  |  Height:  |  Size: 353 KiB

View file

Before

Width:  |  Height:  |  Size: 335 KiB

After

Width:  |  Height:  |  Size: 335 KiB

View file

Before

Width:  |  Height:  |  Size: 386 KiB

After

Width:  |  Height:  |  Size: 386 KiB

View file

Before

Width:  |  Height:  |  Size: 367 KiB

After

Width:  |  Height:  |  Size: 367 KiB

View file

Before

Width:  |  Height:  |  Size: 405 KiB

After

Width:  |  Height:  |  Size: 405 KiB

View file

@ -24,7 +24,7 @@ def djs(js):
break break
return jst return jst
def getjs(): def getjs():
return djs('src/common_plugins/cloud_music/jsdm.js') return djs('src/music/cloud_music/jsdm.js')
# 获取ptqrtoken # 获取ptqrtoken
def ptqrtoken(qrsign): def ptqrtoken(qrsign):

View file

@ -2,14 +2,12 @@
import base64 import base64
import codecs import codecs
import json import json
import pickle
from random import Random from random import Random
from Crypto.Cipher import AES from Crypto.Cipher import AES
import qrcode import qrcode
import src.common_plugins.cloud_music.agent as agent import src.music.cloud_music.agent as agent
from threading import Thread from threading import Thread
import time
import requests import requests
from io import BytesIO from io import BytesIO
from PIL import Image from PIL import Image

View file

@ -1,45 +1,5 @@
# 引入sqlalchemy依赖
from pathlib import Path
from sqlalchemy import Column, Integer, String, Date, create_engine, text from sqlalchemy import Column, Integer, String, Date, create_engine, text
from sqlalchemy.orm import declarative_base, sessionmaker from sqlalchemy.orm import declarative_base, sessionmaker
from nonebot.rule import to_me
from nonebot.plugin import on_command
from nonebot.adapters.qq import Message, MessageEvent, MessageSegment
from src.common_plugins.img.get_image import get_image_names, get_smms_image_url
fortune_by_sqlite = on_command("今日运势", rule=to_me(), priority=10, block=True)
@fortune_by_sqlite.handle()
async def get_today_fortune(message: MessageEvent):
local_image_path = get_image_names()
member_openid = message.get_user_id()
# 查询今日是否已经获取过今日运势,如果获取过则直接从日志取
result = is_get_fortune_log(member_openid)
if result is None:
# 获取 运势说明
result = get_fortune()
# 把抽取的今日运势插入日志
q = QrFortuneLog()
q.fortune_summary = result.fortune_summary
q.lucky_star = result.lucky_star
q.sign_text = result.sign_text
q.un_sign_text = result.un_sign_text
q.user_id = member_openid
insert_fortune_log(q)
content = ("\n" + "您的今日运势为:" + "\n" +
result.fortune_summary + "\n" +
result.lucky_star + "\n" +
"签文:" + result.sign_text + "\n" +
"————————————————————" + "\n" +
"解签:" + result.un_sign_text)
msg = Message([
MessageSegment.file_image(Path(local_image_path)),
# MessageSegment.image(url),
MessageSegment.text(content),
])
await fortune_by_sqlite.finish(msg)
def is_get_fortune_log(member_openid): def is_get_fortune_log(member_openid):

View file

@ -0,0 +1,83 @@
from sqlalchemy import Column, Integer, String, Date, create_engine, text
from sqlalchemy.orm import declarative_base, sessionmaker
def touch_count(member_openid):
session = SqliteSqlalchemy().session
#
result = session.execute(selectTouchCount,
{'member_openid': member_openid}).scalar()
session.close()
return result
def touch(status):
session = SqliteSqlalchemy().session
result = session.execute(selectTouchContent,{'status': status}).fetchone()
session.close()
return result
def insert_touch_log(QrTouchLog):
session = SqliteSqlalchemy().session
session.execute(insertTouchLog,
{'touch_status': QrTouchLog.touch_status,
'reply_touch_content': QrTouchLog.reply_touch_content,
'member_openid': QrTouchLog.user_id})
session.commit()
session.close()
return ""
# 申明基类对象
Base = declarative_base()
class QrTouch:
__tablename__ = 'qr_touch'
id = Column(Integer, primary_key=True)
touch_status = Column(Integer)
reply_touch_content = Column(String(255))
def __repr__(self):
return (
"QrTouch(id:{},touch_status:{},reply_touch_content:{})" .format(
self.id,
self.touch_status,
self.reply_touch_content
))
class QrTouchLog:
__tablename__ = 'qr_touch_log'
id = Column(Integer, primary_key=True)
touch_status = Column(Integer)
reply_touch_content = Column(String(255))
user_id = Column(String(255))
extract_time = Column(Date)
def __repr__(self):
return (
"QrFortune(id:{},touch_status:{},reply_touch_content:{},user_id:{})" .format(
self.id,
self.touch_status,
self.reply_touch_content,
self.user_id))
class SqliteSqlalchemy(object):
def __init__(self):
# 创建Sqlite连接引擎
engine = create_engine('sqlite:///./chat_bot.db', echo=True)
# 创建Sqlite的session连接对象
self.session = sessionmaker(bind=engine)()
# 查询初始化表是否存在
selectInit = text(
"SELECT name FROM sqlite_master WHERE type='table' AND name='qr_touch';")
# 查询回复内容
selectTouchContent = text("select * from qr_touch where touch_status = :status order by random() limit 1")
# 查询触摸次数
selectTouchCount = text(
"select count(*) from qr_touch_log where user_id = :member_openid and extract_time = date('now')")
# 插入日志表
insertTouchLog = text(
"insert into qr_touch_log (touch_status, reply_touch_content,user_id,extract_time) values (:touch_status, :reply_touch_content, :member_openid,date('now'))")

View file

@ -35,22 +35,22 @@ def tarotChoice(lockTarot):
if ints == 0: if ints == 0:
tarots = random.choice(tarot) tarots = random.choice(tarot)
txt = tarots[0] + '\n' + '正位' + '\n' + tarots[1] txt = tarots[0] + '\n' + '正位' + '\n' + tarots[1]
img = f'src/img_resource/tarotT/TarotImages' + tarots[3] img = f'src/image/tarotT/TarotImages' + tarots[3]
img_folder = 'AbstractImages' if lockTarot else 'TarotImages' img_folder = 'AbstractImages' if lockTarot else 'TarotImages'
img = f'src/img_resource/tarot/{img_folder}/{tarots[3]}' img = f'src/image/tarot/{img_folder}/{tarots[3]}'
return txt, img return txt, img
if ints == 1: if ints == 1:
tarots = random.choice(tarot) tarots = random.choice(tarot)
txt = tarots[0] + '\n' + '逆位' + '\n' + tarots[2] txt = tarots[0] + '\n' + '逆位' + '\n' + tarots[2]
img_folder = 'AbstractImages' if lockTarot else 'TarotImages' img_folder = 'AbstractImages' if lockTarot else 'TarotImages'
img1 = f'src/img_resource/tarot/side{img_folder}/' + tarots[3] img1 = f'src/image/tarot/side{img_folder}/' + tarots[3]
if not os.path.exists(f'src/img_resource/tarot/side{img_folder}'): if not os.path.exists(f'src/image/tarot/side{img_folder}'):
os.mkdir(f'src/img_resource/tarot/side{img_folder}') os.mkdir(f'src/image/tarot/side{img_folder}')
if os.path.exists(img1): if os.path.exists(img1):
return txt, img1 return txt, img1
else: else:
# 打开图像 # 打开图像
img = Image.open(f'src/img_resource/tarot/{img_folder}/{tarots[3]}') img = Image.open(f'src/image/tarot/{img_folder}/{tarots[3]}')
if img.mode == 'RGBA': if img.mode == 'RGBA':
img = img.convert('RGB') img = img.convert('RGB')
@ -58,7 +58,7 @@ def tarotChoice(lockTarot):
rotated_img = img.rotate(180) rotated_img = img.rotate(180)
# 保存旋转后的图像 # 保存旋转后的图像
rotated_img.save(f'src/img_resource/tarot/side{img_folder}/' + tarots[3]) rotated_img.save(f'src/image/tarot/side{img_folder}/' + tarots[3])
return txt, img1 return txt, img1
tarot = [['愚者 (The Fool)', tarot = [['愚者 (The Fool)',

View file

@ -1,10 +1,10 @@
from nonebot.rule import Rule, to_me from nonebot.rule import Rule, to_me
from nonebot.adapters.qq import Message, MessageEvent, MessageSegment
from nonebot import on_message from nonebot import on_message
from nonebot.adapters.qq import Message
from nonebot.adapters import Bot, Event from nonebot.adapters import Bot, Event
menu = ['/今日运势','/天气','/','/点歌','/摸摸','/今日老婆'] menu = ['/今日运势','/天气','/','/点歌','/摸摸','/群老婆','/今日老婆']
async def check_value_in_menu(event: Event) -> bool: async def check_value_in_menu(event: Event) -> bool:
value = event.get_plaintext().strip().split(" ") value = event.get_plaintext().strip().split(" ")
if value[0] in menu: if value[0] in menu:
@ -18,6 +18,3 @@ check = on_message(rule=to_me() & rule ,block=True)
@check.handle() @check.handle()
async def check(bot: Bot, event: Event): async def check(bot: Bot, event: Event):
await bot.send(message=Message("是什么呢?猫猫没有识别到,喵~"+'\n'+"(๑>ڡ<)☆ 请注意命令后要加空格哦~"),event=event) await bot.send(message=Message("是什么呢?猫猫没有识别到,喵~"+'\n'+"(๑>ڡ<)☆ 请注意命令后要加空格哦~"),event=event)

View file

@ -1,12 +1,10 @@
import os
import pickle import pickle
import time
from pathlib import Path from pathlib import Path
import requests
from nonebot import on_command from nonebot import on_command
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot.adapters.qq import MessageSegment,MessageEvent from nonebot.adapters.qq import MessageSegment,MessageEvent
from src.common_plugins.cloud_music.cloud_music import * from src.music.cloud_music.cloud_music import *
music = on_command("点歌", rule=to_me(), priority=10, block=True) music = on_command("点歌", rule=to_me(), priority=10, block=True)
@music.handle() @music.handle()

Some files were not shown because too many files have changed in this diff Show more