feat: random vegetable without replacement

This commit is contained in:
Rongrong 2022-10-21 23:16:57 +08:00
parent f11132c2ae
commit 47ee7a5933
No known key found for this signature in database
GPG Key ID: 1C2D45D45AB7FE94

View File

@ -13,6 +13,7 @@ from threading import Thread
from time import sleep from time import sleep
from itertools import product as _product from itertools import product as _product
from random import Random, SystemRandom from random import Random, SystemRandom
from collections import deque, Counter
Filters = filters.Filters Filters = filters.Filters
@ -40,8 +41,11 @@ product = lambda a, b: tuple(map(''.join, _product(a, b)))
PUNCTUATION_TAIL = '.,?!;:~(' \ PUNCTUATION_TAIL = '.,?!;:~(' \
'。,?!;:~(' '。,?!;:~('
VEGETABLE = {
'permission_denied':
class Vegetable:
_counter = Counter()
permission_denied = deque(
product( product(
{'我太菜了', '我好菜', '我好菜啊', '我菜死了'}, {'我太菜了', '我好菜', '我好菜啊', '我菜死了'},
{'pin 不了这条消息', '学不会怎么 pin 这条消息', '连 pin 都不被允许'} {'pin 不了这条消息', '学不会怎么 pin 这条消息', '连 pin 都不被允许'}
@ -55,13 +59,23 @@ VEGETABLE = {
product( product(
{'这可要我怎么 pin 呀', '怎么才能 pin 这条消息呀', 'pin 不动呀,这可怎么办'}, {'这可要我怎么 pin 呀', '怎么才能 pin 这条消息呀', 'pin 不动呀,这可怎么办'},
{'拿大头针钉上吗', '找把锤子敲到柱子上吗', '触及知识盲区了都'} {'拿大头针钉上吗', '找把锤子敲到柱子上吗', '触及知识盲区了都'}
), )
'reject': )
reject = deque(
product( product(
{'我累了', '我好懒,又懒又菜', '我的 bot 生只要像这样躺着混日子就已经很幸福了'}, {'我累了', '我好懒,又懒又菜', '我的 bot 生只要像这样躺着混日子就已经很幸福了'},
{'根本不想 pin 这条消息', '才不要 pin 这条消息', '还是另请高明吧', '一点 pin 的动力都没有'} {'根本不想 pin 这条消息', '才不要 pin 这条消息', '还是另请高明吧', '一点 pin 的动力都没有'}
) )
} )
def __class_getitem__(cls, item: str):
collection = getattr(cls, item)
if cls._counter[item] % len(collection) == 0:
random.shuffle(collection)
cls._counter[item] += 1
collection.rotate()
return collection[-1]
_logger.remove() _logger.remove()
_logger.add(sys.stderr, _logger.add(sys.stderr,
@ -244,7 +258,7 @@ def pin(update: telegram.Update, ctx: telegram.ext.CallbackContext):
msg = update.effective_message msg = update.effective_message
msg_to_pin = msg.reply_to_message msg_to_pin = msg.reply_to_message
if not msg_to_pin: if not msg_to_pin:
vegetable = f'{random.choice(VEGETABLE["reject"])} (Reply to a message to use the command)' vegetable = f'{Vegetable["reject"]} (Reply to a message to use the command)'
msg.reply_text(vegetable) msg.reply_text(vegetable)
logger.warning(vegetable) logger.warning(vegetable)
return return
@ -254,7 +268,7 @@ def pin(update: telegram.Update, ctx: telegram.ext.CallbackContext):
msg_to_pin.pin(disable_notification=True) msg_to_pin.pin(disable_notification=True)
logger.info(f'Pinned {msg_to_pin.text}') logger.info(f'Pinned {msg_to_pin.text}')
except telegram.error.BadRequest as e: except telegram.error.BadRequest as e:
vegetable = f'{random.choice(VEGETABLE["permission_denied"])} ({e})' vegetable = f'{Vegetable["permission_denied"]} ({e})'
msg_to_pin.reply_text(vegetable) msg_to_pin.reply_text(vegetable)
logger.warning(vegetable) logger.warning(vegetable)