Added support for multiple prefixes

master
jef 2020-08-09 18:36:13 +02:00
parent 27c082ece7
commit f344562702
4 changed files with 23 additions and 9 deletions

View File

@ -7,7 +7,8 @@ import yaml
class Frank(discord.Client): class Frank(discord.Client):
PREFIX = "fr" 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__() super().__init__()
self._modules = modules self._modules = modules
self._loaded_modules = [] self._loaded_modules = []
@ -38,10 +39,10 @@ class Frank(discord.Client):
async def on_message(self, message: str): async def on_message(self, message: str):
cmd = shlex.split(message.content.strip()) cmd = shlex.split(message.content.strip())
if cmd[0] == "fr": if cmd[0] == self.PREFIX:
matched_mods = (mod for mod in self._loaded_modules if mod.PREFIX == cmd[1]) matched_mods = (mod for mod in self._loaded_modules if
mod.match(cmd[1]))
module = next(matched_mods, None) module = next(matched_mods, None)
if module: if module:
await module.command(cmd[2:]) await module.command(cmd[2:])

View File

@ -2,8 +2,8 @@ from typing import List, Dict
class Module: class Module:
PREFIX = None PREFIX = []
NAME = None NAME = ""
def __init__(self, client: "Frank", config: Dict = None): def __init__(self, client: "Frank", config: Dict = None):
self._client = client self._client = client
@ -14,3 +14,19 @@ class Module:
async def command(self, cmd: List[str]): async def command(self, cmd: List[str]):
pass 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

View File

@ -30,5 +30,3 @@ class McStat(Module):
else: else:
await channel.send("No one is here bro") await channel.send("No one is here bro")

View File

@ -3,7 +3,6 @@ from .. import Module
class TestMod(Module): class TestMod(Module):
PREFIX = "test" PREFIX = "test"
NAME = "test" NAME = "test"