Flattened code a bit

develop
Jef Roosens 2020-08-27 17:23:38 +02:00
parent e7637a1bce
commit 5648f1a9de
3 changed files with 27 additions and 23 deletions

View File

@ -88,7 +88,10 @@ class Frank(discord.Client):
except ValueError: except ValueError:
return return
if cmd and cmd[0] == self.PREFIX: # Exit if no commands are given or prefix is wrong
if not (cmd and cmd[0] == self.PREFIX):
return
module = next((mod for mod in self._loaded_modules module = next((mod for mod in self._loaded_modules
if mod.match(cmd[1])), None) if mod.match(cmd[1])), None)

View File

@ -102,7 +102,7 @@ class RegexCommand(Command):
class Daemon(Simple): 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): def __init__(self, func: callable, interval: Union[int, float] = 0):
@ -122,6 +122,7 @@ class Daemon(Simple):
if interval > 0: if interval > 0:
async def loop(self, *args, **kwargs): async def loop(self, *args, **kwargs):
while True: while True:
# TODO: does this make func and sleep run at the same time?
await func(self, *args, **kwargs) await func(self, *args, **kwargs)
await asyncio.sleep(interval) await asyncio.sleep(interval)
@ -131,7 +132,7 @@ class Daemon(Simple):
class Default(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. command.
""" """

View File

@ -100,7 +100,10 @@ class Module(ModuleMeta):
func = next((func for func in self.commands func = next((func for func in self.commands
if func.match(cmd[0])), None) if func.match(cmd[0])), None)
if func: # Throw error if no function is found
if not func:
raise InvalidCommand(f'Unknown command: {cmd}')
# A RegexCommand can use the prefix, as it's not a fixed string # A RegexCommand can use the prefix, as it's not a fixed string
if isinstance(func, RegexCommand): if isinstance(func, RegexCommand):
await func(prefix=cmd[0], cmd=cmd[1:], author=author, await func(prefix=cmd[0], cmd=cmd[1:], author=author,
@ -110,9 +113,6 @@ class Module(ModuleMeta):
await func(cmd=cmd[1:], author=author, channel=channel, await func(cmd=cmd[1:], author=author, channel=channel,
mid=mid) mid=mid)
else:
raise InvalidCommand(f'Unknown command: {cmd}')
elif self.default: elif self.default:
await self.default(author=author, channel=channel, mid=mid) await self.default(author=author, channel=channel, mid=mid)
@ -125,12 +125,12 @@ class Module(ModuleMeta):
prefix: prefix to check prefix: prefix to check
""" """
if cls.PREFIX: # Always return False if there's no PREFIX defined
if not cls.PREFIX:
return False
if isinstance(cls.PREFIX, list): if isinstance(cls.PREFIX, list):
return prefix in cls.PREFIX return prefix in cls.PREFIX
else: else:
return prefix == cls.PREFIX return prefix == cls.PREFIX
else:
return False