Fix PMing the bot, error handling for quotes
Diff
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(-)
@@ -16,5 +16,5 @@
db = create_engine(config["database"])
Session = sessionmaker(bind=db)
Session = sessionmaker(bind=db, autocommit=True)
session = Session()
@@ -49,6 +49,10 @@
method = (99999, None)
run = []
if channel == self.nickname:
channel = nick
for importer, modname, ispkg in pkgutil.iter_modules(path, prefix):
m = importer.find_module(modname).load_module(modname)
@@ -60,7 +64,13 @@
continue
for rule in val.rule:
regex = r"^{}(?::|,|) (.*)$".format(self.nickname) if rule["named"] else r"^(.*)$"
if channel == nick:
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 @@
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:
deferToThread(m[0], self, m[1], nick, channel)
return
for m in run:
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:
self.msg(source, msg)
else:
self.msg(source, "{}: {}".format(sender, msg))
def main():
@@ -1,4 +1,5 @@
"""Quote system"""
from sqlalchemy.exc import SQLAlchemyError
import dave.module
import dave.config
import uuid
@@ -12,7 +13,6 @@
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 @@
@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 @@
@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.")
@@ -33,8 +33,6 @@
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"]: