Skip to content

Commit

Permalink
🎨优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
wling-art committed Jan 7, 2024
1 parent 4300368 commit 02d8961
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/plugins/cave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def _(bot: Bot, matcher: Matcher, event: MessageEvent):
await matcher.finish("请输入正确的序号")
try:
data = await cave_models.get(id=key)
except:
except Exception:
await matcher.finish("没有这个序号的投稿")
# 判断是否是超级用户或者是投稿人
if str(event.user_id) in SUPERUSER:
Expand All @@ -109,7 +109,7 @@ async def _(bot: Bot, matcher: Matcher, event: MessageEvent):
f"你的投稿{key}已经被{event.user_id}删除了!\n内容为:\n{data.details}\n原因:{reason}"
),
)
except:
except Exception:
logger.error(f"回声洞删除投稿私聊通知失败,投稿人id:{data.user_id}")
elif event.user_id == data.user_id:
await data.delete()
Expand Down
17 changes: 5 additions & 12 deletions src/plugins/cave/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@
def url_to_base64(image_url):
response = requests.get(image_url)
image_data = response.content
base64_data = base64.b64encode(image_data).decode("utf-8")
return base64_data
return base64.b64encode(image_data).decode("utf-8")


def process_message(original_message):
# 使用正则表达式提取图片URL
url_match = re.search(
if url_match := re.search(
r"\[CQ:image,file=\w+\.image,url=([^\]]+)\]", original_message
)

if url_match:
image_url = url_match.group(1)
):
image_url = url_match[1]

# 将图片URL转换为Base64
base64_image = url_to_base64(image_url)

