diff --git a/commands/dety b/commands/dety deleted file mode 100755 index a6eccdb..0000000 --- a/commands/dety +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env python3 - -# http://inamidst.com/saxo/ -# Created by Sean B. Palmer - -import urllib.parse -import saxo - -@saxo.pipe -def dety(arg): - if not arg: - return "Get etymology of a German word" - arg = urllib.parse.quote(arg) - page = saxo.request("http://services.nonceword.org/p/dety/{}".format(arg)) - return page["text"] diff --git a/commands/lpd b/commands/lpd deleted file mode 100755 index 2c414b1..0000000 --- a/commands/lpd +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env python3 - -import re -import urllib.parse -import saxo - -@saxo.command() -def lpd(arg): - arg = urllib.parse.quote(arg) - page = saxo.request("http://services.nonceword.org/p/lpd/" + arg) - text = page["text"] - return text diff --git a/commands/r2r b/commands/r2r deleted file mode 100755 index ffedea3..0000000 --- a/commands/r2r +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -# http://inamidst.com/saxo/ -# Created by Sean B. Palmer - -import urllib.parse -import saxo - -@saxo.pipe -def r2r(arg): - arg = urllib.parse.quote(arg) - page = saxo.request("http://services.nonceword.org/p/rome2rio/" + arg) - return page["text"] diff --git a/commands/roll b/commands/roll old mode 100644 new mode 100755 index db8b0e9..1458997 --- a/commands/roll +++ b/commands/roll @@ -1,15 +1,28 @@ #!/usr/bin/env python3 -# http://inamidst.com/saxo/ -# Created by Sean B. Palmer +import random +import re -import urllib.parse import saxo + @saxo.pipe -def roll(arg): - if not arg: - return "Let me roll it" - arg = urllib.parse.quote(arg) - page = saxo.request("http://services.nonceword.org/p/roll/{}".format(arg)) - return page["text"] +def roll(dice): + m = re.match(r"(\d+)d(\d+)", dice) + if not m: return 'e.g. .roll 1d12' + number, sides = map(int, m.groups()) + if number > 20: return 'Sorry, you can only roll up to 20 dice at a time.' + if sides > 100: return 'Sorry, I only have dice with sides up to 100.' + def do_roll(number, sides): + results = tuple(random.randint(1, sides) for die in range(number)) + if number == 1: + if sides == 2: + return ['heads', 'tails'][results[0] - 1] + else: + return str(results[0]) + else: + if sides == 2: + return ', '.join(['heads', 'tails'][result - 1] for result in results) + else: + return '%s = %s' % (' + '.join(str(result) for result in results), sum(results)) + return do_roll(number, sides) diff --git a/commands/rot13 b/commands/rot13 index 02b157f..d386fa4 100755 --- a/commands/rot13 +++ b/commands/rot13 @@ -1,13 +1,19 @@ #!/usr/bin/env python3 -# http://inamidst.com/saxo/ -# Created by Sean B. Palmer +import unicodedata as u -import urllib.parse import saxo + +def cipher_char(char): + ascii_value = ord(char) + if 97 <= ascii_value < 123: + return chr(97 + (((ascii_value - 97) + 13) % 26)) + elif 65 <= ascii_value < 91: + return chr(65 + (((ascii_value - 65) + 13) % 26)) + else: + return char + @saxo.pipe -def rot13(arg): - arg = urllib.parse.quote(arg) - page = saxo.request("http://services.nonceword.org/p/rot13/" + arg) - return page["text"] +def _rot13(text): + return ''.join(cipher_char(char) for char in u.normalize('NFD', text)) diff --git a/commands/thesaurus b/commands/thesaurus deleted file mode 100755 index 829a666..0000000 --- a/commands/thesaurus +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -# http://inamidst.com/saxo/ -# Created by Sean B. Palmer - -import urllib.parse -import saxo - -@saxo.pipe -def thesaurus(arg): - arg = urllib.parse.quote(arg) - page = saxo.request("http://services.nonceword.org/r/thesaurus/" + arg) - return page["text"] diff --git a/commands/weather b/commands/weather deleted file mode 100755 index f3d26d6..0000000 --- a/commands/weather +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -# http://inamidst.com/saxo/ -# Created by Sean B. Palmer - -import urllib.parse -import saxo - -@saxo.pipe -def weather(arg): - arg = urllib.parse.quote(arg) - page = saxo.request("http://services.nonceword.org/r/weather/" + arg) - return page["text"]