mirror of
https://github.com/TeamPGM/PagerMaid_Plugins_Pyro.git
synced 2024-11-16 07:05:24 +00:00
lottery 举行单群抽奖活动
This commit is contained in:
parent
02da0685e4
commit
8c63feb0eb
10
list.json
10
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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
121
lottery/main.py
Normal file
121
lottery/main.py
Normal file
@ -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 = """抽奖活动 <b>{}</b> 已经创建
|
||||
奖品数量:<b>{}</b>
|
||||
参与人数达到 <b>{}</b> 人,即可开奖
|
||||
|
||||
发送 <code>{}</code> 即可参与抽奖"""
|
||||
join_text = """感谢参与 <b>{}</b> 抽奖活动
|
||||
奖品数量:<b>{}</b>
|
||||
参与人数达到 <b>{}</b> 人,即可开奖
|
||||
当前参与人数:<b>{}</b> 人"""
|
||||
end_text = """<b>{}</b> 已开奖,中奖用户:
|
||||
|
||||
{}
|
||||
|
||||
请私聊我领奖,感谢其他用户的参与。"""
|
||||
end_empty_text = """<b>{}</b> 已开奖,没有中奖用户"""
|
||||
|
||||
|
||||
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"<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
|
||||
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("有抽奖活动正在进行,请稍后再试")
|
Loading…
Reference in New Issue
Block a user