diff --git a/plugins/read/__init__.py b/plugins/read/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/plugins/read/config_default.py b/plugins/read/config_default.py deleted file mode 100644 index 1103345c9da6732093b2830978f6e13b371968bc..0000000000000000000000000000000000000000 --- a/plugins/read/config_default.py +++ /dev/null @@ -1,12 +0,0 @@ -MAX_STRING_LENGTH = 300 - -# Baidu AI Apps -APP_ID = "" -API_KEY = "" -SECRET_KEY = "" - -# Volume 1(low)-10(high) -VOLUME = 8 -# Speech rate 1(slow)-10(quick) -SPEED = 4 - diff --git a/plugins/read/read.py b/plugins/read/read.py deleted file mode 100644 index b0f914418f1a08aa2b1bf742263f0d5e68f87685..0000000000000000000000000000000000000000 --- a/plugins/read/read.py +++ /dev/null @@ -1,40 +0,0 @@ -from register import command -from global_vars import CONFIG -import base64 -import tempfile -import os -from aip import AipSpeech -import threading -config = CONFIG[__name__] - - -def plugin(): - return { - "author": "Antares", - "version": 1.0, - "description": "文字朗读" - } - - -@command(name="read", help="文字转语音") -def read(bot, context, args): - string = " ".join(args[1:]) - if len(string) > config.MAX_STRING_LENGTH: - bot.send(context, "字符串过长") - return - - def handle(): - client = AipSpeech(config.APP_ID, config.API_KEY, config.SECRET_KEY) - voice = client.synthesis(string, 'zh', 1, { - 'vol': config.VOLUME, - 'per': 4, - 'spd': config.SPEED - }) - - if not isinstance(voice, dict): - result = str(base64.encodebytes(voice).decode().replace("\n", "")) - bot.send(context, "[CQ:record,file=base64://{}]".format(result)) - else: - bot.send(context, "转换语音失败,请检查是否含有非法字符") - - threading.Thread(target=handle).start() diff --git a/plugins_new/read/plugin.py b/plugins_new/read/plugin.py new file mode 100644 index 0000000000000000000000000000000000000000..fe407ed32e6bbc70bda660925282d501168c4820 --- /dev/null +++ b/plugins_new/read/plugin.py @@ -0,0 +1,77 @@ +from common.plugin import Plugin +from common.config_loader import ConfigBase +from common.datatypes import PluginMeta +from common.countdown_bot import CountdownBot +from common.loop import TimeTuple +from common.command import ChatType +from common.event import GroupMessageEvent +from typing import List +import base64 +from aip import AipSpeech + + +class ReadConfig(ConfigBase): + MAX_STRING_LENGTH = 300 + + # Baidu AI Apps + APP_ID = "" + API_KEY = "" + SECRET_KEY = "" + + # Volume 1(low)-10(high) + VOLUME = 8 + # Speech rate 1(slow)-10(quick) + SPEED = 4 + + +class ReadPlugin(Plugin): + def get_voice(self, text: str) -> str: + result = self.client.synthesis(text, 'zh', 1, { + 'vol': self.config.VOLUME, + 'per': 4, + 'spd': self.config.SPEED + }) + if isinstance(result, dict): + return "" + else: + return base64.encodebytes(result).decode().replace("\n", "") + + def command_read(self, plugin, args: List[str], raw_string: str, context, evt: GroupMessageEvent): + def wrapper(): + text = " ".join(args[1:]) + if len(text) > self.config.MAX_STRING_LENGTH: + self.bot.send(context, "字符串过长") + else: + b64voice = self.get_voice(text) + if b64voice: + self.bot.send( + context, f"[CQ:record,file=base64://{b64voice}]") + else: + self.bot.send(context, "生成错误,请检查是否含有非法字符") + self.bot.submit_multithread_task(wrapper) + + def on_enable(self): + self.client = AipSpeech( + self.config.APP_ID, self.config.API_KEY, self.config.SECRET_KEY) + self.config: ReadConfig + self.bot: CountdownBot + self.register_command_wrapped( + command_name="read", + command_handler=self.command_read, + help_string="文字转语音", + chats={ChatType.discuss, ChatType.group, ChatType.private}, + ) + + +def get_plugin_class(): + return ReadPlugin + + +def get_config_class(): + return ReadConfig + + +def get_plugin_meta(): + return PluginMeta( + "read", 2.0, "文字转语音" + )