PagerMaid_Plugins_Pyro/lottery/main.py

129 lines
4.7 KiB
Python
Raw Normal View History

2022-07-19 13:49:00 +00:00
import contextlib
import secrets
from pagermaid import bot, log
2022-07-19 14:00:47 +00:00
from pagermaid.utils import alias_command
2022-07-19 13:49:00 +00:00
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 = """抽奖活动 <b>{}</b> 已经创建
奖品数量<b>{}</b>
参与人数达到 <b>{}</b> 即可开奖
发送 <code>{}</code> 即可参与抽奖"""
join_text = """感谢参与 <b>{}</b> 抽奖活动
奖品数量<b>{}</b>
参与人数达到 <b>{}</b> 即可开奖
当前参与人数<b>{}</b> """
end_text = """<b>{}</b> 已开奖,中奖用户:
{}
2022-07-22 07:05:25 +00:00
请私聊发起者领奖感谢其他用户的参与"""
2022-07-19 13:49:00 +00:00
end_empty_text = """<b>{}</b> 已开奖,没有中奖用户"""
async def lottery_end():
lottery_json["start"] = False
2022-07-22 07:05:25 +00:00
all_user = lottery_bot.get_subs()[:lottery_json["num"]]
2022-07-19 13:49:00 +00:00
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"<a href=\"tg://user?id={uid}\">@{uid}</a>" 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
2022-07-22 07:05:25 +00:00
if message.text != lottery_json["keyword"]:
2022-07-19 13:49:00 +00:00
return
if lottery_bot.check_id(message.from_user.id):
return
lottery_bot.add_id(message.from_user.id)
2022-07-22 07:05:25 +00:00
all_join = len(lottery_bot.get_subs())
2022-07-22 07:57:38 +00:00
if all_join > (lottery_json["num"]):
2022-07-22 07:05:25 +00:00
lottery_json["start"] = False
return await lottery_end()
2022-07-19 13:49:00 +00:00
with contextlib.suppress(Exception):
reply = await message.reply(
join_text.format(
lottery_json["title"],
lottery_json["win"],
lottery_json["num"],
2022-07-22 07:05:25 +00:00
all_join))
2022-07-19 13:49:00 +00:00
add_delete_message_job(reply, 15)
2022-07-22 07:57:38 +00:00
if all_join >= lottery_json["num"]:
lottery_json["start"] = False
await lottery_end()
2022-07-19 13:49:00 +00:00
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,
need_admin=True,
2022-07-19 13:49:00 +00:00
parameters="<奖品数/人数> <关键词> <标题> / 强制开奖",
2022-07-19 14:00:47 +00:00
description=f"举行抽奖活动\n\n例如:,{alias_command('lottery')} 1/10 测试 测试")
2022-07-19 13:49:00 +00:00
async def lottery(message: Message):
if not message.arguments:
2022-07-19 14:00:47 +00:00
return await message.edit(
f"请输入 奖品数、人数等参数 或者 强制开奖\n\n例如 `,{alias_command('lottery')} 1/10 测试 测试`")
2022-07-19 13:49:00 +00:00
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("有抽奖活动正在进行,请稍后再试")