Skip to content

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

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions csbot/plugins/quote.py
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
Copy link
Member

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 :)

else:
return False

def _addquote(self, nick=""):
Copy link
Member

Choose a reason for hiding this comment

The 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]
Copy link
Member

@LordAro LordAro Oct 2, 2016

Choose a reason for hiding this comment

The 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))