Skip to content

Instantly share code, notes, and snippets.

@tyrelsouza
Forked from anonymous/plugin.py
Created April 27, 2012 03:59
Show Gist options
  • Save tyrelsouza/2505644 to your computer and use it in GitHub Desktop.
Save tyrelsouza/2505644 to your computer and use it in GitHub Desktop.
Da bikebot code
import bcrypt
import redis
import re, os
import urllib2
import json
import time
import datetime
import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.ircmsgs as ircmsgs
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.schedule as schedule
class Reddit(callbacks.Plugin):
'''Add the help for '@plugin help Reddit' here
This should describe *how* to use this plugin.'''
threaded = True
def __init__(self, irc):
self.__parent = super(Reddit, self)
self.__parent.__init__(irc)
self.redis_server = redis.Redis('localhost')
self.errors = {
'about': '%s is boring as hell because I know nothing about him/her',
'location': '%s must live on the moon',
'photo': '%s is not pretty enough to have a photo',
'bike': '%s must have a derpy bike because I don\'t know what it is',
'reddit': '%s is not a redditor',
'bikephoto': '%s must have non-photogenic bike because I don\'t have a picture'}
self.replies = {
'about': '%s wanted you to know: %s',
'location': '%s\'s location is %s',
'photo': '%s\'s photo is %s',
'bike': '%s has a %s',
'reddit': '%s has been a redditor for %s days, has %s link karma and %s comment karma',
'bikephoto': 'Here is %s\'s sexy ass bike: %s'}
self.functions = {
'about': self._do_others ,
'location': self._do_others ,
'photo': self._do_others ,
'bike': self._do_others,
'reddit': self._do_reddit,
'bikephoto': self._do_others
}
def _default(self, irc, msg, args, text):
cmd = msg.args[1].split()[0].lower()
if cmd[0] in ['.','!','#']:
cmd = cmd[1:]
if text.split()[0] == 'set':
#if not self._check_host(msg):
# irc.reply('Your hostname does not match the one last used. If this is really you, tell an op.')
# return
self._set_data(msg.nick, cmd, msg.host, text[4:])
if msg.args[0] == '#/r/bicycling':
irc.reply('Done, but if you want to change more data please /msg me so you don\'t spam the channel.')
else:
irc.reply('Done')
else:
nick = text.split()[0]
data = str(self._get_data(nick, cmd)).strip()
if not data or data == 'None':
irc.reply(self.errors[cmd] % nick)
else:
irc.reply(self.functions[cmd](nick, cmd))
reddit = wrap(_default, ['text'])
about = wrap(_default, ['text'])
location = wrap(_default, ['text'])
bike = wrap(_default, ['text'])
photo = wrap(_default, ['text'])
bikephoto = wrap(_default, ['text'])
def clear_host(self, irc, msg, args, nick):
'''<nick>
stuff'''
if msg.nick in irc.state.channels[msg.args[0]].ops:
self.redis_server.hset('users:%s' % nick.lower(), 'host', False)
irc.reply('%s\'s hostlock has been lifted.' % nick, prefixNick=False)
else:
irc.reply('You need to be an op to do that.')
clear_host = wrap(clear_host, ['nick'])
def beer(self, irc, msg, args, offender):
'''<nick>
Gets <nick> a beer.
'''
if offender == 'bikebot':
text = 'smashes a beer over %s\'s head' % msg.nick
else:
text = 'gets %s a beer' % (offender)
irc.reply(text, prefixNick=False, action=True, to='#/r/bicycling')
beer = wrap(beer, ['nick'])
def weather(self, irc, msg, args, offender):
'''<place>
gets weather for place
'''
irc.reply('Go look out the window %s' % msg.nick, prefixNick=False, action=True, to='#/r/bicycling')
weather = wrap(weather, ['text'])
def slap(self, irc, msg, args, offender):
'''<nick>
Slaps <nick>.
'''
if offender == 'bikebot':
text = 'punches %s in the face' % msg.nick
else:
text = 'slaps %s' % offender
irc.reply(text, prefixNick=False, action=True, to='#/r/bicycling')
slap = wrap(slap, ['nick'])
def fuckshitupyo(self, irc, msg, args):
if msg.nick in irc.state.channels[msg.args[0]].ops:
for nick in self.redis_server.smembers('users'):
for d in ['about','reddit','bike','location','photo']:
o = self.redis_server.hget('users:%s' % nick.lower(), d)
if o == 'None':
self.redis_server.hdel('users:%s' % nick.lower(), d)
irc.reply('Shit has been fucked up.')
irc.reply('yo')
else:
irc.reply('bitch please')
fuckshitupyo = wrap(fuckshitupyo)
def doJoin(self, irc, msg):
self.redis_server.sadd('online_users', msg.nick)
then = int(self.redis_server.get('last_message'))
now = int(time.time())
d = now - then
minutes = d / 60
if self.Create_user(msg.nick) and minutes > 5:
irc.reply('Hey %s, it looks like nobody is talking now so if you \
have a question, ask it and then stick around for an answer.' % msg.nick)
def doQuit(self, irc, msg):
self.redis_server.srem('online_users', msg.nick)
doKick = doQuit
doPart = doQuit
def doPrivmsg(self, irc, msg):
self.redis_server.set('last_message', int(time.time()))
def Create_user(self, nick):
return self.redis_server.sadd('users', nick.lower())
def _get_data(self, nick, dtype):
if dtype == 'reddit':
return self.redis_server.hget('users:%s' % nick.lower(), dtype) or nick
else:
return self.redis_server.hget('users:%s' % nick.lower(), dtype)
def _set_data(self, nick, dtype, host, data):
self.redis_server.hset('users:%s' % nick.lower(), dtype, data)
self.redis_server.hset('users:%s' % nick.lower(), 'host', host)
def _do_others(self, nick, cmd):
return self.replies[cmd] % (nick, self._get_data(nick, cmd))
def _do_reddit(self, account, cmd):
try:
reddit_data = urllib2.urlopen('http://www.reddit.com/user/%s/about.json' % account).read()
except urllib2.HTTPError:
return 'No reddit account for %s. You can set your reddit account with !reddit set <username>' % account
else:
js = json.loads(reddit_data)
made_utc = js['data']['created_utc']
link_karma = js['data']['link_karma']
comment_karma = js['data']['comment_karma']
now = datetime.date.today()
then = datetime.date.fromtimestamp(made_utc)
age = now - then
return self.replies['reddit'] % (account, age.days, link_karma, comment_karma)
def _check_host(self, msg):
host = self.redis_server.hget('users:%s' % msg.nick.lower(), 'host')
if not host or host == msg.host:
return True
else:
return False
Class = Reddit
# vim:set shiftwidth=4 softtabstop=4 expandtab:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment