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,10 +88,13 @@ 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
module = next((mod for mod in self._loaded_modules if not (cmd and cmd[0] == self.PREFIX):
if mod.match(cmd[1])), None) return
if module: module = next((mod for mod in self._loaded_modules
await module(cmd=cmd[2:], author=message.author, if mod.match(cmd[1])), None)
channel=message.channel, mid=message.id)
if module:
await module(cmd=cmd[2:], author=message.author,
channel=message.channel, mid=message.id)

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,18 +100,18 @@ 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
# A RegexCommand can use the prefix, as it's not a fixed string if not func:
if isinstance(func, RegexCommand): raise InvalidCommand(f'Unknown command: {cmd}')
await func(prefix=cmd[0], cmd=cmd[1:], author=author,
channel=channel, mid=mid)
else: # A RegexCommand can use the prefix, as it's not a fixed string
await func(cmd=cmd[1:], author=author, channel=channel, if isinstance(func, RegexCommand):
mid=mid) await func(prefix=cmd[0], cmd=cmd[1:], author=author,
channel=channel, mid=mid)
else: else:
raise InvalidCommand(f'Unknown command: {cmd}') await func(cmd=cmd[1:], author=author, channel=channel,
mid=mid)
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 isinstance(cls.PREFIX, list): if not cls.PREFIX:
return prefix in cls.PREFIX return False
else: if isinstance(cls.PREFIX, list):
return prefix == cls.PREFIX return prefix in cls.PREFIX
else: else:
return False return prefix == cls.PREFIX