Skip to content

Commit 0517eb1

Browse files
committed
Switch custom value checks to cloudbot.util.string.pluralize()
1 parent 725fd93 commit 0517eb1

1 file changed

Lines changed: 17 additions & 21 deletions

File tree

plugins/reddit_info.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime
55
from bs4 import BeautifulSoup
66
from cloudbot import hook
7+
from cloudbot.util.formatting import pluralize
78

89
search_pages = defaultdict(list)
910
search_page_indexes = {}
@@ -117,14 +118,12 @@ def karma(text):
117118
out += "email has been verified | "
118119
out += "cake day is {} | ".format(datetime.fromtimestamp(data['data']['created_utc']).strftime('%B %d') )
119120
account_age = datetime.now() - datetime.fromtimestamp(data['data']['created'])
120-
if account_age.days > 365:
121-
age = int(account_age.days / 365)
122-
if age == 1:
123-
out += "redditor for {} year.".format(age)
124-
else:
125-
out += "redditor for {} years.".format(age)
126-
else:
127-
out += "redditor for {} days.".format(account_age.days)
121+
age = account_age.days
122+
age_unit = "day"
123+
if age > 365:
124+
age //= 365
125+
age_unit = "year"
126+
out += "redditor for {}.".format(pluralize(age, age_unit))
128127
return out
129128

130129

@@ -140,14 +139,12 @@ def cake_day(text):
140139
out = "\x02{}'s\x02 ".format(user)
141140
out += "cake day is {}, ".format(datetime.fromtimestamp(data['data']['created_utc']).strftime('%B %d') )
142141
account_age = datetime.now() - datetime.fromtimestamp(data['data']['created'])
143-
if account_age.days > 365:
144-
age = int(account_age.days / 365)
145-
if age == 1:
146-
out += "they have been a redditor for {} year.".format(age)
147-
else:
148-
out += "they have been a redditor for {} years.".format(age)
149-
else:
150-
out += "they have been a redditor for {} days.".format(account_age.days)
142+
age = account_age.days
143+
age_unit = "day"
144+
if age > 365:
145+
age //= 365
146+
age_unit = "year"
147+
out += "they have been a redditor for {}.".format(pluralize(age, age_unit))
151148
return out
152149

153150

@@ -214,11 +211,10 @@ def subinfo(text):
214211
subscribers = data['data']['subscribers']
215212
active = data['data']['accounts_active']
216213
sub_age = datetime.now() - datetime.fromtimestamp(data['data']['created'])
217-
if sub_age.days >= 365:
218-
age = (int(sub_age.days / 365), "y")
219-
else:
220-
age = (sub_age.days, "d")
221-
out = "/r/\x03{}\x02 - {} - a community for {}{}, there are {:,} subscribers and {:,} people online now.".format(name, title, age[0], age[1], subscribers, active)
214+
age, age_unit = time_format(sub_age.days)
215+
out = "/r/\x02{}\x02 - {} - a community for {}{}, there are {:,} subscribers and {:,} people online now.".format(
216+
name, title, age, age_unit, subscribers, active
217+
)
222218
if nsfw:
223219
out += " \x0304NSFW\x0304"
224220
return out

0 commit comments

Comments
 (0)