Skip to content

Commit

Permalink
Update iou.py
Browse files Browse the repository at this point in the history
changed some stuff at LordAro's request, attempted to make more codestyle compliant
  • Loading branch information
FromAnkyra authored Jun 12, 2019
1 parent 0d6d2bb commit 41c9b9e
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions src/csbot/plugins/iou.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ class IOU(Plugin):
db_owage = Plugin.use('mongodb', collection='owage')

def setup(self):
'''
In case for some reason, the db isn't initialised, makes sure that it is
(and because a mongodb doesn't exist if it doesn't have a document, makes sure there is one.)
Should really only be called once, ever.
'''
super(IOU, self).setup()

#check if owage already exists
if self.db_owage.find_one("example"):
#Check if owage already exists.
if self.db_owage.find_one('example'):
self.initialised = True
return

#if owage doesn't exist yet, we want to make it
#If owage doesn't exist yet, we want to make it.
self.initialised = False

self.blankuser = {"_id": "example", "owee_nick": 0}

self.blankuser = {'_id': 'example', 'owee_nick': 0}
x = self.db_owage.insert(self.blankuser)

def get_owage(self, nick, owee_name):
all_owed = self.db_owage.find_one(nick)
if all_owed:
if owee_name in all_owed:
if all_owed and owee_name in all_owed:
return all_owed[owee_name]/100
return 0
return 0 #this is what I keep recieving
return 0

def update_owage(self, nick, owee_name, amount):
all_owed = self.db_owage.find_one(nick)
Expand All @@ -42,28 +44,19 @@ def update_owage(self, nick, owee_name, amount):
else:
all_owed = {"_id": nick, owee_name: int(amount*100)}

self.db_owage.insert_one(all_owed) #inserts updated value into db
return self.get_owage(nick, owee_name) #returns the updated amount

def get_all_nicks(self):
cursor = self.db_owage.find()
nicks = []
for document in cursor:
nicks += cursor["_id"]
return nicks

self.db_owage.insert_one(all_owed)
return self.get_owage(nick, owee_name)


@Plugin.command('IOU', help = ('IOU <nick>: returns what you owe to that person '
'| IOU <nick> <amount> adds the amount (in pounds) to what you alread owe that person'))
@Plugin.command('iou', help = ('equivalent to IOU'))
@Plugin.command('iou', help=('iou <nick>: returns what you owe to that person '
'| iou <nick> <amount> adds the amount (in pounds) to what you alread owe that person'))
def iou(self, e):
nick_ = nick(e['user']).strip('<')
nick_ = nick_.strip('>')
request = e['data']
request_words = request.split(" ")
request_words = request.split()
res = None
if request.strip(" ") == "":
if request.strip() == '':
e.reply('Could not parse - please refer to help iou')
return

Expand All @@ -72,8 +65,9 @@ def iou(self, e):
float(request_words[1])
except ValueError:
e.reply('Please use an actual number')
if not (-2**48 < int(request_words[1]) < 2**48):
e.reply('stop with the fuckoff big integers')
if not (-2**48 < int(request_words[1]) < 2**48):
#MongoDB has an 64-bit limit, enforcing a somewhat smaller limit just in case.
e.reply('please use a smaller integer.')
return
res = self.update_owage(nick_, request_words[0], float(request_words[1]))
if len(request_words) == 1:
Expand All @@ -87,23 +81,20 @@ def iou(self, e):
e.reply('you owe {} £{:.02f}.'.format(request_words[0], res))
return


#forgetting this for now because I'm just hitting a brick wall
@Plugin.command('IAmOwed', help = ('Iamowed <nick> tells you what nick owes you'))
@Plugin.command('iamowed', help = ('Iamowed <nick> tells you what nick owes you'))
#hey froman transitivity when

@Plugin.command('iamowed', help=('iamowed <nick> tells you what nick owes you'))
def iamowed(self, e):
nick_ = nick(e['user']).strip('<')
nick_ = nick_.strip('>')
request = e['data']
if request.strip(" ") == "":
e.reply('Could not parse - please refer to help IAmOwed')
if request.strip() == '':
e.reply('Could not parse - please refer to help iamowed')
return
request_words = request.split(" ")
request_words = request.split()
res = None
if len(request_words) == 1:
res = self.get_owage(str((request_words[0])), nick_)
e.reply('{} owes you £{}'.format(request, res))
return
e.reply('could not parse - please refer to help IAmOwed')
e.reply('could not parse - please refer to help iamowed')
return

0 comments on commit 41c9b9e

Please sign in to comment.