From bd726b8f67c1bc5ed91448adf502039d231b5199 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Tue, 9 Apr 2024 17:36:24 +0000 Subject: [PATCH] Add shorten command to urls plugin --- plugins/urls/plugin.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/plugins/urls/plugin.py b/plugins/urls/plugin.py index c719b34..c861847 100644 --- a/plugins/urls/plugin.py +++ b/plugins/urls/plugin.py @@ -10,7 +10,7 @@ from twisted.internet import defer from twisted.internet.threads import deferToThread -from cardinal.decorators import regex +from cardinal.decorators import command, help, regex # Some notes about this regex - it will attempt to capture URLs prefixed by a # space, a control character (e.g. for formatting), or the beginning of the @@ -162,8 +162,23 @@ def shorten_url(self, url): return response['url'] - def close(self, cardinal): - cardinal.event_manager.remove('urls.detection') + @command("shorten") + @help("Syntax: .shorten ") + def close(self, cardinal, user, channel, msg): + try: + url = msg.split(" ")[1] + except IndexError: + cardinal.sendMsg(channel, "Syntax: .shorten ") + return + + try: + url = self.shorten_url("http://example.com") + except Exception as e: + self.logger.exception("Unable to shorten URL: %s" % url) + cardinal.sendMsg(channel, "Error shortening URL") + return + + cardinal.sendMsg(channel, "Shortened URL: %s" % url) entrypoint = URLsPlugin