Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/random/__init__.py

76 lines
2.4 KiB
Python
Raw Normal View History

2023-01-17 03:38:37 +00:00
import secrets
2023-05-17 10:26:28 +00:00
import string
import uuid
2023-01-17 03:38:37 +00:00
2023-02-05 14:33:33 +00:00
from core.builtins import Bot
2023-03-04 08:51:56 +00:00
from core.component import module
2023-01-17 03:38:37 +00:00
2023-06-06 03:18:43 +00:00
r = module('random', alias=['rand', 'rng'],
2023-05-17 10:57:00 +00:00
developers=['Dianliang233', 'DoroWolf'], desc='{random.help.desc}', )
2023-01-17 03:38:37 +00:00
2023-03-15 11:29:25 +00:00
@r.handle('number <min> <max> {{random.help.number}}', )
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2023-05-17 11:25:14 +00:00
try:
_min = int(msg.parsed_msg['<min>'])
_max = int(msg.parsed_msg['<max>'])
2023-05-17 11:47:24 +00:00
except ValueError:
2023-05-27 10:19:24 +00:00
return await msg.finish(msg.locale.t('error.range.notnumber'))
2023-05-17 11:30:28 +00:00
if _min > _max:
2023-05-27 10:19:24 +00:00
return await msg.finish(msg.locale.t('error.range.invalid'))
2023-05-17 11:47:24 +00:00
2023-05-17 11:25:14 +00:00
random = secrets.randbelow(_max - _min + 1) + _min
2023-05-17 11:26:50 +00:00
await msg.finish('' + str(random))
2023-01-17 03:38:37 +00:00
2023-03-15 11:29:25 +00:00
@r.handle('choice ... {{random.help.choice}}', )
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2023-01-17 03:38:37 +00:00
choices = msg.parsed_msg['...']
await msg.finish(secrets.choice(choices))
2023-03-10 13:59:18 +00:00
2023-05-01 04:18:03 +00:00
@r.handle('shuffle ... {{random.help.shuffle}}', )
async def _(msg: Bot.MessageSession):
cards: list = msg.parsed_msg['...']
x = cards.copy()
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = secrets.randbelow(i + 1)
x[i], x[j] = x[j], x[i]
await msg.finish(', '.join(x))
@r.handle('string <count> [-u] [-l] [-n] [-s] {{random.help.string}}',
options_desc={'-u': '{random.help.option.string.u}',
'-l': '{random.help.option.string.l}',
'-n': '{random.help.option.string.n}',
'-s': '{random.help.option.string.s}'})
2023-05-17 10:26:28 +00:00
async def _(msg: Bot.MessageSession):
2023-05-17 10:57:00 +00:00
try:
length = int(msg.parsed_msg['<count>'])
if length < 1 or length > 100:
raise ValueError
except ValueError:
2023-05-17 11:30:28 +00:00
return await msg.finish(msg.locale.t('random.message.string.error.invalid'))
2023-05-17 10:26:28 +00:00
characters = ""
if msg.parsed_msg.get('-u', False):
characters += string.ascii_uppercase
2023-05-17 10:26:28 +00:00
if msg.parsed_msg.get('-l', False):
characters += string.ascii_lowercase
2023-05-17 10:26:28 +00:00
if msg.parsed_msg.get('-n', False):
characters += string.digits
2023-05-17 10:26:28 +00:00
if msg.parsed_msg.get('-s', False):
characters += "!@#$%^&*-_+=?"
2023-05-17 10:26:28 +00:00
if not characters:
characters = string.ascii_letters + string.digits
2023-05-17 11:25:14 +00:00
random = ''.join(secrets.choice(characters) for _ in range(length))
await msg.finish(random)
2023-05-17 10:26:28 +00:00
2023-03-15 11:29:25 +00:00
@r.handle('uuid {{random.help.uuid}}', )
2023-03-10 13:59:18 +00:00
async def _(msg: Bot.MessageSession):
2023-03-10 14:03:03 +00:00
await msg.finish(str(uuid.uuid4()))