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:
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)

View File

@ -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.
"""

View File

@ -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