fulvia/modules/rand.py

75 lines
1.9 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/usr/bin/env python3
"""
Pick some random numbers.
"""
import sys
import random
2019-12-31 19:23:28 -05:00
import string
2018-03-16 03:13:43 -04:00
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."""
2020-01-07 18:58:19 -05:00
arg1 = trigger.args[1] if len(trigger.args) >= 2 else 0
arg2 = trigger.args[2] if len(trigger.args) >= 3 else sys.maxsize
2018-03-16 03:13:43 -04:00
try:
2020-01-07 18:58:19 -05:00
low = int(arg1)
high = int(arg2)
2018-03-16 03:13:43 -04:00
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}")
2019-12-31 19:23:28 -05:00
2020-01-08 09:27:34 -05:00
@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)
2019-12-31 19:23:28 -05:00
@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.
"""
2020-01-07 18:58:19 -05:00
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")
2019-12-31 19:23:28 -05:00
2020-11-30 15:16:28 -05:00
if num_letters > 200:
num_letters = 200
2019-12-31 19:23:28 -05:00
msg = []
for _ in range(num_letters):
c = random.choice(string.ascii_lowercase + "aioue" * num_vowels)
msg.append(c)
bot.msg(''.join(msg))