Added support for multiple prefixes
parent
27c082ece7
commit
f344562702
|
@ -7,7 +7,8 @@ import yaml
|
|||
class Frank(discord.Client):
|
||||
PREFIX = "fr"
|
||||
|
||||
def __init__(self, modules: List["Module"], config_file: str = "frank.yaml"):
|
||||
def __init__(self, modules: List["Module"],
|
||||
config_file: str = "frank.yaml"):
|
||||
super().__init__()
|
||||
self._modules = modules
|
||||
self._loaded_modules = []
|
||||
|
@ -38,10 +39,10 @@ class Frank(discord.Client):
|
|||
async def on_message(self, message: str):
|
||||
cmd = shlex.split(message.content.strip())
|
||||
|
||||
if cmd[0] == "fr":
|
||||
matched_mods = (mod for mod in self._loaded_modules if mod.PREFIX == cmd[1])
|
||||
if cmd[0] == self.PREFIX:
|
||||
matched_mods = (mod for mod in self._loaded_modules if
|
||||
mod.match(cmd[1]))
|
||||
module = next(matched_mods, None)
|
||||
|
||||
if module:
|
||||
await module.command(cmd[2:])
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ from typing import List, Dict
|
|||
|
||||
|
||||
class Module:
|
||||
PREFIX = None
|
||||
NAME = None
|
||||
PREFIX = []
|
||||
NAME = ""
|
||||
|
||||
def __init__(self, client: "Frank", config: Dict = None):
|
||||
self._client = client
|
||||
|
@ -14,3 +14,19 @@ class Module:
|
|||
|
||||
async def command(self, cmd: List[str]):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def match(cls, s: str) -> bool:
|
||||
"""
|
||||
Checks wether the given string is a prefix in the module.
|
||||
"""
|
||||
|
||||
if cls.PREFIX:
|
||||
if isinstance(cls.PREFIX, list):
|
||||
return s in cls.PREFIX
|
||||
|
||||
else:
|
||||
return s == cls.PREFIX
|
||||
|
||||
else:
|
||||
return False
|
||||
|
|
|
@ -30,5 +30,3 @@ class McStat(Module):
|
|||
|
||||
else:
|
||||
await channel.send("No one is here bro")
|
||||
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ from .. import Module
|
|||
|
||||
|
||||
class TestMod(Module):
|
||||
|
||||
PREFIX = "test"
|
||||
NAME = "test"
|
||||
|
||||
|
|
Reference in New Issue