CMD module refactor
This commit is contained in:
parent
2e78289a2b
commit
4c693205a7
|
@ -4,11 +4,18 @@ import shlex
|
|||
|
||||
|
||||
class MatrixModule(BotModule):
|
||||
commands = dict() # cmd name -> shell command
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.commands = []
|
||||
|
||||
async def matrix_message(self, bot, room, event):
|
||||
args = shlex.split(event.body)
|
||||
args.pop(0)
|
||||
if len(args) >= 2:
|
||||
# Get full command without using quotation marks
|
||||
command = f"{args[1]}"
|
||||
for part_of_command in args[2:]:
|
||||
command += f" {part_of_command}"
|
||||
|
||||
if len(args) == 2:
|
||||
if args[0] == 'run':
|
||||
|
@ -17,26 +24,41 @@ class MatrixModule(BotModule):
|
|||
await self.send_output(bot, room, out)
|
||||
if args[0] == 'remove':
|
||||
bot.must_be_owner(event)
|
||||
cmdname = args[1]
|
||||
self.commands.pop(cmdname, None)
|
||||
bot.save_settings()
|
||||
await bot.send_text(room, 'Command removed.')
|
||||
command_number = int(args[1])
|
||||
if self.commands[command_number] is not None:
|
||||
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)
|
||||
cmdname = args[1]
|
||||
self.commands[cmdname] = args[2]
|
||||
self.commands.append(command)
|
||||
bot.save_settings()
|
||||
await bot.send_text(room, 'Command added.')
|
||||
elif len(args) == 1:
|
||||
if args[0] == 'list':
|
||||
await bot.send_text(room, 'Known commands: ' + str(self.commands))
|
||||
elif args[0] in self.commands:
|
||||
self.logger.debug(f"room: {room.display_name} sender: {event.sender} wants to run cmd {args[0]}")
|
||||
out = self.run_command(self.commands[args[0]], event.sender, room.display_name)
|
||||
await self.send_output(bot, room, out)
|
||||
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:
|
||||
await bot.send_text(room, 'Unknown command.')
|
||||
command_number = int(args[0])
|
||||
target_command = self.commands[command_number]
|
||||
if target_command is not None:
|
||||
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):
|
||||
return 'Runs shell commands'
|
||||
|
|
Loading…
Reference in New Issue