From 41c9b9e8a0e8c7512d56a156c21cbeb7ca333be0 Mon Sep 17 00:00:00 2001 From: uint8_lotte <37625186+FromAnkyra@users.noreply.github.com> Date: Wed, 12 Jun 2019 15:24:52 +0100 Subject: [PATCH] Update iou.py changed some stuff at LordAro's request, attempted to make more codestyle compliant --- src/csbot/plugins/iou.py | 61 +++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/src/csbot/plugins/iou.py b/src/csbot/plugins/iou.py index cc041308..949593ed 100644 --- a/src/csbot/plugins/iou.py +++ b/src/csbot/plugins/iou.py @@ -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) @@ -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 : returns what you owe to that person ' - '| IOU adds the amount (in pounds) to what you alread owe that person')) - @Plugin.command('iou', help = ('equivalent to IOU')) + @Plugin.command('iou', help=('iou : returns what you owe to that person ' + '| iou 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 @@ -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: @@ -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 tells you what nick owes you')) - @Plugin.command('iamowed', help = ('Iamowed tells you what nick owes you')) - #hey froman transitivity when + + @Plugin.command('iamowed', help=('iamowed 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