Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Mar 12, 2024
1 parent 8fb6329 commit 4c3553d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 91 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ pip install nonebot_plugin_memes_api

并按照 [NoneBot 加载插件](https://nonebot.dev/docs/tutorial/create-plugin#加载插件) 加载插件

#### 配置驱动器​

插件需要“客户端型驱动器”(如 httpx)来下载图片等,驱动器安装和配置参考 [NoneBot 选择驱动器](https://nonebot.dev/docs/advanced/driver)

同时需要在 `.env.*` 配置文件中启用对应的驱动器,例如:

```
DRIVER=~fastapi+~httpx+~websockets
```

#### meme-generator 部署

按照 [meme-generator 安装](https://github.com/MeetWq/meme-generator#安装) 中的说明安装,并下载图片、安装字体等
Expand Down Expand Up @@ -94,6 +104,12 @@ pip install nonebot_plugin_memes_api
- 默认:`False`
- 说明:在表情需要至少1段文字且没有输入文字时,是否使用默认文字(谨慎使用,容易误触发)

#### `memes_random_meme_show_info`

- 类型:`bool`
- 默认:`False`
- 说明:使用“随机表情”时是否同时发出表情关键词

### 使用

使用方式与 [nonebot-plugin-memes](https://github.com/noneplugin/nonebot-plugin-memes) 基本一致
21 changes: 14 additions & 7 deletions nonebot_plugin_memes_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@
"nonebot_plugin_session",
"nonebot_plugin_userinfo",
),
extra={
"unique_name": "memes_api",
"author": "meetwq <[email protected]>",
"version": "0.3.0",
},
)

memes_cache_dir = get_cache_dir("nonebot_plugin_memes_api")
Expand Down Expand Up @@ -247,6 +242,7 @@ async def process(
texts: List[str],
user_infos: List[UserInfo],
args: Dict[str, Any] = {},
show_info: bool = False,
):
images: List[bytes] = []

Expand Down Expand Up @@ -282,7 +278,11 @@ async def process(
logger.warning(traceback.format_exc())
await matcher.finish("出错了,请稍后再试")

msg = UniMessage.image(raw=result)
msg = UniMessage()
if show_info:
keywords = "、".join([f'"{keyword}"' for keyword in meme.keywords])
msg += f"关键词:{keywords}"
msg += UniMessage.image(raw=result)
await msg.send()


Expand Down Expand Up @@ -357,7 +357,14 @@ async def random_handler(state: T_State, matcher: Matcher):
)
]
)
await process(matcher, random_meme, image_sources, texts, user_infos)
await process(
matcher,
random_meme,
image_sources,
texts,
user_infos,
show_info=memes_config.memes_random_meme_show_info,
)


random_matcher = on_message(command_rule(["随机表情"]), block=False, priority=12)
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_memes_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Config(BaseModel):
memes_prompt_params_error: bool = False
memes_use_sender_when_no_image: bool = False
memes_use_default_when_no_text: bool = False
memes_random_meme_show_info: bool = False


memes_config = get_plugin_config(Config)
14 changes: 8 additions & 6 deletions nonebot_plugin_memes_api/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from nonebot.adapters import Bot, Event, Message
from nonebot.params import Depends
from nonebot.typing import T_State
from nonebot_plugin_alconna import At, Image, Text, UniMessage, image_fetch
from nonebot_plugin_alconna.uniseg.segment import reply_handle
from nonebot_plugin_alconna import At, Image, Text, UniMessage
from nonebot_plugin_alconna.uniseg.tools import image_fetch, reply_fetch
from nonebot_plugin_userinfo import ImageSource, UserInfo, get_user_info

from .config import memes_config
Expand Down Expand Up @@ -42,11 +42,13 @@ async def dependency(bot: Bot, event: Event, state: T_State):
msg: Message = state[MSG_KEY]
uni_msg = UniMessage()
if msg:
uni_msg = await UniMessage.generate(message=msg)
uni_msg = UniMessage.generate_without_reply(message=msg)
uni_msg_with_reply = UniMessage()
if reply := await reply_handle(event, bot):
if isinstance(reply.msg, Message) and reply.msg:
uni_msg_with_reply = await UniMessage.generate(message=reply.msg)
if (reply := await reply_fetch(event, bot)) and reply.msg:
reply_msg = reply.msg
if isinstance(reply_msg, str):
reply_msg = msg.__class__(reply_msg)
uni_msg_with_reply = UniMessage.generate_without_reply(message=reply_msg)
uni_msg_with_reply.extend(uni_msg)

for msg_seg in uni_msg_with_reply:
Expand Down
Loading

0 comments on commit 4c3553d

Please sign in to comment.