# 构建新的消息
new_message = re.sub(
return re.sub(
r"\[CQ:image,file=\w+\.image,url=([^\]]+)\]",
f"[CQ:image,file=base64://{base64_image}]",
original_message,
)

return new_message
else:
return original_message

Expand Down
52 changes: 20 additions & 32 deletions src/plugins/waifu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import contextlib
import random
from venv import logger
import nonebot
from datetime import datetime, timedelta
from nonebot.plugin.on import on_command, on_message
from nonebot.permission import SUPERUSER
from nonebot.typing import T_State
Expand All @@ -17,7 +19,6 @@
from .models import *
from .utils import *
from .config import Config
from datetime import datetime, timedelta

__plugin_meta__ = PluginMetadata(name="waifu", description="", usage="", config=Config)

Expand Down Expand Up @@ -99,14 +100,12 @@ async def waifu_rule(bot: Bot, event: GroupMessageEvent, state: T_State) -> bool
group_id = event.group_id
user_id = event.user_id
protect_list = await WaifuProtect.get_or_none(group_id=group_id)
if protect_list is not None:
if user_id in protect_list.user_id:
return False
if protect_list is not None and user_id in protect_list.user_id:
return False
at = get_message_at(event.message)
at = at[0] if at else None
if protect_list is not None:
if at in protect_list.user_id:
return False
if protect_list is not None and at in protect_list.user_id:
return False
tips = "伱的群友結婚对象是、"
rec, _ = await WaifuCP.get_or_create(group_id=group_id)
rec = rec.affect
Expand Down Expand Up @@ -191,9 +190,9 @@ async def waifu_rule(bot: Bot, event: GroupMessageEvent, state: T_State) -> bool

@waifu.handle()
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
group_id = event.group_id
user_id = event.user_id
waifu_id, tips = state["waifu"]
group_id = event.group_id
if waifu_id == user_id:
record_cp, _ = await WaifuCP.get_or_create(group_id=group_id)
record_cp.affect[user_id] = user_id
Expand All @@ -206,9 +205,7 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
waifu_cp = rec[str(waifu_id)]
member = await bot.get_group_member_info(group_id=group_id, user_id=waifu_cp)
msg = (
"人家已经名花有主了~"
+ MessageSegment.image(file=await user_img(waifu_cp))
+ "ta的cp:"
f"人家已经名花有主了~{MessageSegment.image(file=await user_img(waifu_cp))}ta的cp:"
+ (member["card"] or member["nickname"])
)
record_lock, _ = await WaifuLock.get_or_create(group_id=group_id)
Expand All @@ -220,10 +217,8 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
record_CP.affect[user_id] = user_id
else:
rec.pop(waifu_cp)
try:
with contextlib.suppress(Exception):
record_waifu.waifu.remove(waifu_cp)
except:
pass
await waifu.send(msg + "\n但是...", at_sender=True)
await asyncio.sleep(1)
record_CP, _ = await WaifuCP.get_or_create(group_id=group_id)
Expand Down Expand Up @@ -280,10 +275,8 @@ async def _(event: GroupMessageEvent):
rec.affect.pop(str(user_id))
rec.affect.pop(str(waifu_id))
waifu_set.waifu.remove(waifu_id)
try:
with contextlib.suppress(Exception):
waifu_set.waifu.remove(user_id)
except:
pass
record_lock, _ = await WaifuLock.get_or_create(group_id=group_id)
if group_id in record_lock.lock:
if waifu_id in record_lock.lock:
Expand Down Expand Up @@ -364,14 +357,14 @@ async def _(bot: Bot, event: GroupMessageEvent):
try:
member = await bot.get_group_member_info(group_id=group_id, user_id=user_id)
niknameA = member["card"] or member["nickname"]
except:
except Exception:
niknameA = ""
try:
member = await bot.get_group_member_info(
group_id=group_id, user_id=waifu_id
)
niknameB = member["card"] or member["nickname"]
except:
except Exception:
niknameB = ""

msg += f"♥ {niknameA} | {niknameB}\n"
Expand Down Expand Up @@ -402,7 +395,7 @@ async def yinpa_rule(bot: Bot, event: GroupMessageEvent, state: T_State) -> bool
if at in protect_set:
return False
if at == user_id:
msg = f"恭喜你涩到了你自己!" + MessageSegment.image(file=await user_img(user_id))
msg = f"恭喜你涩到了你自己!{MessageSegment.image(file=await user_img(user_id))}"
await bot.send(event, msg, at_sender=True)
return False
X = random.randint(1, 100)
Expand Down Expand Up @@ -470,7 +463,6 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
@yinpa_list.handle()
async def _(bot: Bot, event: GroupMessageEvent):
group_id = event.group_id
msg_list = []
# 输出卡池
member_list = await bot.get_group_member_list(group_id=event.group_id)
lastmonth = event.time - last_sent_time_filter
Expand All @@ -482,11 +474,10 @@ async def _(bot: Bot, event: GroupMessageEvent):
and member["last_sent_time"] > lastmonth
]
member_list.sort(key=lambda x: x["last_sent_time"], reverse=True)
msg = "卡池:\n——————————————\n"
msg += "\n".join(
msg = "卡池:\n——————————————\n" + "\n".join(
[(member["card"] or member["nickname"]) for member in member_list[:80]]
)
msg_list.append(
msg_list = [
{
"type": "node",
"data": {
Expand All @@ -495,8 +486,7 @@ async def _(bot: Bot, event: GroupMessageEvent):
"content": MessageSegment.image(text_to_png(msg)),
},
}
)

]
# 输出透群友记录

record = [
Expand All @@ -505,13 +495,12 @@ async def _(bot: Bot, event: GroupMessageEvent):
if (times := await Waifuyinppa1.get_or_none(user_id=member["user_id"]))
]
record.sort(key=lambda x: x[1], reverse=True)
msg = "\n".join(
if msg := "\n".join(
[
f"[align=left]{nickname}[/align][align=right]今日透群友 {times} 次[/align]"
for nickname, times in record
]
)
if msg:
):
msg_list.append(
{
"type": "node",
Expand All @@ -535,13 +524,12 @@ async def _(bot: Bot, event: GroupMessageEvent):
record.sort(key=lambda x: x[1], reverse=True)

msg = "涩涩记录②:\n——————————————\n"
msg = "\n".join(
if msg := "\n".join(
[
f"[align=left]{nickname}[/align][align=right]今日被透 {times} 次[/align]"
for nickname, times in record
]
)
if msg:
):
msg_list.append(
{
"type": "node",
Expand Down

0 comments on commit 02d8961

Please sign in to comment.