fulvia/modules/rand.py
2020-01-08 09:27:34 -05:00

72 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Pick some random numbers.
"""
import sys
import random
import string
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.args[1] if len(trigger.args) >= 2 else 0
arg2 = trigger.args[2] if len(trigger.args) >= 3 else sys.maxsize
try:
low = int(arg1)
high = int(arg2)
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}")
@commands("choice", "choose")
@example(".choose opt1|opt2|opt3")
def choose(bot, trigger):
"""
.choice option1|option2|option3 - Makes a difficult choice easy.
"""
if len(trigger.args) < 2:
return bot.reply("Choose what?")
choices = trigger.args[1].split('|')
pick = random.choice(choices)
msg = f"Your options: {'|'.join(choices)}. My choice: {pick}"
bot.reply(msg)
@commands('letters')
@example('.letters', 'iloynlle')
@example('.letters 16', 'oaotordbwaauouxk')
def rand_letters(bot, trigger):
"""
usage: .letters [num_letters] [num_vowels]
Generates a series of string of random letters.
"""
arg1 = trigger.args[1] if len(trigger.args) >= 2 else 8
arg2 = trigger.args[2] if len(trigger.args) >= 3 else 2
try:
num_letters = int(arg1)
num_vowels = int(arg2)
except (ValueError, TypeError):
return bot.reply("Arguments must be of integer type")
msg = []
for _ in range(num_letters):
c = random.choice(string.ascii_lowercase + "aioue" * num_vowels)
msg.append(c)
bot.msg(''.join(msg))