Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 提供一个插件好感度限制 #1846

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions zhenxun/builtin_plugins/hooks/_auth_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from zhenxun.models.level_user import LevelUser
from zhenxun.models.plugin_info import PluginInfo
from zhenxun.models.plugin_limit import PluginLimit
from zhenxun.models.sign_user import SignUser
from zhenxun.models.user_console import UserConsole
from zhenxun.services.log import logger
from zhenxun.utils.enum import (
Expand Down Expand Up @@ -210,7 +211,9 @@ def is_send_limit_message(self, plugin: PluginInfo, sid: str) -> bool:
return False
if plugin.plugin_type == PluginType.DEPENDANT:
return False
return plugin.module != "ai" if self._flmt_s.check(sid) else False
if plugin.ignore_prompt:
return False
return self._flmt_s.check(sid)
Comment on lines +214 to +216
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): We've found these issues:

Suggested change
if plugin.ignore_prompt:
return False
return self._flmt_s.check(sid)
return False if plugin.ignore_prompt else self._flmt_s.check(sid)


async def auth(
self,
Expand Down Expand Up @@ -358,14 +361,28 @@ async def auth_plugin(
group_id = channel_id
channel_id = None
if user_id := session.id1:
is_poke = isinstance(event, PokeNotifyEvent)
if plugin.impression > 0:
sign_user = await SignUser.get_user(user_id)
if float(sign_user.impression) < plugin.impression:
if self.is_send_limit_message(plugin, user_id):
self._flmt_s.start_cd(user_id)
await MessageUtils.build_message(
f"好感度不足哦,当前功能需要好感度: {plugin.impression},"
"请继续签到提升好感度吧!"
).send(reply_to=True)
logger.debug(
f"{plugin.name}({plugin.module}) 用户好感度不足...",
"AuthChecker",
session=session,
)
raise IgnoredException("好感度不足...")
if group_id:
sid = group_id or user_id
if await GroupConsole.is_superuser_block_plugin(
group_id, plugin.module
):
"""超级用户群组插件状态"""
if self.is_send_limit_message(plugin, sid) and not is_poke:
if self.is_send_limit_message(plugin, sid):
self._flmt_s.start_cd(group_id or user_id)
await MessageUtils.build_message(
"超级管理员禁用了该群此功能..."
Expand All @@ -378,7 +395,7 @@ async def auth_plugin(
raise IgnoredException("超级管理员禁用了该群此功能...")
if await GroupConsole.is_normal_block_plugin(group_id, plugin.module):
"""群组插件状态"""
if self.is_send_limit_message(plugin, sid) and not is_poke:
if self.is_send_limit_message(plugin, sid):
self._flmt_s.start_cd(group_id or user_id)
await MessageUtils.build_message("该群未开启此功能...").send(
reply_to=True
Expand All @@ -392,7 +409,7 @@ async def auth_plugin(
if plugin.block_type == BlockType.GROUP:
"""全局群组禁用"""
try:
if self.is_send_limit_message(plugin, sid) and not is_poke:
if self.is_send_limit_message(plugin, sid):
self._flmt_c.start_cd(group_id)
await MessageUtils.build_message(
"该功能在群组中已被禁用..."
Expand All @@ -415,7 +432,7 @@ async def auth_plugin(
if plugin.block_type == BlockType.PRIVATE:
"""全局私聊禁用"""
try:
if self.is_send_limit_message(plugin, sid) and not is_poke:
if self.is_send_limit_message(plugin, sid):
self._flmt_c.start_cd(user_id)
await MessageUtils.build_message(
"该功能在私聊中已被禁用..."
Expand All @@ -442,7 +459,7 @@ async def auth_plugin(
"AuthChecker",
session=session,
)
if self.is_send_limit_message(plugin, sid) and not is_poke:
if self.is_send_limit_message(plugin, sid):
self._flmt_s.start_cd(group_id or user_id)
await MessageUtils.build_message("全局未开启此功能...").send()
raise IgnoredException("全局未开启此功能...")
Expand Down
2 changes: 1 addition & 1 deletion zhenxun/builtin_plugins/init/init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async def _handle_setting(
is_show=extra_data.is_show,
ignore_prompt=extra_data.ignore_prompt,
parent=(plugin.parent_plugin.module_name if plugin.parent_plugin else None),
impression=setting.impression,
)
)
if extra_data.limits:
Expand Down Expand Up @@ -123,7 +124,6 @@ async def _():
"admin_level",
"plugin_type",
"is_show",
"ignore_prompt",
]
)
update_list.append(plugin)
Expand Down
2 changes: 2 additions & 0 deletions zhenxun/configs/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class PluginSetting(BaseModel):
"""是否限制超级用户"""
cost_gold: int = 0
"""调用插件花费金币"""
impression: float = 0.0
"""调用插件好感度限制"""


class SchedulerModel(BaseModel):
Expand Down
3 changes: 3 additions & 0 deletions zhenxun/models/plugin_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class PluginInfo(Model):
"""父插件"""
is_show = fields.BooleanField(default=True, description="是否显示在帮助中")
"""是否显示在帮助中"""
impression = fields.FloatField(default=0, description="插件好感度限制")
"""插件好感度限制"""

class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
table = "plugin_info"
Expand Down Expand Up @@ -87,4 +89,5 @@ async def _run_script(cls):
"ALTER TABLE plugin_info ADD COLUMN parent character varying(255);",
"ALTER TABLE plugin_info ADD COLUMN is_show boolean DEFAULT true;",
"ALTER TABLE plugin_info ADD COLUMN ignore_prompt boolean DEFAULT false;",
"ALTER TABLE plugin_info ADD COLUMN impression float DEFAULT 0;",
]
18 changes: 18 additions & 0 deletions zhenxun/models/sign_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
table = "sign_users"
table_description = "用户签到数据表"

@classmethod
async def get_user(cls, user_id: str, platform: str | None = None) -> "SignUser":
"""获取签到用户

参数:
user_id: 用户id
platform: 平台.

返回:
Self: SignUser
"""
user_console = await UserConsole.get_user(user_id, platform)
user, _ = await SignUser.get_or_create(
user_id=user_id,
defaults={"user_console": user_console, "platform": platform},
)
return user

@classmethod
async def sign(
cls,
Expand Down
16 changes: 16 additions & 0 deletions zhenxun/utils/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ def random(cls, query, limit: int = 1) -> str:
f"Unsupported database type: {db_class_name}", query.__module__
)
return query

@classmethod
def add_column(
cls,
table_name: str,
column_name: str,
column_type: str,
default: str | None = None,
not_null: bool = False,
) -> str:
sql = f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type}"
if default:
sql += f" DEFAULT {default}"
if not_null:
sql += " NOT NULL"
return sql
Loading