From bf0e271546ed9b670a9da253c9bb54dc756280be Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 29 Mar 2016 14:32:01 +0100 Subject: [PATCH] added random.py, added code to commands.py to remove all empty strings from args_split. --- modules/commands.py | 3 ++- modules/random.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 modules/random.py diff --git a/modules/commands.py b/modules/commands.py index d31c1e3d..3815e5e8 100644 --- a/modules/commands.py +++ b/modules/commands.py @@ -83,7 +83,8 @@ class Module(object): event["stderr"].write(returned).send() return min_args = hook.kwargs.get("min_args") - args_split = event["message_split"][1:] + # get rid of all the empty strings + args_split = list(filter(None, event["message_split"][1:])) if min_args and len(args_split) < min_args: ChannelStdErr("Error", event["channel"] ).write("Not enough arguments (" diff --git a/modules/random.py b/modules/random.py new file mode 100644 index 00000000..49682d6d --- /dev/null +++ b/modules/random.py @@ -0,0 +1,25 @@ +import random + +class Module(object): + def __init__(self, bot): + bot.events.on("received").on("command").on("random", + "rand").hook(self.random) + + def random(self, event): + start, end = "1", "100" + if len(event["args_split"]) > 1: + start, end = event["args_split"][:2] + elif len(event["args_split"]) == 1: + end = event["args_split"][0] + if start.isdigit() and end.isdigit(): + start, end = int(start), int(end) + if end > start: + number = random.randint(start, end) + event["stdout"].write("(%d-%d) %d" % (start, end, + number)) + else: + event["stderr"].write( + "End must be greater than start") + else: + event["stderr"].write( + "Both start and end must be valid integers")