Compare commits

..

3 Commits

Author SHA1 Message Date
Pyhtagodzilla
a3c5ae75d3 同步 2025-07-20 23:31:42 +08:00
Pyhtagodzilla
58b5dcff73 Merge remote-tracking branch 'origin/master' 2025-07-20 23:29:25 +08:00
Pyhtagodzilla
504a472b22 呃呃重写了解析图片消息的 2025-07-19 11:48:46 +08:00
6 changed files with 91 additions and 93 deletions

6
.env.bak Normal file
View File

@ -0,0 +1,6 @@
BOT_ACCOUNT="Your-bot-account"
ROOT_ACCOUNT="Your-root-account"
WS_URI="ws://Your-uri:port"
WS_TOKEN="Your-token"
WEBUI_URI="http://Your-uri:port"
WEBUI_TOKEN="Your-token

5
.gitignore vendored
View File

@ -1 +1,4 @@
tests tests
.env
src/data/pics
src/logs

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
requests~=2.32.4
ncatbot~=3.8.8.post7
dotenv~=0.9.9
python-dotenv~=1.1.1

View File

@ -1,92 +0,0 @@
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"
)

0
src/utils/__init__.py Normal file
View File

77
src/utils/get_image.py Normal file
View File

@ -0,0 +1,77 @@
from ncatbot.core import BotClient, GroupMessage, PrivateMessage
# from ncatbot.core import (MessageChain, Text, Image)
from ncatbot.utils import get_log
from dotenv import load_dotenv
import requests
import os
bot = BotClient()
_log = get_log()
@bot.private_event()
async def on_receive_images_private_event(msg: PrivateMessage):
meta_message = str(msg)
_log.info(f"The message's meta data is: {meta_message}")
_log.info(f"The message's raw message is: {msg.raw_message}")
if "[CQ:image," in msg.raw_message:
divided_message = msg.raw_message.split(',')
for item in divided_message:
if "file=" in item:
file_name = item.split("=")[1]
_log.info(f"Name of the image is: {file_name}")
image_url = await bot.api.get_image(file_name)
_log.info(f"Image's information collected by \"get_image\" is: {image_url}")
real_url = image_url['data']['url']
_log.info(f"Image's real url is: {real_url}")
response = requests.get(real_url)
img = response.content
with open(f"D:/Projects/memebot/src/data/pics/{file_name}", 'wb') as f:
f.write(img)
@bot.group_event()
async def on_receive_images_group_event(msg: GroupMessage):
meta_message = str(msg)
_log.info(f"The message's meta data is: {meta_message}")
_log.info(f"The message's raw message is: {msg.raw_message}")
# if "[CQ:image," in msg.raw_message:
if msg.raw_message.startswith("[CQ:image,"):
divided_message = msg.raw_message.split(',')
for item in divided_message:
# if "file=" in item:
if item.startswith("file="):
file_name = item.split("=")[1]
_log.info(f"Name of the image is: {file_name}")
image_url = await bot.api.get_image(file_name)
_log.info(f"Image's information collected by \"get_image\" is: {image_url}")
real_url = image_url['data']['url']
_log.info(f"Image's real url is: {real_url}")
response = requests.get(real_url)
img = response.content
with open(f"../../data/pics/{file_name}", 'wb') as f:
f.write(img)
"""
当单独运行这个程序时会连接napcat并保存图片到本地
"""
if __name__ == "__main__":
load_dotenv(dotenv_path=r"../../.env")
api = bot.run(
bt_uin=os.getenv("BOT_ACCOUNT"),
root=os.getenv("ROOT_ACCOUNT"),
ws_uri=os.getenv("WS_URI"),
ws_token=os.getenv("WS_TOKEN"),
webui_uri=os.getenv("WEBUI_URI"),
webui_token=os.getenv("WEBUI_TOKEN")
)