ica-plugins/plugins/hyp_vote.py

84 lines
2.3 KiB
Python
Raw Normal View History

2024-08-09 15:36:03 +08:00
from __future__ import annotations
2024-08-08 23:14:00 +08:00
import re
from typing import TYPE_CHECKING, TypeVar
if TYPE_CHECKING:
from ica_typing import IcaNewMessage, IcaClient
else:
IcaNewMessage = TypeVar("NewMessage")
IcaClient = TypeVar("IcaClient")
2024-08-09 15:36:03 +08:00
def gen_room() -> dict[int, list[int]]:
return {i: [] for i in range(0, 24)}
2024-08-09 13:23:13 +08:00
VOTE = {}
2024-08-08 23:14:00 +08:00
2024-08-09 13:23:13 +08:00
def fmt_vote(room_id) -> str:
global VOTE
if room_id not in VOTE:
2024-08-09 15:36:03 +08:00
VOTE[room_id] = gen_room()
2024-08-09 14:24:28 +08:00
return "\n".join(
f"{x}: {len(VOTE[room_id][x])}" for x in VOTE[room_id] if VOTE[room_id][x]
2024-08-09 13:23:13 +08:00
)
2024-08-08 23:14:00 +08:00
2024-08-09 13:23:13 +08:00
def hypvote(msg: IcaNewMessage, client: IcaClient):
global VOTE
matchs = re.match("/hyp (.+)", msg.content + " ")
if matchs:
arg = matchs.group(1).split(" ")
else:
return
if msg.room_id not in VOTE:
2024-08-09 15:36:03 +08:00
VOTE[msg.room_id] = gen_room()
2024-08-08 23:14:00 +08:00
if arg[0] == "vote":
2024-08-09 13:23:13 +08:00
for x in arg[1:]:
if x.isdigit() and 0 <= int(x) < 24:
if msg.sender_id in VOTE[msg.room_id][int(x) % 24]:
continue
VOTE[msg.room_id][int(x) % 24].append(msg.sender_id)
elif arg[0] == "unvote":
for x in arg[1:]:
if x.isdigit() and 0 <= int(x) < 24:
2024-08-09 13:30:29 +08:00
if msg.sender_id in VOTE[msg.room_id][int(x) % 24]:
2024-08-09 13:23:13 +08:00
VOTE[msg.room_id][int(x) % 24].remove(msg.sender_id)
elif arg[0] == "clear":
2024-08-09 15:36:03 +08:00
VOTE[msg.room_id] = gen_room()
2024-08-09 16:15:18 +08:00
elif arg[0] == "view":
...
2024-08-09 13:23:13 +08:00
elif arg == [] or arg[0] == "ls":
res = fmt_vote(msg.room_id)
reply = msg.reply_with(res)
client.send_message(reply)
elif arg[0] == "help":
reply = msg.reply_with("""NAME
2024-08-08 23:14:00 +08:00
/hyp - 计划时间高效开黑
SYNOPSIS
/hyp [command] [args]
OPTIONS
vote <space seperated hour>
vote for time you want to play
unvote <space seperated hour>
unvote for time you want to play
clear
clear the vote (OP only)
ls
list voted time, equivalent to empty
2024-08-09 16:15:18 +08:00
view <space seperated hour>
2024-08-08 23:14:00 +08:00
help
show this help
AUTHOR
dongdigua
2024-08-09 13:23:13 +08:00
shenjack(bugfixs)
2024-08-08 23:14:00 +08:00
""")
2024-08-09 13:23:13 +08:00
client.send_message(reply)
def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None:
if (not (msg.is_from_self or msg.is_reply)) and msg.content.startswith("/hyp"):
hypvote(msg, client)