diff --git a/frank/frank.py b/frank/frank.py index 62c52ab..2a8a01e 100644 --- a/frank/frank.py +++ b/frank/frank.py @@ -88,10 +88,13 @@ class Frank(discord.Client): except ValueError: return - if cmd and cmd[0] == self.PREFIX: - module = next((mod for mod in self._loaded_modules - if mod.match(cmd[1])), None) + # Exit if no commands are given or prefix is wrong + if not (cmd and cmd[0] == self.PREFIX): + return - if module: - await module(cmd=cmd[2:], author=message.author, - channel=message.channel, mid=message.id) + module = next((mod for mod in self._loaded_modules + if mod.match(cmd[1])), None) + + if module: + await module(cmd=cmd[2:], author=message.author, + channel=message.channel, mid=message.id) diff --git a/frank/module/decorators/classes.py b/frank/module/decorators/classes.py index 11f17a3..3b1c187 100644 --- a/frank/module/decorators/classes.py +++ b/frank/module/decorators/classes.py @@ -102,7 +102,7 @@ class RegexCommand(Command): class Daemon(Simple): """ - Represents a daemon aka a background process. + Represents a daemon a.k.a. a background process. """ def __init__(self, func: callable, interval: Union[int, float] = 0): @@ -122,6 +122,7 @@ class Daemon(Simple): if interval > 0: async def loop(self, *args, **kwargs): while True: + # TODO: does this make func and sleep run at the same time? await func(self, *args, **kwargs) await asyncio.sleep(interval) @@ -131,7 +132,7 @@ class Daemon(Simple): class Default(Simple): """ - Represents a default command (a.k.a. when the module is called without a + Represents a default command a.k.a. when the module is called without a command. """ diff --git a/frank/module/module.py b/frank/module/module.py index 5facd13..3c9dbf4 100644 --- a/frank/module/module.py +++ b/frank/module/module.py @@ -100,18 +100,18 @@ class Module(ModuleMeta): func = next((func for func in self.commands if func.match(cmd[0])), None) - if func: - # A RegexCommand can use the prefix, as it's not a fixed string - if isinstance(func, RegexCommand): - await func(prefix=cmd[0], cmd=cmd[1:], author=author, - channel=channel, mid=mid) + # Throw error if no function is found + if not func: + raise InvalidCommand(f'Unknown command: {cmd}') - else: - await func(cmd=cmd[1:], author=author, channel=channel, - mid=mid) + # A RegexCommand can use the prefix, as it's not a fixed string + if isinstance(func, RegexCommand): + await func(prefix=cmd[0], cmd=cmd[1:], author=author, + channel=channel, mid=mid) else: - raise InvalidCommand(f'Unknown command: {cmd}') + await func(cmd=cmd[1:], author=author, channel=channel, + mid=mid) elif self.default: await self.default(author=author, channel=channel, mid=mid) @@ -125,12 +125,12 @@ class Module(ModuleMeta): prefix: prefix to check """ - if cls.PREFIX: - if isinstance(cls.PREFIX, list): - return prefix in cls.PREFIX + # Always return False if there's no PREFIX defined + if not cls.PREFIX: + return False - else: - return prefix == cls.PREFIX + if isinstance(cls.PREFIX, list): + return prefix in cls.PREFIX else: - return False + return prefix == cls.PREFIX