Command names instead of numbers

This commit is contained in:
Dylan Hackworth 2020-03-29 17:04:10 -05:00
parent 4c693205a7
commit 9b00214dd3
1 changed files with 63 additions and 47 deletions

View File

@ -6,59 +6,62 @@ import shlex
class MatrixModule(BotModule): class MatrixModule(BotModule):
def __init__(self, name): def __init__(self, name):
super().__init__(name) super().__init__(name)
self.commands = [] self.commands = {}
async def matrix_message(self, bot, room, event): async def matrix_message(self, bot, room, event):
args = shlex.split(event.body) args = shlex.split(event.body)
args.pop(0) args.pop(0)
if len(args) >= 2: # Message body possibilities:
# Get full command without using quotation marks # ["run", "echo", "Hello", "world"]
command = f"{args[1]}" if args[0] == 'run':
for part_of_command in args[2:]: command_body = MatrixModule.stitch(args[1:])
command += f" {part_of_command}" bot.must_be_owner(event)
out = self.run_command(command_body, event.sender, room.display_name)
if len(args) == 2: await self.send_output(bot, room, out)
if args[0] == 'run': # Message body possibilities:
bot.must_be_owner(event) # ["remove", "command_name"]
out = self.run_command(args[1], event.sender, room.display_name) elif args[0] == 'remove':
await self.send_output(bot, room, out) command_name = args[1]
if args[0] == 'remove': bot.must_be_owner(event)
bot.must_be_owner(event) if command_name in self.commands:
command_number = int(args[1]) await bot.send_text(room, f'Removed "{self.commands[command_name]}"')
if self.commands[command_number] is not None: del self.commands[command_name]
await bot.send_text(room, f'Removed "{self.commands[command_number]}"')
self.commands.pop(command_number)
bot.save_settings()
else:
await bot.send_text(room, f'Could not find command #{command_number}')
elif len(args) == 3:
if args[0] == 'add':
bot.must_be_owner(event)
self.commands.append(command)
bot.save_settings() bot.save_settings()
await bot.send_text(room, 'Command added.')
elif len(args) == 1:
if args[0] == 'list':
if len(self.commands) == 0:
await bot.send_text(room, "No known commands")
else:
list_commands = ""
i = 0
for command_name in self.commands:
list_commands += f"\n - {i}. \"{command_name}\""
i += 1
await bot.send_text(room, 'Known commands: ' + list_commands)
else: else:
command_number = int(args[0]) await bot.send_text(room, f'Could not find command "{command_name}"')
target_command = self.commands[command_number] # Message body possibilities:
if target_command is not None: # ["add", "command_name", "echo", "Hello", "world"]
self.logger.debug( elif args[0] == 'add':
f"room: {room.display_name} sender: {event.sender} wants to run cmd {target_command}" command_name = args[1]
) command_body = MatrixModule.stitch(args[2:])
out = self.run_command(target_command, event.sender, room.display_name) bot.must_be_owner(event)
await self.send_output(bot, room, out) self.commands[command_name] = command_body
else: bot.save_settings()
await bot.send_text(room, 'Unknown command.') await bot.send_text(room, f'Added "{command_name}" -> "{command_body}".')
# Message body possibilities:
# ["list"]
elif args[0] == 'list':
if len(self.commands) == 0:
await bot.send_text(room, "No known commands")
else:
known_commands = "Known commands:\n"
for command_name in self.commands.keys():
command_body = self.commands[command_name]
known_commands += f' - "{command_name}" -> "{command_body}"\n'
await bot.send_text(room, known_commands)
# Message body possibilities:
# ["command_name"]
else:
command_name = args[0]
if command_name in self.commands:
target_command = self.commands[command_name]
self.logger.debug(
f"room: {room.display_name} sender: {event.sender} wants to run cmd {target_command}"
)
out = self.run_command(target_command, event.sender, room.display_name)
await self.send_output(bot, room, out)
else:
await bot.send_text(room, 'Unknown command.')
def help(self): def help(self):
return 'Runs shell commands' return 'Runs shell commands'
@ -73,6 +76,19 @@ class MatrixModule(BotModule):
if data.get('commands'): if data.get('commands'):
self.commands = data['commands'] self.commands = data['commands']
@staticmethod
def stitch(body: list) -> str:
"""
This is used for stitching arguments again
Examples:
["echo", "Hello", "world"] -> "echo Hello world"
Args:
body: str[]
Returns: str
"""
return " ".join(body)
def run_command(self, command, user, roomname): def run_command(self, command, user, roomname):
self.logger.info(f"Running command {command}..") self.logger.info(f"Running command {command}..")
environment = {'MATRIX_USER': user, 'MATRIX_ROOM': roomname} environment = {'MATRIX_USER': user, 'MATRIX_ROOM': roomname}