From 4f018e773e38418eeaa5e59a9bd4a84ef6f22e05 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 29 Jul 2017 13:47:35 +0100 Subject: [PATCH] Fix PMing the bot, error handling for quotes --- dave/config.py | 2 +- dave/dave.py | 28 +++++++++++++++++++++++----- dave/modules/quote.py | 23 ++++++++++++++++------- dave/modules/weather.py | 2 -- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/dave/config.py b/dave/config.py index 552863a..0c81cae 100644 --- a/dave/config.py +++ b/dave/config.py @@ -16,5 +16,5 @@ redis = redis.StrictRedis(host=config["redis"]["host"], port=config["redis"]["po # connect to the database db = create_engine(config["database"]) -Session = sessionmaker(bind=db) +Session = sessionmaker(bind=db, autocommit=True) session = Session() \ No newline at end of file diff --git a/dave/dave.py b/dave/dave.py index 5c1f682..4b4017c 100644 --- a/dave/dave.py +++ b/dave/dave.py @@ -49,6 +49,10 @@ class Dave(irc.IRCClient): method = (99999, None) # priority, method to run run = [] # methods which match the message which should be run regardless of priority + if channel == self.nickname: + # message was sent directly to the bot to respond directly back to the user + channel = nick + for importer, modname, ispkg in pkgutil.iter_modules(path, prefix): m = importer.find_module(modname).load_module(modname) @@ -60,7 +64,13 @@ class Dave(irc.IRCClient): continue for rule in val.rule: - regex = r"^{}(?::|,|) (.*)$".format(self.nickname) if rule["named"] else r"^(.*)$" + if channel == nick: + # message was sent directly to the bot so make name optional + regex = r"^(?:{}(?::|,|) )?(.*)$".format(self.nickname) \ + if rule["named"] else r"^(.*)$" + else: + regex = r"^{}(?::|,|) (.*)$".format(self.nickname) \ + if rule["named"] else r"^(.*)$" match = re.match(regex, msg) @@ -78,16 +88,24 @@ class Dave(irc.IRCClient): deferToThread(method[1], self, method[2], nick, channel) if not (hasattr(method[1], "dont_always_run") and method[1].dont_always_run): - for m in run: - # modules that should always be run regardless of priority - deferToThread(m[0], self, m[1], nick, channel) + # if dont_always_run is set, the command the user sent doesn't + # want "always run" modules to run. + return + + for m in run: + # modules that should always be run regardless of priority + deferToThread(m[0], self, m[1], nick, channel) def irc_unknown(self, prefix, command, params): if command == "INVITE": self.join(params[1]) def reply(self, source, sender, msg): - self.msg(source, "{}: {}".format(sender, msg)) + if source == sender: + # responding directly back to the user so don't tag them + self.msg(source, msg) + else: + self.msg(source, "{}: {}".format(sender, msg)) def main(): diff --git a/dave/modules/quote.py b/dave/modules/quote.py index 2f9b712..74c64d9 100644 --- a/dave/modules/quote.py +++ b/dave/modules/quote.py @@ -1,4 +1,5 @@ """Quote system""" +from sqlalchemy.exc import SQLAlchemyError import dave.module import dave.config import uuid @@ -12,7 +13,6 @@ def add_quote(bot, args, sender, source): generated_uuid = str(uuid.uuid4()) quote = Quote(id=generated_uuid, quote=args[0], attributed=args[1], added_by=sender) dave.config.session.add(quote) - dave.config.session.commit() bot.reply(source, sender, assembleFormattedText( A.normal["Successfully added quote to database: ", A.bold[args[0]], " by ", @@ -41,10 +41,14 @@ def quote(bot, args, sender, source): @dave.module.help("Syntax: fq [search]. Search for a quote in the quote database.") @dave.module.command(["fq", "findquote"], "(.*)$") def find_quote(bot, args, sender, source): - quotes = dave.config.session.query(Quote).filter( - (Quote.quote.op("~")(args[0])) | (Quote.attributed.op("~")(args[0])) - | (Quote.added_by.op("~")(args[0])) - ).all() + try: + quotes = dave.config.session.query(Quote).filter( + (Quote.quote.op("~")(args[0])) | (Quote.attributed.op("~")(args[0])) + | (Quote.added_by.op("~")(args[0])) + ).all() + except SQLAlchemyError as e: + bot.reply(source, sender, SQLAlchemyError.__str__(e)) + return if len(quotes) == 0: bot.reply(source, sender, "No results for query.") @@ -62,6 +66,11 @@ def find_quote(bot, args, sender, source): @dave.module.help("Syntax: dq [uuid]. Allow the quote owner to delete a quote.") @dave.module.command(["dq", "deletequote"], "(.*)$") def delete_quote(bot, args, sender, source): - dave.config.session.query(Quote).filter(Quote.id == args[0]).delete() - dave.config.session.commit() + query = dave.config.session.query(Quote).filter(Quote.id == args[0]) + + if not query.count(): + bot.reply(source, sender, "Couldn't find a quote with that UUID.") + return + + query.delete() bot.reply(source, sender, "Successfully deleted quote.") \ No newline at end of file diff --git a/dave/modules/weather.py b/dave/modules/weather.py index 7b0bb2e..884eae9 100644 --- a/dave/modules/weather.py +++ b/dave/modules/weather.py @@ -33,8 +33,6 @@ def weather(bot, args, sender, source): db_location = Location(nick=sender, location=location) dave.config.session.add(db_location) - dave.config.session.commit() - geocode = get_location(location) if not geocode or not geocode["results"]: -- libgit2 1.7.2