From 8c63feb0eba13bb3fddb5553df80f7bedd1a5a28 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 19 Jul 2022 21:49:00 +0800 Subject: [PATCH] =?UTF-8?q?lottery=20=E4=B8=BE=E8=A1=8C=E5=8D=95=E7=BE=A4?= =?UTF-8?q?=E6=8A=BD=E5=A5=96=E6=B4=BB=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list.json | 10 ++++ lottery/main.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 lottery/main.py diff --git a/list.json b/list.json index 1e738b0..cea6c67 100644 --- a/list.json +++ b/list.json @@ -359,6 +359,16 @@ "supported": true, "des-short": "解析 Telegram 聊天窗口背景图。", "des": "解析 Telegram 聊天窗口背景图。\n指令:,tgbg" + }, + { + "name": "lottery", + "version": "1.0", + "section": "chat", + "maintainer": "xtaodada", + "size": "4.51 kb", + "supported": true, + "des-short": "举行单群抽奖活动。", + "des": "举行单群抽奖活动。\n指令:,lottery" } ] } diff --git a/lottery/main.py b/lottery/main.py new file mode 100644 index 0000000..22abcae --- /dev/null +++ b/lottery/main.py @@ -0,0 +1,121 @@ +import contextlib +import secrets + +from pagermaid import bot, log +from pagermaid.listener import listener +from pagermaid.single_utils import Message +from pagermaid.sub_utils import Sub +from pagermaid.scheduler import add_delete_message_job + +lottery_bot = Sub("lottery_bot") +lottery_json = {"start": False, "chat_id": 0, "num": 0, "win": 0, "title": "", "keyword": ""} +create_text = """抽奖活动 {} 已经创建 +奖品数量:{} +参与人数达到 {} 人,即可开奖 + +发送 {} 即可参与抽奖""" +join_text = """感谢参与 {} 抽奖活动 +奖品数量:{} +参与人数达到 {} 人,即可开奖 +当前参与人数:{} 人""" +end_text = """{} 已开奖,中奖用户: + +{} + +请私聊我领奖,感谢其他用户的参与。""" +end_empty_text = """{} 已开奖,没有中奖用户""" + + +async def lottery_end(): + lottery_json["start"] = False + all_user = lottery_bot.get_subs() + secret_generator = secrets.SystemRandom() + win_user = [] + for _ in range(min(lottery_json["win"], len(all_user))): + temp = secret_generator.choice(all_user) + if temp not in win_user: + win_user.append(temp) + win_text = end_text.format( + lottery_json["title"], + "\n".join(f"@{uid}" for uid in win_user + )) if win_user else end_empty_text.format(lottery_json["title"]) + with contextlib.suppress(Exception): + await bot.send_message(lottery_json["chat_id"], win_text) + with contextlib.suppress(Exception): + await log(win_text) + lottery_bot.clear_subs() + + +@listener(incoming=True, outgoing=True, groups_only=True) +async def handle_lottery(_, message: Message): + if not message.from_user: + return + if not message.text: + return + if not lottery_json["start"]: + return + if message.chat.id != lottery_json["chat_id"]: + return + if message.text.lower() != lottery_json["keyword"].lower(): + return + if lottery_bot.check_id(message.from_user.id): + return + lottery_bot.add_id(message.from_user.id) + with contextlib.suppress(Exception): + reply = await message.reply( + join_text.format( + lottery_json["title"], + lottery_json["win"], + lottery_json["num"], + len(lottery_bot.get_subs()))) + add_delete_message_job(reply, 15) + if len(lottery_bot.get_subs()) == lottery_json["num"]: + lottery_json["start"] = False + await lottery_end() + + +async def create_lottery(chat_id: int, num: int, win: int, title: str, keyword: str): + if lottery_json["start"]: + raise FileExistsError + lottery_bot.clear_subs() + lottery_json["start"] = True + lottery_json["chat_id"] = chat_id + lottery_json["num"] = num + lottery_json["win"] = win + lottery_json["title"] = title + lottery_json["keyword"] = keyword + with contextlib.suppress(Exception): + await bot.send_message(chat_id, create_text.format(title, win, num, keyword)) + + +@listener(command="lottery", + groups_only=True, + parameters="<奖品数/人数> <关键词> <标题> / 强制开奖", + description="举行抽奖活动") +async def lottery(message: Message): + if not message.arguments: + return await message.edit("请输入 奖品数、人数、关键字和标题 或者 强制开奖") + if message.arguments == "强制开奖": + await message.edit("强制开奖成功。") + return await lottery_end() + if len(message.parameter) < 3: + return await message.edit("奖品数、人数、关键字和标题不能为空") + num_list = message.parameter[0].split("/") + if len(num_list) != 2: + return await message.edit("奖品数、人数不能为空") + try: + num = int(num_list[1]) + win = int(num_list[0]) + if win > num: + return await message.edit("奖品数不能超过人数") + except ValueError: + return await message.edit("人数必须是整数") + keyword = message.parameter[1] + title = message.parameter[2] + if not (keyword and title): + return await message.edit("奖品数、人数、关键字和标题不能为空") + try: + await create_lottery(message.chat.id, num, win, title, keyword) + await message.safe_delete() + except FileExistsError: + return await message.edit("有抽奖活动正在进行,请稍后再试")