修复了多文件导入问题 & 重写了MegaKits (#18)

* modified:   pyproject.toml
	nonebot_plugin_marshoai/tools/marshoai-megakits/

* modified:   nonebot_plugin_marshoai/models.py
	new file:   nonebot_plugin_marshoai/tools/marshoai-megakits/a.py
	nonebot_plugin_marshoai/tools/marshoai-megakits/__init__.py
	nonebot_plugin_marshoai/tools/marshoai-megakits/tools.json

* modified:   nonebot_plugin_marshoai/models.py
	nonebot_plugin_marshoai/tools/marshoai-megakits/__init__.py
	nonebot_plugin_marshoai/tools/marshoai-megakits/tools.json

* deleted:    nonebot_plugin_marshoai/tools/marshoai-megakits/a.py
	new file:   nonebot_plugin_marshoai/tools/marshoai-megakits/mk_Common.py
	new file:   nonebot_plugin_marshoai/tools/marshoai-megakits/mk_Info.py
	new file:   nonebot_plugin_marshoai/tools/marshoai-megakits/mk_MorseCode.py
	new file:   nonebot_plugin_marshoai/tools/marshoai-megakits/mk_NyaCode.py
	nonebot_plugin_marshoai/tools/marshoai-megakits/__init__.py
	nonebot_plugin_marshoai/tools/marshoai-megakits/tools.json

* modified:   nonebot_plugin_marshoai/models.py
	modified:   nonebot_plugin_marshoai/tools/marshoai-megakits/mk_Info.py
	modified:   nonebot_plugin_marshoai/util.py
	modified:   pyproject.toml
	nonebot_plugin_marshoai/tools/marshoai-megakits/__init__.py
	nonebot_plugin_marshoai/tools/marshoai-megakits/tools.json
This commit is contained in:
Nya_Twisuki 2024-12-05 18:44:26 +08:00 committed by GitHub
parent c1385f81d0
commit 5c51797e00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 135 additions and 3 deletions

View File

@ -1,9 +1,9 @@
from .util import * from .util import *
from .config import config from .config import config
import os import os
import re
import json import json
import importlib import importlib
import sys
# import importlib.util # import importlib.util
import traceback import traceback
@ -74,6 +74,9 @@ class MarshoTools:
for package_name in os.listdir(tools_dir): for package_name in os.listdir(tools_dir):
package_path = os.path.join(tools_dir, package_name) package_path = os.path.join(tools_dir, package_name)
logger.info(f"尝试加载工具包 {package_name}")
if os.path.isdir(package_path) and os.path.exists( if os.path.isdir(package_path) and os.path.exists(
os.path.join(package_path, "__init__.py") os.path.join(package_path, "__init__.py")
): ):
@ -84,13 +87,15 @@ class MarshoTools:
data = json.load(json_file) data = json.load(json_file)
for i in data: for i in data:
self.tools_list.append(i) self.tools_list.append(i)
# 导入包
spec = importlib.util.spec_from_file_location( spec = importlib.util.spec_from_file_location(
package_name, os.path.join(package_path, "__init__.py") package_name, os.path.join(package_path, "__init__.py")
) )
package = importlib.util.module_from_spec(spec) package = importlib.util.module_from_spec(spec)
spec.loader.exec_module(package)
self.imported_packages[package_name] = package self.imported_packages[package_name] = package
sys.modules[package_name] = package
spec.loader.exec_module(package)
logger.success(f"成功加载工具包 {package_name}") logger.success(f"成功加载工具包 {package_name}")
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
logger.error(f"解码 JSON {json_path} 时发生错误: {e}") logger.error(f"解码 JSON {json_path} 时发生错误: {e}")

View File

@ -0,0 +1,24 @@
import random
# Random Turntable
async def random_turntable(upper: int, lower: int = "0"):
return random.randint(lower, upper)
# Number Calc
def number_calc(a: str, b: str, op: str):
a, b = float(a), float(b)
match op:
case "+":
return str(a + b)
case "-":
return str(a - b)
case "*":
return str(a * b)
case "/":
return str(a / b)
case "**":
return str(a ** b)
case "%":
return str(a % b)
case _:
return "未知运算符"

View File

@ -0,0 +1,7 @@
# Twisuki
def twisuki():
return "Twiuski(苏阳)是megakits插件作者, Github : 'https://github.com/Twisuki'"
# MegaKits
def megakits():
return "MegaKits插件是一个功能混杂的MarshoAI插件, 由Twisuki(Github : 'https://github.com/Twisuki')开发, 插件仓库 : 'https://github.com/LiteyukiStudio/marsho-toolsets/tree/main/Twisuki/marshoai-megakits'"

View File

@ -0,0 +1,44 @@
# MorseCode
MorseEncode = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..',
'1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----',
'.': '.-.-.-', ':': '---...', ',': '--..--', ';': '-.-.-.',
'?': '..--..', '=': '-...-', '\'': '.----.', '/': '-..-.',
'!': '-.-.--', '-': '-....-', '_': '..--.-', '\"': '.-..-.',
'(': '-.--.', ')': '-.--.-', '$': '...-..-', '&': '....',
'@': '.--.-.', ' ': ' '
}
MorseDecode = {value: key for key, value in MorseEncode.items()}
# MorseCode Encrypt
def morse_encrypt(msg: str):
result = ""
msg = msg.upper()
for char in msg:
if char in MorseEncode:
result += MorseEncode[char]
else:
result += '..--..'
result += ' '
return result
# MorseCode Decrypt
def morse_decrypt(msg: str):
result = ""
msg_arr = msg.split()
for char in msg_arr:
if char in MorseDecode:
result += MorseDecode[char]
else:
result += '?'
return result

View File

@ -0,0 +1,52 @@
import random
import base64
# NyaCode
NyaCodeCharset = [
'', '', '?', '~'
]
NyaCodeSpecialCharset = [
'', '!', '...', '....'
]
NyaCodeEncode = {}
for i in range(64):
triplet = ''.join(NyaCodeCharset[(i // (4 ** j)) % 4] for j in range(3))
NyaCodeEncode[chr(65 + i if i < 26 else 97 + (i - 26) if i < 52 else 48 + (i - 52) if i < 62 else (
43 if i == 62 else 47))] = triplet
NyaCodeDecode = {value: key for key, value in NyaCodeEncode.items()}
# NyaCode Encrypt
def nya_encode(msg: str):
msg_b64str = base64.b64encode(msg.encode()).decode().replace('=', '')
msg_nyastr = ''.join(NyaCodeEncode[base64_char] for base64_char in msg_b64str)
result = ""
for char in msg_nyastr:
if random.random() < 0.2:
result += random.choice(NyaCodeSpecialCharset) + char
else:
result += char
return result
# NyaCode Decrypt
def nya_decode(msg: str):
msg = msg.replace('', '').replace('.', '').replace('!', '')
msg_nyastr = []
i = 0
if len(msg) % 3 != 0 :
return "这句话不是正确的猫语"
while i < len(msg):
nyachar = msg[i : i + 3]
try:
if all(char in NyaCodeCharset for char in nyachar):
msg_nyastr.append(nyachar)
i += 3
except Exception:
return "这句话不是正确的猫语"
msg_b64str = ''.join(NyaCodeDecode[nya_char] for nya_char in msg_nyastr)
try:
result = base64.b64decode(msg_b64str.encode()).decode()
except Exception:
return "翻译失败"
return result