-
Notifications
You must be signed in to change notification settings - Fork 16
Quote plugin #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quote plugin #115
Changes from all commits
45b2854
3c40dd3
31a23fc
d613247
2d706b4
5751473
07be5d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import last | ||
import random | ||
from ..plugin import Plugin | ||
|
||
class Quote(Plugin): | ||
"""Quote people out of context for fun and for profit. | ||
Add quotes, recall quotes | ||
""" | ||
db = Plugin.use('mongodb', collection='quote') | ||
|
||
class QuoteError(Exception): | ||
pass | ||
|
||
def _quote(self, nick): | ||
"""Find a quote by person""" | ||
if not nick: | ||
self.QuoteError("No nick!") | ||
|
||
#find quote by nick | ||
quotes = self.db.find({'nick': nick}) | ||
|
||
#Find returns cursor, convert cursor iterator to list, | ||
quote_list = list(quotes) | ||
|
||
if len(quote_list) == 0: | ||
self.QuoteError("No quotes by", nick) | ||
|
||
#Randomly pick quote from returned quotes | ||
quote = random.choice(quote_list) | ||
|
||
return (quote["quote"], quote["nick"]) | ||
|
||
def _quoteExists(self, quote_post): | ||
"""Checks to see if a nick + quote already exists to stop repeats.""" | ||
if self.db.find(quote_post).count > 0: | ||
return True | ||
else: | ||
return False | ||
|
||
def _addquote(self, nick=""): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use a default parameter if you just error on it anyway? Better to have a TypeError if you manage to call without a nick, imo |
||
"""Add quote from what person last said""" | ||
|
||
if not nick: | ||
raise self.QuoteError("No nick!") | ||
|
||
quote = {} #output | ||
quote["quote"] = last.last_message(nick)[2] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be very surprised if this worked at all... Plugins aren't generally good at interacting with each other like that. Have you tested this? |
||
|
||
if not quote["quote"]: | ||
raise self.QuoteError("No Last message from nick, or nick not found.") | ||
|
||
#Create post object | ||
post_quote = {'nick': nick, 'quote': quote["quote"]} | ||
|
||
#check if quote already in database | ||
if not _quoteExists(post_quote): | ||
self.db.insert_one(post_quote) | ||
else: | ||
self.QuoteError("Quote already in Database.") | ||
|
||
return(quote["quote"], nick) | ||
|
||
|
||
@Plugin.command('quote', help="quote [nick]") | ||
def quote(self, e): | ||
"""quote somebody | ||
""" | ||
try: | ||
e.reply("\"{}\" - {}".format(*self._quote(e["data"]))) | ||
except self.QuoteError as ex: | ||
e.reply(str(ex)) | ||
|
||
@Plugin.command('addquote', help="addquote <nick>") | ||
def addquote(self, e): | ||
"""add a quote | ||
""" | ||
try: | ||
e.reply("\"{}\" - {} added as quote.".format(*self._addquote(e["data"]))) | ||
except self.QuoteError as ex: | ||
e.reply(str(ex)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Come on, boolean logic please :)