|
25 | 25 | } |
26 | 26 |
|
27 | 27 |
|
| 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 | + |
28 | 46 | def get_request(ticker, currency): |
29 | 47 | return requests.get(API_URL.format(quote_plus(ticker)), params={'convert': currency}) |
30 | 48 |
|
31 | 49 |
|
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))) |
38 | 53 |
|
| 54 | + func.__doc__ = """- Returns the current {} value""".format(alias.name) |
| 55 | + func.__name__ = alias.name + "_alias" |
39 | 56 |
|
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 |
45 | 58 |
|
46 | 59 |
|
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) |
52 | 64 |
|
53 | 65 |
|
54 | 66 | # main command |
|
0 commit comments