Skip to content

Instantly share code, notes, and snippets.

@LordAro
Created October 30, 2016 02:03
Show Gist options
  • Save LordAro/a45ea73940e0b21ef1effdb03d4350d4 to your computer and use it in GitHub Desktop.
Save LordAro/a45ea73940e0b21ef1effdb03d4350d4 to your computer and use it in GitHub Desktop.
from csbot.plugin import Plugin
from csbot.util import nick
class Exam(Plugin):
PLUGIN_DEPENDS = ['usertrack', 'auth']
examdb = Plugin.use('mongodb', collection='exam')
@Plugin.command('exam', help=('exam: show time until your next (set) exam.'
' See also exam.set, exam.all & exam.list'))
def exam(self, e):
ident = self.identify_user(nick(e['user']))
examcodes = self.whoisdb.find_one(ident)
@Plugin.command('exam.set', help=('exam.set [exam...]: List (space/comma'
' delimited) of exams you\'re taking'))
def examset(self, e):
ident = self.identify_user(nick(e['user']))
self.examdb.remove(ident) # Remove any existing
examlist = e['data']
if ',' in examlist:
exams = examlist.split(',')
exams = exams.strip()
else:
exams = examlist.split()
# TODO: Check exams exist, error/drop on missing exam
# Also check exam is not yet in the past
ident['data'] = exams
self.examdb.insert(ident)
e.reply('Set {} exams'.format(len(exams)))
@Plugin.command('exam.all', help='exam.all: List all your set exams')
def examall(self, e):
ident = self.identify_user(nick(e['user']))
examcodes = self.whoisdb.find_one(ident)
@Plugin.command('exam.list', help=('exam.list: PM\'s a list of all'
' available exams to requester'))
def examlist(self, e):
ident = self.identify_user(nick(e['user']))
e.bot.reply(e['user'], "blah") # Only ever send PM to requester
@Plugin.command('exam.add', help=('exam.add [code] [YYYY-mm-dd] [name]'))
def examadd(self, e):
if not self.bot.plugins['auth'].check_or_error(e, 'exam', e['channel']):
return False
try:
code, date, name = e['data'].split(' ', 2)
except ValueError:
# Not enough values to unpack
e.reply('error: invalid format, expected [code] [YYYY-mm-dd] [name]')
# TODO: Error on trying to add exam in the past
exam = {code: {'date': date, 'name': name}}
self.examdb.insert(exam)
@Plugin.command('exam.clear' help='exam.clear: Clear all existing exam data')
def examclear(self, e):
# TODO: Force flag if exams not run yet?
self.examdb.delete_many({})
def identify_user(self, nick):
"""Identify a user: by account if authed, if not, by nick. Produces a dict
suitable for throwing at mongo."""
user = self.bot.plugins['usertrack'].get_user(nick)
if user['account'] is not None:
return {'account': user['account']}
else:
return {'nick': nick}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment