Skip to content

Commit

Permalink
✨ conflict resolve for command
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Aug 15, 2024
1 parent 3e8107e commit 1c37685
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 3 deletions.
3 changes: 2 additions & 1 deletion example/.env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ SATORI_CLIENTS='
}
]
'
SUPERUSERS=["3165388245"]
SUPERUSERS=["3165388245"]
ALCONNA_CONFLICT_RESOLVER="merge"
3 changes: 3 additions & 0 deletions src/nonebot_plugin_alconna/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ class Config(BaseModel):

alconna_builtin_plugins: set[str] = Field(default_factory=set)
"""需要加载的alc内置插件集合"""

alconna_conflict_resolver: Literal["raise", "default", "ignore", "replace"] = Field(default="default")
"""命令冲突解决策略,default 为保留两个命令,raise 为抛出异常,ignore 为忽略新命令,replace 为替换旧命令"""
5 changes: 5 additions & 0 deletions src/nonebot_plugin_alconna/i18n/.lang.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
"description": "value of lang item type 'discord_prefix'",
"type": "string"
},
"existed_command": {
"title": "existed_command",
"description": "value of lang item type 'existed_command'",
"type": "string"
},
"extension": {
"title": "Extension",
"description": "Scope 'extension' of lang item",
Expand Down
1 change: 1 addition & 0 deletions src/nonebot_plugin_alconna/i18n/.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"subtype": "error",
"types": [
"discord_prefix",
"existed_command",
{
"subtype": "extension",
"types": [
Expand Down
1 change: 1 addition & 0 deletions src/nonebot_plugin_alconna/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"error": {
"discord_prefix": "The Alconna obj must have '/' prefix when use to translate to Discord slash-command",
"existed_command": "Command {cmd} already existed",
"extension": {
"forbid_exclude": "Extension which id starts with '!' cannot be excluded",
"path_load": "Value of {path} is not a subclass of Extension",
Expand Down
1 change: 1 addition & 0 deletions src/nonebot_plugin_alconna/i18n/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NbpAlcErrorExtension:

class NbpAlcError:
discord_prefix: LangItem = LangItem("nbp-alc", "error.discord_prefix")
existed_command: LangItem = LangItem("nbp-alc", "error.existed_command")
extension = NbpAlcErrorExtension
matcher_got_path: LangItem = LangItem("nbp-alc", "error.matcher_got_path")

Expand Down
1 change: 1 addition & 0 deletions src/nonebot_plugin_alconna/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"error": {
"discord_prefix": "Alconna 命令对象在用于转换为 Discord 斜杠命令时必须具有 '/' 前缀",
"existed_command": "Alconna 命令对象 {cmd} 已经存在",
"extension": {
"forbid_exclude": "不能排除 id 以“!”开头的扩展",
"path_load": "{path} 的值不是扩展的子类",
Expand Down
31 changes: 29 additions & 2 deletions src/nonebot_plugin_alconna/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from nonebot.params import Depends
from nonebot.utils import escape_tag
from tarina.lang.model import LangItem
from nonebot import require, get_driver
from nonebot.permission import Permission
from nonebot.dependencies import Dependent
from nonebot.message import run_postprocessor
Expand All @@ -25,6 +24,7 @@
from arclet.alconna.typing import ShortcutRegWrapper
from nepattern import ANY, STRING, TPattern, AnyString
from _weakref import _remove_dead_weakref # type: ignore
from nonebot import require, get_driver, get_plugin_config
from arclet.alconna.tools import AlconnaFormat, AlconnaString
from nonebot.plugin.on import store_matcher, get_matcher_source
from arclet.alconna.tools.construct import FuncMounter, MountConfig
Expand All @@ -37,6 +37,7 @@

from .i18n import Lang
from .rule import alconna
from .config import Config
from .typings import MReturn
from .util import annotation
from .pattern import patterns
Expand Down Expand Up @@ -67,6 +68,14 @@ class ArgsMounter(Protocol):
args: Args


try:
global_config = get_driver().config
config = get_plugin_config(Config)
conflict_resolver = config.alconna_conflict_resolver
except ValueError:
conflict_resolver = "ignore"


def extract_arg(path: str, target: ArgsMounter | None) -> Arg | None:
"""从 Alconna 中提取参数"""
if not target:
Expand Down Expand Up @@ -934,7 +943,25 @@ def on_alconna(
state: 默认 state
"""
if isinstance(command, str):
command = AlconnaFormat(command)
command = AlconnaFormat(command, union=False)
try:
exist = command_manager.get_command(command.path)
if exist != command and (_matcher := referent(exist)):
if conflict_resolver == "raise":
raise RuntimeError(Lang.nbp_alc.error.existed_command(cmd=command.path))
if conflict_resolver == "ignore":
command_manager.delete(command)
return _matcher
if conflict_resolver == "replace":
_matcher.destroy()
command_manager.delete(exist)
command_manager.register(command)
else:
exist.formatter.remove(command)
command.formatter = command.formatter.__class__()
command.formatter.add(command)
except ValueError:
pass
_rule = alconna(
command,
skip_for_unmatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ async def send_to(self, target: Union[Target, Event], bot: Bot, message: Message
if isinstance(target, Event):
return await bot.send(target, message, **kwargs) # type: ignore
if _target.private:
if _target.parent_id:
return await bot.send_msg(
message_type="private",
user_id=int(_target.id),
group_id=int(_target.parent_id),
message=message,
**kwargs,
)
return await bot.send_msg(message_type="private", user_id=int(_target.id), message=message, **kwargs)
else:
return await bot.send_msg(message_type="group", group_id=int(_target.id), message=message, **kwargs)
Expand Down

0 comments on commit 1c37685

Please sign in to comment.