lottery 修复高并发问题

This commit is contained in:
xtaodada 2022-07-22 15:05:25 +08:00
parent 77c51cc767
commit b034b716d0
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 10 additions and 9 deletions

View File

@ -362,10 +362,10 @@
}, },
{ {
"name": "lottery", "name": "lottery",
"version": "1.002", "version": "1.003",
"section": "chat", "section": "chat",
"maintainer": "xtaodada", "maintainer": "xtaodada",
"size": "4.51 kb", "size": "4.72 kb",
"supported": true, "supported": true,
"des-short": "举行单群抽奖活动。", "des-short": "举行单群抽奖活动。",
"des": "举行单群抽奖活动。\n指令,lottery" "des": "举行单群抽奖活动。\n指令,lottery"

View File

@ -23,13 +23,13 @@ end_text = """<b>{}</b> 已开奖,中奖用户:
{} {}
请私聊领奖感谢其他用户的参与""" 请私聊发起者领奖感谢其他用户的参与"""
end_empty_text = """<b>{}</b> 已开奖,没有中奖用户""" end_empty_text = """<b>{}</b> 已开奖,没有中奖用户"""
async def lottery_end(): async def lottery_end():
lottery_json["start"] = False lottery_json["start"] = False
all_user = lottery_bot.get_subs() all_user = lottery_bot.get_subs()[:lottery_json["num"]]
secret_generator = secrets.SystemRandom() secret_generator = secrets.SystemRandom()
win_user = [] win_user = []
for _ in range(min(lottery_json["win"], len(all_user))): for _ in range(min(lottery_json["win"], len(all_user))):
@ -57,22 +57,23 @@ async def handle_lottery(_, message: Message):
return return
if message.chat.id != lottery_json["chat_id"]: if message.chat.id != lottery_json["chat_id"]:
return return
if message.text.lower() != lottery_json["keyword"].lower(): if message.text != lottery_json["keyword"]:
return return
if lottery_bot.check_id(message.from_user.id): if lottery_bot.check_id(message.from_user.id):
return return
lottery_bot.add_id(message.from_user.id) lottery_bot.add_id(message.from_user.id)
all_join = len(lottery_bot.get_subs())
if all_join >= lottery_json["num"]:
lottery_json["start"] = False
return await lottery_end()
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
reply = await message.reply( reply = await message.reply(
join_text.format( join_text.format(
lottery_json["title"], lottery_json["title"],
lottery_json["win"], lottery_json["win"],
lottery_json["num"], lottery_json["num"],
len(lottery_bot.get_subs()))) all_join))
add_delete_message_job(reply, 15) 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): async def create_lottery(chat_id: int, num: int, win: int, title: str, keyword: str):