Atheuz

Untitled

Aug 14th, 2011 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. # Karma plugin for Skybot
  2.  
  3. import time
  4. import re
  5.  
  6. from util import hook, timesince
  7.  
  8.  
  9. def up(db, nick_vote):
  10.     db.execute("""UPDATE karma SET
  11.               up_karma = up_karma+1,
  12.               total_karma = total_karma+1 WHERE nick_vote=?""", (nick_vote.lower(),))
  13.     db.commit()
  14.  
  15.  
  16. def down(db, nick_vote):
  17.     db.execute("""UPDATE karma SET
  18.               down_karma = down_karma+1,
  19.               total_karma = total_karma+1 WHERE nick_vote=?""", (nick_vote.lower(),))
  20.     db.commit()
  21.  
  22.  
  23. def allowed(db, nick, nick_vote):
  24.     time_restriction = 3600
  25.     db.execute("""DELETE FROM karma_voters WHERE ? - epoch >= 3600""",
  26.             (time.time(),))
  27.     db.commit()
  28.     check = db.execute("""SELECT epoch FROM karma_voters WHERE voter=? AND votee=?""",
  29.             (nick.lower(), nick_vote.lower())).fetchone()
  30.  
  31.     if check:
  32.         check = check[0]
  33.         if time.time() - check >= time_restriction:
  34.             db.execute("""INSERT OR REPLACE INTO karma_voters(
  35.                       voter,
  36.                       votee,
  37.                       epoch) values(?,?,?)""", (nick.lower(), nick_vote.lower(), time.time()))
  38.             db.commit()
  39.             return True#, 0
  40.         else:
  41.             return False#, timesince.timeuntil(check, now=time.time()-time_restriction)
  42.     else:
  43.         db.execute("""INSERT OR REPLACE INTO karma_voters(
  44.                   voter,
  45.                   votee,
  46.                   epoch) values(?,?,?)""", (nick.lower(), nick_vote.lower(), time.time()))
  47.         db.commit()
  48.         return True#, 0
  49.  
  50.  
  51. # TODO Make this work on multiple matches in a string, right now it'll only
  52. # work on one match. Scaevolus might have to change the hook function to work
  53. # with findall, as search seems limited.
  54. # karma_re = ('((\S+)(\+\+|\-\-))+', re.I)
  55. karma_re = ('(.+)(\+\+|\-\-)$', re.I)
  56.  
  57. @hook.regex(*karma_re)
  58. def karma_add(match, nick='', chan='', db=None):
  59.     nick_vote = match.group(1).strip()
  60.     if nick.lower() == nick_vote.lower():
  61.         return
  62.     vote_allowed = allowed(db, nick, nick_vote)
  63.     if vote_allowed:
  64.         if match.group(2) == '++':
  65.             db.execute("""INSERT or IGNORE INTO karma(
  66.                       nick_vote,
  67.                       up_karma,
  68.                       down_karma,
  69.                       total_karma) values(?,?,?,?)""", (nick_vote.lower(),0,0,0))
  70.             up(db, nick_vote)
  71.         if match.group(2) == '--':
  72.             db.execute("""INSERT or IGNORE INTO karma(
  73.                       nick_vote,
  74.                       up_karma,
  75.                       down_karma,
  76.                       total_karma) values(?,?,?,?)""", (nick_vote.lower(),0,0,0))
  77.             down(db, nick_vote)
  78.         else:
  79.             return
  80.     else:
  81.         return
  82.  
  83.     return
  84.  
  85.  
  86. @hook.command('k')
  87. @hook.command
  88. def karma(inp, nick='', chan='', db=None):
  89.     """.k/.karma <nick> -- returns karma stats for <nick>"""
  90.  
  91.     db.execute("""CREATE TABLE if not exists karma(
  92.               nick_vote TEXT PRIMARY KEY,
  93.               up_karma INTEGER,
  94.               down_karma INTEGER,
  95.               total_karma INTEGER)""")
  96.  
  97.     db.execute("""CREATE TABLE if not exists karma_voters(
  98.               voter TEXT,
  99.               votee TEXT,
  100.               epoch FLOAT,
  101.               PRIMARY KEY(voter, votee))""")
  102.  
  103.     if not chan.startswith('#'):
  104.         return
  105.  
  106.     nick_vote = inp
  107.     out = db.execute("""SELECT * FROM karma WHERE nick_vote=?""",
  108.             (nick_vote.lower(),)).fetchall()
  109.  
  110.     if not out:
  111.         return "no karma"
  112.     else:
  113.         out = out[0]
  114.         return "'%s' has %s karma" % (nick_vote, out[1]-out[2])
  115.  
  116.     return
Add Comment
Please, Sign In to add comment