mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-01-26 18:12:47 +08:00
✨ 添加聊天功能,支持获取会话信息和发送消息到用户或群组
This commit is contained in:
parent
1e58944edc
commit
10b2634b00
@ -1,12 +1,12 @@
|
|||||||
from nonebot_plugin_marshoai.plugin import PluginMetadata
|
from nonebot_plugin_marshoai.plugin import PluginMetadata
|
||||||
|
|
||||||
|
from .chat import *
|
||||||
from .file_io import *
|
from .file_io import *
|
||||||
from .manager import *
|
from .manager import *
|
||||||
from .network import *
|
from .network import *
|
||||||
|
|
||||||
__marsho_meta__ = PluginMetadata(
|
__marsho_meta__ = PluginMetadata(
|
||||||
name="内置增强组件",
|
name="内置增强组件",
|
||||||
version="0.0.1",
|
|
||||||
description="内置工具插件",
|
description="内置工具插件",
|
||||||
author="MarshoTeam of LiteyukiStudio",
|
author="MarshoTeam of LiteyukiStudio",
|
||||||
)
|
)
|
||||||
|
74
nonebot_plugin_marshoai/plugins/builtin_tools/chat.py
Normal file
74
nonebot_plugin_marshoai/plugins/builtin_tools/chat.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
from nonebot.adapters.onebot.v11 import (
|
||||||
|
Bot,
|
||||||
|
GroupMessageEvent,
|
||||||
|
MessageEvent,
|
||||||
|
PrivateMessageEvent,
|
||||||
|
)
|
||||||
|
from nonebot.exception import FinishedException
|
||||||
|
from nonebot.permission import SUPERUSER
|
||||||
|
|
||||||
|
from nonebot_plugin_marshoai.plugin import String, on_function_call
|
||||||
|
|
||||||
|
|
||||||
|
@on_function_call(description="获取当前会话信息,比如群聊或用户的身份信息").permission(
|
||||||
|
SUPERUSER
|
||||||
|
)
|
||||||
|
async def get_session_info(bot: Bot, event: MessageEvent) -> str:
|
||||||
|
"""获取当前会话信息,比如群聊或用户的身份信息
|
||||||
|
|
||||||
|
Args:
|
||||||
|
bot (Bot): Bot对象
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: 会话信息
|
||||||
|
"""
|
||||||
|
if isinstance(event, PrivateMessageEvent):
|
||||||
|
return f"当前会话为私聊,用户ID: {event.user_id}"
|
||||||
|
elif isinstance(event, GroupMessageEvent):
|
||||||
|
return f"当前会话为群聊,群组ID: {event.group_id}, 用户ID: {event.user_id}"
|
||||||
|
else:
|
||||||
|
return "未知会话类型"
|
||||||
|
|
||||||
|
|
||||||
|
@on_function_call(description="发送消息到指定用户").params(
|
||||||
|
user=String(description="用户ID"), message=String(description="消息内容")
|
||||||
|
).permission(SUPERUSER)
|
||||||
|
async def send_message(user: str, message: str, bot: Bot) -> str:
|
||||||
|
"""发送消息到指定用户,实验性功能,仅限onebotv11适配器
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user (str): 用户ID
|
||||||
|
message (str): 消息内容
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: 发送结果
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
await bot.send_private_msg(user_id=int(user), message=message)
|
||||||
|
return "发送成功"
|
||||||
|
except FinishedException as e:
|
||||||
|
return "发送完成"
|
||||||
|
except Exception as e:
|
||||||
|
return "发送失败: " + str(e)
|
||||||
|
|
||||||
|
|
||||||
|
@on_function_call(description="发送消息到指定群组").params(
|
||||||
|
group=String(description="群组ID"), message=String(description="消息内容")
|
||||||
|
).permission(SUPERUSER)
|
||||||
|
async def send_group_message(group: str, message: str, bot: Bot) -> str:
|
||||||
|
"""发送消息到指定群组,实验性功能,仅限onebotv11适配器
|
||||||
|
|
||||||
|
Args:
|
||||||
|
group (str): 群组ID
|
||||||
|
message (str): 消息内容
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: 发送结果
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
await bot.send_group_msg(group_id=int(group), message=message)
|
||||||
|
return "发送成功"
|
||||||
|
except FinishedException as e:
|
||||||
|
return "发送完成"
|
||||||
|
except Exception as e:
|
||||||
|
return "发送失败: " + str(e)
|
@ -21,3 +21,24 @@ async def read_file(fp: str) -> str:
|
|||||||
return await f.read()
|
return await f.read()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return "读取出错: " + str(e)
|
return "读取出错: " + str(e)
|
||||||
|
|
||||||
|
|
||||||
|
@on_function_call(description="写入内容到设备上本地文件").params(
|
||||||
|
fp=String(description="文件路径"), content=String(description="写入内容")
|
||||||
|
).permission(SUPERUSER)
|
||||||
|
async def write_file(fp: str, content: str) -> str:
|
||||||
|
"""写入内容到设备上本地文件
|
||||||
|
|
||||||
|
Args:
|
||||||
|
fp (str): 文件路径
|
||||||
|
content (str): 写入内容
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: 写入结果
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
async with aiofiles.open(fp, "w", encoding="utf-8") as f:
|
||||||
|
await f.write(content)
|
||||||
|
return "写入成功"
|
||||||
|
except Exception as e:
|
||||||
|
return "写入出错: " + str(e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user