Restructure command parsing
This commit is contained in:
parent
ac04e7fd30
commit
aee21f359c
2 changed files with 47 additions and 42 deletions
|
|
@ -13,21 +13,25 @@ logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
class ExampleBot(yaboli.Bot):
|
class ExampleBot(yaboli.Bot):
|
||||||
async def on_send(self, room, message):
|
async def on_command_specific(self, room, message, command, nick, argstr):
|
||||||
ping = "ExamplePong!"
|
|
||||||
short_help = "Example bot for the yaboli bot library"
|
|
||||||
long_help = (
|
long_help = (
|
||||||
"I'm an example bot for the yaboli bot library,"
|
"I'm an example bot for the yaboli bot library,"
|
||||||
" which can be found at https://github.com/Garmelon/yaboli"
|
" which can be found at https://github.com/Garmelon/yaboli"
|
||||||
)
|
)
|
||||||
|
|
||||||
await self.botrulez_ping_general(room, message, text=ping)
|
if similar(nick, room.session.nick) and not argstr:
|
||||||
await self.botrulez_ping_specific(room, message, text=ping)
|
await self.botrulez_ping(room, message, command, text="ExamplePong!")
|
||||||
await self.botrulez_help_general(room, message, text=short_help)
|
await self.botrulez_help(room, message, command, text=long_help)
|
||||||
await self.botrulez_help_specific(room, message, text=long_help)
|
await self.botrulez_uptime(room, message, command)
|
||||||
await self.botrulez_uptime(room, message)
|
await self.botrulez_kill(room, message, command)
|
||||||
await self.botrulez_kill(room, message, text="/me dies spectacularly")
|
await self.botrulez_restart(room, message, command)
|
||||||
await self.botrulez_restart(room, message, text="/me restarts spectacularly")
|
|
||||||
|
async def on_command_general(self, room, message, command, argstr):
|
||||||
|
short_help = "Example bot for the yaboli bot library"
|
||||||
|
|
||||||
|
if not argstr:
|
||||||
|
await self.botrulez_ping(room, message, command, text="ExamplePong!")
|
||||||
|
await self.botrulez_help(room, message, command, text=short_help)
|
||||||
|
|
||||||
def main(configfile):
|
def main(configfile):
|
||||||
config = configparser.ConfigParser(allow_no_value=True)
|
config = configparser.ConfigParser(allow_no_value=True)
|
||||||
|
|
@ -35,7 +39,6 @@ def main(configfile):
|
||||||
|
|
||||||
nick = config.get("general", "nick")
|
nick = config.get("general", "nick")
|
||||||
cookiefile = config.get("general", "cookiefile", fallback=None)
|
cookiefile = config.get("general", "cookiefile", fallback=None)
|
||||||
print(cookiefile)
|
|
||||||
bot = ExampleBot(nick, cookiefile=cookiefile)
|
bot = ExampleBot(nick, cookiefile=cookiefile)
|
||||||
|
|
||||||
for room, password in config.items("rooms"):
|
for room, password in config.items("rooms"):
|
||||||
|
|
|
||||||
|
|
@ -19,23 +19,14 @@ GENERAL_RE = re.compile(r"!(\S+)\s*([\S\s]*)")
|
||||||
# Decorator magic for commands and triggers.
|
# Decorator magic for commands and triggers.
|
||||||
# I think commands could probably be implemented as some kind of triggers,
|
# I think commands could probably be implemented as some kind of triggers,
|
||||||
# but I'm not gonna do that now because commands are working fine this way.
|
# but I'm not gonna do that now because commands are working fine this way.
|
||||||
def command(commandname, specific=True, args=True):
|
def command(*commands):
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
async def wrapper(self, room, message, *args_, **kwargs_):
|
async def wrapper(self, room, message, command, *args, **kwargs):
|
||||||
if specific:
|
if command in commands:
|
||||||
result = self._parse_command(message.content, specific=room.session.nick)
|
await func(self, room, message, *args, **kwargs)
|
||||||
else:
|
|
||||||
result = self._parse_command(message.content)
|
|
||||||
if result is None: return False
|
|
||||||
cmd, argstr = result
|
|
||||||
if cmd != commandname: return False
|
|
||||||
if args:
|
|
||||||
await func(self, room, message, argstr, *args_, **kwargs_)
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
if argstr: return
|
return False
|
||||||
await func(self, room, message, *args_, **kwargs_)
|
|
||||||
return True
|
|
||||||
return wrapper
|
return wrapper
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
@ -77,30 +68,41 @@ class Bot(Inhabitant):
|
||||||
if room:
|
if room:
|
||||||
await room.exit()
|
await room.exit()
|
||||||
|
|
||||||
|
# COMMANDS
|
||||||
|
|
||||||
|
async def on_command_specific(self, room, message, command, nick, argstr):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def on_command_general(self, room, message, command, argstr):
|
||||||
|
pass
|
||||||
|
|
||||||
# INHABITED FUNCTIONS
|
# INHABITED FUNCTIONS
|
||||||
|
|
||||||
|
async def on_send(self, room, message):
|
||||||
|
match = SPECIFIC_RE.fullmatch(message.content)
|
||||||
|
if match:
|
||||||
|
command, nick, argstr = match.groups()
|
||||||
|
await self.on_command_specific(room, message, command, nick, argstr)
|
||||||
|
|
||||||
|
match = GENERAL_RE.fullmatch(message.content)
|
||||||
|
if match:
|
||||||
|
command, argstr = match.groups()
|
||||||
|
await self.on_command_general(room, message, command, argstr)
|
||||||
|
|
||||||
async def on_stopped(self, room):
|
async def on_stopped(self, room):
|
||||||
await self.part_room(room.roomname)
|
await self.part_room(room.roomname)
|
||||||
|
|
||||||
# BOTRULEZ
|
# BOTRULEZ
|
||||||
|
|
||||||
@command("ping", specific=False, args=False)
|
@command("ping")
|
||||||
async def botrulez_ping_general(self, room, message, text="Pong!"):
|
async def botrulez_ping(self, room, message, text="Pong!"):
|
||||||
await room.send(text, message.mid)
|
await room.send(text, message.mid)
|
||||||
|
|
||||||
@command("ping", specific=True, args=False)
|
@command("help")
|
||||||
async def botrulez_ping_specific(self, room, message, text="Pong!"):
|
async def botrulez_help(self, room, message, text="Placeholder help text"):
|
||||||
await room.send(text, message.mid)
|
await room.send(text, message.mid)
|
||||||
|
|
||||||
@command("help", specific=False, args=False)
|
@command("uptime")
|
||||||
async def botrulez_help_general(self, room, message, text="Placeholder help text"):
|
|
||||||
await room.send(text, message.mid)
|
|
||||||
|
|
||||||
@command("help", specific=True, args=False)
|
|
||||||
async def botrulez_help_specific(self, room, message, text="Placeholder help text"):
|
|
||||||
await room.send(text, message.mid)
|
|
||||||
|
|
||||||
@command("uptime", specific=True, args=False)
|
|
||||||
async def botrulez_uptime(self, room, message):
|
async def botrulez_uptime(self, room, message):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
startformat = format_time(room.start_time)
|
startformat = format_time(room.start_time)
|
||||||
|
|
@ -108,12 +110,12 @@ class Bot(Inhabitant):
|
||||||
text = f"/me has been up since {startformat} ({deltaformat})"
|
text = f"/me has been up since {startformat} ({deltaformat})"
|
||||||
await room.send(text, message.mid)
|
await room.send(text, message.mid)
|
||||||
|
|
||||||
@command("kill", specific=True, args=False)
|
@command("kill")
|
||||||
async def botrulez_kill(self, room, message, text="/me dies"):
|
async def botrulez_kill(self, room, message, text="/me dies"):
|
||||||
await room.send(text, message.mid)
|
await room.send(text, message.mid)
|
||||||
await self.part_room(room.roomname)
|
await self.part_room(room.roomname)
|
||||||
|
|
||||||
@command("restart", specific=True, args=False)
|
@command("restart")
|
||||||
async def botrulez_restart(self, room, message, text="/me restarts"):
|
async def botrulez_restart(self, room, message, text="/me restarts"):
|
||||||
await room.send(text, message.mid)
|
await room.send(text, message.mid)
|
||||||
await self.part_room(room.roomname)
|
await self.part_room(room.roomname)
|
||||||
|
|
@ -192,9 +194,9 @@ class Bot(Inhabitant):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse_command(content, specific=None):
|
def _parse_command(content, specific=None):
|
||||||
if specific is not None:
|
if specific:
|
||||||
match = SPECIFIC_RE.fullmatch(content)
|
match = SPECIFIC_RE.fullmatch(content)
|
||||||
if match and similar(match.group(2), specific):
|
if match:
|
||||||
return match.group(1), match.group(3)
|
return match.group(1), match.group(3)
|
||||||
else:
|
else:
|
||||||
match = GENERAL_RE.fullmatch(content)
|
match = GENERAL_RE.fullmatch(content)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue