Skip to content

Commit ad08280

Browse files
committed
Switch cryptocurrency aliases to hook generators
1 parent 53dc414 commit ad08280

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

plugins/cryptocurrency.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,42 @@
2525
}
2626

2727

28+
class Alias:
29+
__slots__ = ("name", "cmds")
30+
31+
def __init__(self, name, *cmds):
32+
self.name = name
33+
if name not in cmds:
34+
cmds = (name,) + cmds
35+
36+
self.cmds = cmds
37+
38+
39+
ALIASES = (
40+
Alias('bitcoin', 'btc'),
41+
Alias('litecoin', 'ltc'),
42+
Alias('dogecoin', 'doge'),
43+
)
44+
45+
2846
def get_request(ticker, currency):
2947
return requests.get(API_URL.format(quote_plus(ticker)), params={'convert': currency})
3048

3149

32-
# aliases
33-
@hook.command("bitcoin", "btc", autohelp=False)
34-
def bitcoin(text):
35-
""" -- Returns current bitcoin value """
36-
# alias
37-
return crypto_command(" ".join(["bitcoin", text]))
50+
def alias_wrapper(alias):
51+
def func(text):
52+
return crypto_command(" ".join((alias.name, text)))
3853

54+
func.__doc__ = """- Returns the current {} value""".format(alias.name)
55+
func.__name__ = alias.name + "_alias"
3956

40-
@hook.command("litecoin", "ltc", autohelp=False)
41-
def litecoin(text):
42-
""" -- Returns current litecoin value """
43-
# alias
44-
return crypto_command(" ".join(["litecoin", text]))
57+
return func
4558

4659

47-
@hook.command("dogecoin", "doge", autohelp=False)
48-
def dogecoin(text):
49-
""" -- Returns current dogecoin value """
50-
# alias
51-
return crypto_command(" ".join(["dogecoin", text]))
60+
def init_aliases():
61+
for alias in ALIASES:
62+
_hook = alias_wrapper(alias)
63+
globals()[_hook.__name__] = hook.command(*alias.cmds, autohelp=False)(_hook)
5264

5365

5466
# main command

0 commit comments

Comments
 (0)