pyircbot/pyircbot/modules/LMGTFY.py

40 lines
1.1 KiB
Python
Raw Normal View History

2015-09-05 21:44:46 -07:00
#!/ysr/bin/env python3
"""
.. module::LMGTFY
2015-11-01 18:03:11 -08:00
:synopsis: LMGTFY
2015-09-05 21:44:46 -07:00
.. moduleauthor::Nick Krichevsky <nick@ollien.com>
"""
2015-09-05 22:26:19 -07:00
import urllib.parse
2015-09-05 21:44:46 -07:00
from pyircbot.modulebase import ModuleBase, ModuleHook
BASE_URL = "http://lmgtfy.com/?q="
2017-01-01 14:59:01 -08:00
2015-09-05 21:44:46 -07:00
class LMGTFY(ModuleBase):
2015-11-01 18:03:11 -08:00
def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName)
self.hooks.append(ModuleHook("PRIVMSG", self.handleMessage))
self.bot = bot
2015-09-05 21:44:46 -07:00
2015-11-01 18:03:11 -08:00
def handleMessage(self, args, prefix, trailing):
channel = args[0]
prefix = self.bot.decodePrefix(prefix)
if self.bot.messageHasCommand(".lmgtfy", trailing):
message = trailing.split(" ")[1:]
link = self.createLink(message)
self.bot.act_PRIVMSG(channel, "%s: %s" % (prefix.nick, link))
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def createLink(self, message):
finalUrl = BASE_URL
if type(message) == str:
message = message.split(" ")
2015-09-05 21:44:46 -07:00
2015-11-01 18:03:11 -08:00
for word in message:
finalUrl += urllib.parse.quote(word)
if word != message[-1]:
2017-01-01 14:59:01 -08:00
finalUrl += "+"
2015-09-05 21:44:46 -07:00
2015-11-01 18:03:11 -08:00
return finalUrl