66import dico
77from .command import Command
88from .context import Context
9- from .exception import InvalidArgument , InvalidModule , MissingLoadFunction , MissingUnloadFunction , ModuleNotLoaded
9+ from .exception import *
1010from .utils import smart_split , is_coro
1111
1212if typing .TYPE_CHECKING :
@@ -25,6 +25,7 @@ def __init__(self,
2525 super ().__init__ (token , intents = intents , default_allowed_mentions = default_allowed_mentions , loop = loop , cache = cache )
2626 self .prefixes = [prefix ] if not isinstance (prefix , list ) else prefix
2727 self .commands = {}
28+ self .aliases = {}
2829 self .logger = logging .Logger ("dico_command" )
2930 self .on ("MESSAGE_CREATE" , self .execute_handler )
3031 self .addons : typing .List [Addon ] = []
@@ -48,10 +49,10 @@ async def execute_handler(self, message: dico.Message):
4849 raw_ipt = cont [len (prefix_result ):]
4950 ipt = raw_ipt .split (maxsplit = 1 )
5051 name = ipt [0 ]
51- cmd = self .commands .get (name )
52+ cmd = self .commands .get (self . aliases . get ( name , name ) )
5253 if not cmd :
5354 return
54- context = Context .from_message (message , prefix_result , cmd )
55+ context = Context .from_message (message , prefix_result , cmd , name )
5556 try :
5657 try :
5758 args , kwargs = smart_split (ipt [1 ] if len (ipt ) > 1 else "" , cmd .args_data )
@@ -64,18 +65,25 @@ async def execute_handler(self, message: dico.Message):
6465
6566 def add_command (self , command : Command ):
6667 if command .name in self .commands :
67- raise
68+ raise CommandAlreadyExists ( name = command . name )
6869 self .commands [command .name ] = command
70+ for x in command .aliases :
71+ if x in self .aliases :
72+ raise CommandAlreadyExists (name = x )
73+ self .aliases [x ] = command .name
6974 return command
7075
7176 def remove_command (self , name : str ):
7277 if name not in self .commands :
73- raise
74- del self .commands [name ]
78+ return
79+ command = self .commands .pop (name )
80+ for x in command .aliases :
81+ if x in self .aliases :
82+ del self .aliases [x ]
7583
76- def command (self , name : str = None ):
84+ def command (self , name : typing . Optional [ str ] = None , * , aliases : typing . Optional [ typing . List [ str ]] = None ):
7785 def wrap (func ):
78- cmd = Command (func , name or func .__name__ )
86+ cmd = Command (func , name or func .__name__ , aliases = aliases )
7987 self .add_command (cmd )
8088 return cmd
8189 return wrap
@@ -89,7 +97,7 @@ def handle_command_error(self, context, ex):
8997 def load_addons (self , * addons : typing .Type ["Addon" ]):
9098 for x in addons :
9199 if x .name in self .addon_names :
92- raise
100+ raise AddonAlreadyLoaded ( name = x . name )
93101 self .addon_names .append (x .name )
94102 loaded = x (self )
95103 self .addons .append (loaded )
@@ -130,13 +138,15 @@ def load_module(self, import_path: str):
130138 try :
131139 module = importlib .import_module (import_path )
132140 importlib .reload (module )
141+ if module .__name__ in self .modules :
142+ raise ModuleAlreadyLoaded (path = import_path )
133143 self .modules .append (module .__name__ )
134144 if hasattr (module , "load" ):
135145 module .load (self )
136146 else :
137- raise MissingLoadFunction
147+ raise MissingLoadFunction ( path = import_path )
138148 except ImportError :
139- raise InvalidModule
149+ raise InvalidModule ( path = import_path )
140150
141151 def unload_module (self , import_path : str ):
142152 try :
@@ -146,11 +156,11 @@ def unload_module(self, import_path: str):
146156 module .unload (self )
147157 self .modules .remove (module .__name__ )
148158 else :
149- raise MissingUnloadFunction
159+ raise MissingUnloadFunction ( path = import_path )
150160 else :
151- raise ModuleNotLoaded
161+ raise ModuleNotLoaded ( path = import_path )
152162 except ImportError :
153- raise InvalidModule
163+ raise InvalidModule ( path = import_path )
154164
155165 def reload_module (self , import_path : str ):
156166 self .unload_module (import_path )
0 commit comments