diff --git a/bot/mcmaniac.py b/bot/mcmaniac.py index d68a55a..b5cbef5 100644 --- a/bot/mcmaniac.py +++ b/bot/mcmaniac.py @@ -3,7 +3,6 @@ import irc3 from docopt import Dict from irc3.plugins.command import command from irc3.utils import IrcString -from psycopg2 import Error from . import DatabasePlugin from .utils import parse_int @@ -24,23 +23,23 @@ class McManiac(DatabasePlugin): return index, order = index order = 'id {order}'.format(order=order) - offset = 'offset %s' + offset = 'OFFSET %s' else: order = 'random()' offset = '' # Fetch result from database self.cur.execute(''' - select + SELECT item, - rank() over (order by id), - count(*) over (rows between unbounded preceding - and unbounded following) as total - from + rank() OVER (ORDER BY id), + count(*) OVER (ROWS BETWEEN UNBOUNDED PRECEDING + AND UNBOUNDED FOLLOWING) AS total + FROM mcmaniacs - order by + ORDER BY {order} - limit + LIMIT 1 {offset} '''.format(order=order, offset=offset), [index]) @@ -53,15 +52,11 @@ class McManiac(DatabasePlugin): @irc3.event(r'^:(?P\S+) PRIVMSG \S+ :.*(?PMc\S+iaC).*') def save(self, mask: str, item: str): if IrcString(mask).nick != self.bot.nick: - try: - # Insert into database - self.cur.execute(''' - INSERT INTO - mcmaniacs (item) - VALUES - (%s) - ''', [item]) - self.con.commit() - except Error: - # Rollback transaction on error - self.con.rollback() + self.cur.execute(''' + INSERT INTO + mcmaniacs (item) + VALUES + (%s) + ON CONFLICT DO NOTHING + ''', [item]) + self.con.commit()