1+ import random
12from typing import List , Tuple , Type , Any
23from src .plugin_system import (
34 BasePlugin ,
1213 EventType ,
1314 MaiMessages ,
1415 ToolParamType ,
16+ ReplyContentType ,
17+ emoji_api ,
1518)
19+ from src .config .config import global_config
1620
1721
1822class CompareNumbersTool (BaseTool ):
@@ -24,6 +28,7 @@ class CompareNumbersTool(BaseTool):
2428 ("num1" , ToolParamType .FLOAT , "第一个数字" , True , None ),
2529 ("num2" , ToolParamType .FLOAT , "第二个数字" , True , None ),
2630 ]
31+ available_for_llm = True
2732
2833 async def execute (self , function_args : dict [str , Any ]) -> dict [str , Any ]:
2934 """执行比较两个数的大小
@@ -136,12 +141,80 @@ class PrintMessage(BaseEventHandler):
136141 handler_name = "print_message_handler"
137142 handler_description = "打印接收到的消息"
138143
139- async def execute (self , message : MaiMessages | None ) -> Tuple [bool , bool , str | None , None ]:
144+ async def execute (self , message : MaiMessages | None ) -> Tuple [bool , bool , str | None , None , None ]:
140145 """执行打印消息事件处理"""
141146 # 打印接收到的消息
142147 if self .get_config ("print_message.enabled" , False ):
143148 print (f"接收到消息: { message .raw_message if message else '无效消息' } " )
144- return True , True , "消息已打印" , None
149+ return True , True , "消息已打印" , None , None
150+
151+
152+ class ForwardMessages (BaseEventHandler ):
153+ """
154+ 把接收到的消息转发到指定聊天ID
155+
156+ 此组件是HYBRID消息和FORWARD消息的使用示例。
157+ 每收到10条消息,就会以1%的概率使用HYBRID消息转发,否则使用FORWARD消息转发。
158+ """
159+
160+ event_type = EventType .ON_MESSAGE
161+ handler_name = "forward_messages_handler"
162+ handler_description = "把接收到的消息转发到指定聊天ID"
163+
164+ def __init__ (self , * args , ** kwargs ):
165+ super ().__init__ (* args , ** kwargs )
166+ self .counter = 0 # 用于计数转发的消息数量
167+ self .messages : List [str ] = []
168+
169+ async def execute (self , message : MaiMessages | None ) -> Tuple [bool , bool , None , None , None ]:
170+ if not message :
171+ return True , True , None , None , None
172+ stream_id = message .stream_id or ""
173+
174+ if message .plain_text :
175+ self .messages .append (message .plain_text )
176+ self .counter += 1
177+ if self .counter % 10 == 0 :
178+ if random .random () < 0.01 :
179+ success = await self .send_hybrid (stream_id , [(ReplyContentType .TEXT , msg ) for msg in self .messages ])
180+ else :
181+ success = await self .send_forward (
182+ stream_id ,
183+ [
184+ (
185+ str (global_config .bot .qq_account ),
186+ str (global_config .bot .nickname ),
187+ [(ReplyContentType .TEXT , msg )],
188+ )
189+ for msg in self .messages
190+ ],
191+ )
192+ if not success :
193+ raise ValueError ("转发消息失败" )
194+ self .messages = []
195+ return True , True , None , None , None
196+
197+
198+ class RandomEmojis (BaseCommand ):
199+ command_name = "random_emojis"
200+ command_description = "发送多张随机表情包"
201+ command_pattern = r"^/random_emojis$"
202+
203+ async def execute (self ):
204+ emojis = await emoji_api .get_random (5 )
205+ if not emojis :
206+ return False , "未找到表情包" , False
207+ emoji_base64_list = []
208+ for emoji in emojis :
209+ emoji_base64_list .append (emoji [0 ])
210+ return await self .forward_images (emoji_base64_list )
211+
212+ async def forward_images (self , images : List [str ]):
213+ """
214+ 把多张图片用合并转发的方式发给用户
215+ """
216+ success = await self .send_forward ([("0" , "神秘用户" , [(ReplyContentType .IMAGE , img )]) for img in images ])
217+ return (True , "已发送随机表情包" , True ) if success else (False , "发送随机表情包失败" , False )
145218
146219
147220# ===== 插件注册 =====
@@ -153,7 +226,7 @@ class HelloWorldPlugin(BasePlugin):
153226
154227 # 插件基本信息
155228 plugin_name : str = "hello_world_plugin" # 内部标识符
156- enable_plugin : bool = True
229+ enable_plugin : bool = False
157230 dependencies : List [str ] = [] # 插件依赖列表
158231 python_dependencies : List [str ] = [] # Python包依赖列表
159232 config_file_name : str = "config.toml" # 配置文件名
@@ -185,6 +258,8 @@ def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
185258 (ByeAction .get_action_info (), ByeAction ), # 添加告别Action
186259 (TimeCommand .get_command_info (), TimeCommand ),
187260 (PrintMessage .get_handler_info (), PrintMessage ),
261+ (ForwardMessages .get_handler_info (), ForwardMessages ),
262+ (RandomEmojis .get_command_info (), RandomEmojis ),
188263 ]
189264
190265
0 commit comments