mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-29 01:01:24 +00:00
Warn if failed to register command
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
|
import warnings
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import (
|
from typing import (
|
||||||
Tuple, Union, Callable, Iterable, Any, Optional, List, Dict,
|
Tuple, Union, Callable, Iterable, Any, Optional, List, Dict,
|
||||||
@ -130,6 +131,12 @@ class Command:
|
|||||||
return await perm.check_permission(session.bot, session.ctx,
|
return await perm.check_permission(session.bot, session.ctx,
|
||||||
self.permission)
|
self.permission)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<Command, name={self.name.__repr__()}>'
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.__repr__()
|
||||||
|
|
||||||
|
|
||||||
class CommandFunc:
|
class CommandFunc:
|
||||||
__slots__ = ('cmd', 'func')
|
__slots__ = ('cmd', 'func')
|
||||||
@ -187,6 +194,12 @@ def on_command(name: Union[str, CommandName_T], *,
|
|||||||
for parent_key in cmd_name[:-1]:
|
for parent_key in cmd_name[:-1]:
|
||||||
current_parent[parent_key] = current_parent.get(parent_key) or {}
|
current_parent[parent_key] = current_parent.get(parent_key) or {}
|
||||||
current_parent = current_parent[parent_key]
|
current_parent = current_parent[parent_key]
|
||||||
|
if not isinstance(current_parent, dict):
|
||||||
|
warnings.warn(f'{current_parent} is not a registry dict')
|
||||||
|
return func
|
||||||
|
if cmd_name[-1] in current_parent:
|
||||||
|
warnings.warn(f'There is already a command named {cmd_name}')
|
||||||
|
return func
|
||||||
current_parent[cmd_name[-1]] = cmd
|
current_parent[cmd_name[-1]] = cmd
|
||||||
|
|
||||||
nonlocal aliases
|
nonlocal aliases
|
||||||
|
Reference in New Issue
Block a user