take an option server alias arg for !reconnect

closes #166
This commit is contained in:
jesopo 2019-09-27 12:35:28 +01:00
parent efeffb62a9
commit 54d874ddee

View file

@ -42,15 +42,32 @@ class Module(ModuleManager.BaseModule):
event["stderr"].write("No channel provided") event["stderr"].write("No channel provided")
event["server"].send_part(target) event["server"].send_part(target)
def _from_alias(self, alias):
id = self.bot.database.servers.get_by_alias(alias)
if id == None:
raise utils.EventError("Unknown server alias")
return self.bot.get_server_by_id(id)
@utils.hook("received.command.reconnect") @utils.hook("received.command.reconnect")
def reconnect(self, event): def reconnect(self, event):
""" """
:help: Reconnect to the current network :help: Reconnect to the current network
:permission: reconnect :permission: reconnect
""" """
line = event["server"].send_quit("Reconnecting") server = event["server"]
alias = str(event["server"])
if event["args"]:
alias = event["args_split"][0]
server = self._from_alias(alias)
if server:
line = server.send_quit("Reconnecting")
line.events.on("send").hook(lambda e: self.bot.reconnect( line.events.on("send").hook(lambda e: self.bot.reconnect(
event["server"].id, event["server"].connection_params)) server.id, server.connection_params))
if not server == event["server"]:
event["stdout"].write("Reconnecting to %s" % alias)
else:
event["stdout"].write("Not connected to %s" % alias)
@utils.hook("received.command.connect", min_args=1) @utils.hook("received.command.connect", min_args=1)
def connect(self, event): def connect(self, event):
@ -59,13 +76,8 @@ class Module(ModuleManager.BaseModule):
:usage: <server id> :usage: <server id>
:permission: connect :permission: connect
""" """
alias = event["args"] server = self._from_alias(event["args"])
id = self.bot.database.servers.get_by_alias(alias) if server:
if id == None:
raise utils.EventError("Unknown server alias")
existing_server = self.bot.get_server_by_id(id)
if existing_server:
raise utils.EventError("Already connected to %s" % str( raise utils.EventError("Already connected to %s" % str(
existing_server)) existing_server))
@ -79,16 +91,12 @@ class Module(ModuleManager.BaseModule):
:usage: [server id] :usage: [server id]
:permission: disconnect :permission: disconnect
""" """
id = event["server"].id server = event["server"]
alias = event["server"].alias alias = str(event["server"])
if event["args"]: if event["args"]:
alias = event["args_split"][0] alias = event["args_split"][0]
id = self.bot.database.servers.get_by_alias(alias) server = self._from_alias(alias)
if id == None:
raise utils.EventError("Unknown server alias")
server = self.bot.get_server_by_id(id)
alias = None
if not server == None: if not server == None:
alias = str(server) alias = str(server)
server.disconnect() server.disconnect()
@ -99,7 +107,7 @@ class Module(ModuleManager.BaseModule):
else: else:
raise utils.EventError("Server not connected") raise utils.EventError("Server not connected")
event["stdout"].write("Disconnected from %s" % str(server)) event["stdout"].write("Disconnected from %s" % alias)
@utils.hook("received.command.shutdown") @utils.hook("received.command.shutdown")
def shutdown(self, event): def shutdown(self, event):