Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- seen.py - Phenny seen module
- """
- import sqlite3 as lite
- import os
- def db_connect(db):
- return lite.connect(db, check_same_thread = False)
- def setup(self):
- self.seen_db = os.path.join(os.path.expanduser('~/.phenny'), 'seen.db')
- self.seen_conn = db_connect(self.seen_db)
- c = self.seen_conn.cursor()
- c.execute('''create table if not exists seen(
- nick varchar(31) NOT NULL PRIMARY KEY,
- channel varchar(31) NOT NULL,
- message text,
- event varchar(10) NOT NULL,
- time time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
- );''')
- c.close()
- self.seen_conn.commit()
- self.seen_conn.close()
- setup.thread = False
- def smart_truncate(content):
- suffix='...'
- length=int(150)
- if len(content) <= length:
- return content
- else:
- return content[:length].rsplit(' ', 1)[0]+suffix
- def seen(phenny, input):
- """Gives when a user was last seen."""
- inputnick = input.group(2)
- add.conn = db_connect(phenny.seen_db)
- c = add.conn.cursor()
- query = (inputnick,)
- c.execute("SELECT * FROM seen WHERE nick LIKE ?;", query)
- results = c.fetchall()
- try:
- nick = results[0]
- channel = results[1]
- message = results[2]
- event = results[3]
- seentime = results[4]
- except:
- nick = None
- if not nick:
- phenny.say("Sorry I haven't seen " + inputnick)
- else:
- message = smart_truncate(message)
- if event == "PRIVMSG":
- phenny.say(nick + " was last seen in " + channel + 'saying "' + message + '" at ' + seentime)
- elif event == "JOIN":
- phenny.say(nick + " was last seen joining " + channel + " at " + seentime)
- elif event == "PART":
- phenny.say(nick + " was last seen leaving " + channel + ' with message "' + message + '" at ' + seentime)
- elif event == "QUIT":
- phenny.say(nick + ' was last seen quitting with message "' + message + '" at ' + seentime)
- c.close()
- seen.commands = ['seen']
- seen.example = ".seen somenick"
- def seenstore(phenny, input, event):
- nick = input.nick
- channel = input.sender
- if event == "JOIN":
- message = None
- else:
- message = input.group()
- add.conn = db_connect(phenny.seen_db)
- c = add.conn.cursor()
- entry = (nick, channel, message, event)
- c.execute("INSERT INTO seen VALUES(?, ?, ?, ?)", entry)
- add.conn.commit()
- c.close()
- add.conn.close()
- def seenmsg(phenny, input):
- event = "PRIVMSG"
- seenstore(phenny, input, event)
- seenmsg.rule = r'.*'
- def seenjoin(phenny, input):
- event = "JOIN"
- seenstore(phenny, input, event)
- seenjoin.event = 'JOIN'
- seenjoin.rule = r'.*'
- def seenquit(phenny, input):
- event = "QUIT"
- seenstore(phenny, input, event)
- seenquit.event = 'QUIT'
- seenquit.rule = r'.*'
- def seenpart(phenny, input):
- event = "PART"
- seenstore(phenny, input, event)
- seenquit.event = 'PART'
- seenquit.rule = r'.*'
Advertisement
Add Comment
Please, Sign In to add comment