From 8b7cc1c55bca3f8bcbdc4ac7422ee418c73cf638 Mon Sep 17 00:00:00 2001 From: Pyhtagodzilla <1670671958@qq.com> Date: Wed, 16 Jul 2025 23:57:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=BD=E5=A4=9F=E6=8E=A5=E6=94=B6=E5=88=B0?= =?UTF-8?q?=E7=A7=81=E8=81=8A=EF=BC=88=E6=9A=82=E6=9C=AA=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=EF=BC=89=E5=92=8C=E7=BE=A4=E7=BB=84=E7=9A=84=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=B9=B6=E5=B0=86=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=9C=A8=E6=9C=AC=E5=9C=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ docs/踩坑.md | 19 +++++++++++ src/main.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 docs/踩坑.md diff --git a/README.md b/README.md index e69de29..290190f 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,2 @@ +# +一个用于分类和查询meme的工具。 \ No newline at end of file diff --git a/docs/踩坑.md b/docs/踩坑.md new file mode 100644 index 0000000..416cce6 --- /dev/null +++ b/docs/踩坑.md @@ -0,0 +1,19 @@ +## ncatbot 开发踩的坑 + +--- + +由于一上来就采用了远程开发的模式,所以在bot连接的过程中,需要在 ```bot.run()``` 函数中另外传入参数 +```python +bot.run( + bt_uin = "bot account", + root = "bot's master account", + ws_uri = "ws://", + ws_token = "Your token", + webui_uri = "http://Your server:port", + webui_token = "Your token" +) +``` +具体的信息在 [配置项文档](https://docs.ncatbot.xyz/guide/kfcvme50/#%E9%85%8D%E7%BD%AE%E9%A1%B9%E5%88%97%E8%A1%A8) 中提及。 +一些报错是由于webui的token所导致的,默认值为napcat,另外连接都需要带上协议,例如连接webui时需要带上 ```http://``` +--- + diff --git a/src/main.py b/src/main.py index e69de29..3b7025a 100644 --- a/src/main.py +++ b/src/main.py @@ -0,0 +1,92 @@ +from ncatbot.core import BotClient, GroupMessage, PrivateMessage +# from ncatbot.core import (MessageChain, Text, Image) +from ncatbot.utils import get_log +import requests + +bot = BotClient() +_log = get_log() + +@bot.private_event() +async def on_receive_images_private_event(msg: PrivateMessage): + _log.info(msg) + + if "url=https://" not in msg.raw_message: + return + + else: + try: + file_name = msg.raw_message.split(",")[1].split("=")[1] + image_url = await bot.api.get_image(file_name) + real_url = image_url['data']['url'] + except IndexError as e: + _log.debug(e) + real_url = "None" + file_name = "None" + except TypeError as e: + _log.debug(e) + real_url = "None" + file_name = "None" + + if real_url == "None": + return + else: + response = requests.get(real_url) + img = response.content + with open(f"data/pics/{file_name}", 'wb') as f: + f.write(img) + + # file_name = msg.raw_message.split(",")[1].split("=")[1] + # image_url = await bot.api.get_image(file_name) + # real_url = image_url['data']['url'] + # + # _log.debug(f"image's real url: {real_url}") + # + # try: + # response = requests.get(real_url) + # img = response.content + # with open(f"data/pics/{file_name}", 'wb') as f: + # f.write(img) + # except Exception as e: + # _log.error(e) + # pass + +@bot.group_event() +async def on_receive_images_group_event(msg: GroupMessage): + _log.info(msg) + + if "url=https://" not in msg.raw_message: + return + + else: + try: + file_name = msg.raw_message.split(",")[1].split("=")[1] + image_url = await bot.api.get_image(file_name) + real_url = image_url['data']['url'] + except IndexError as e: + _log.debug(e) + real_url = "None" + file_name = "None" + except TypeError as e: + _log.debug(e) + real_url = "None" + file_name = "None" + + + if real_url == "None": + return + else: + response = requests.get(real_url) + img = response.content + with open(f"data/pics/{file_name}", 'wb') as f: + f.write(img) + +if __name__ == "__main__": + + api = bot.run( + bt_uin="203758258", + root="1670671958", + ws_uri="ws://192.168.10.78:3001", + ws_token="", + webui_uri="http://192.168.10.78:6099", + webui_token="@Jtbx2mtblj" + ) \ No newline at end of file