fulvia/modules/rand.py
2018-03-16 03:13:43 -04:00

39 lines
886 B
Python
Executable File

#!/usr/bin/env python3
"""
Pick some random numbers.
"""
import sys
import random
from module import commands, example
@commands("rand")
@example(".rand 2", "random(0, 2) = 1")
@example(".rand -1 -1", "random(-1, -1) = -1")
@example(".rand", "random(0, 56) = 13")
@example(".rand 99 10", "random(10, 99) = 53")
@example(".rand 10 99", "random(10, 99) = 29")
def rand(bot, trigger):
"""Replies with a random number between first and second argument."""
arg1 = trigger.group(3)
arg2 = trigger.group(4)
try:
if arg2 is not None:
low = int(arg1)
high = int(arg2)
elif arg1 is not None:
low = 0
high = int(arg1)
else:
low = 0
high = sys.maxsize
except (ValueError, TypeError):
return bot.reply("Arguments must be of integer type")
if low > high:
low, high = high, low
number = random.randint(low, high)
bot.reply(f"random({low}, {high}) = {number}")