-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
229 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "nonebot-plugin-deer-pipe" | ||
version = "0.1.4" | ||
version = "0.2.0" | ||
description = "A deer-pipe attendance nonebot2 plugin" | ||
authors = [ | ||
{name = "SNRainiar", email = "[email protected]"}, | ||
|
@@ -10,6 +10,8 @@ dependencies = [ | |
"nonebot-plugin-alconna>=0.51.1", | ||
"nonebot-plugin-localstore>=0.7.1", | ||
"nonebot-plugin-userinfo>=0.2.5", | ||
"sqlmodel>=0.0.21", | ||
"aiosqlite>=0.20.0", | ||
] | ||
requires-python = ">=3.10" | ||
readme = "README.md" | ||
|
@@ -33,7 +35,7 @@ build-backend = "pdm.backend" | |
distribution = true | ||
|
||
[tool.pdm.dev-dependencies] | ||
test = [ | ||
dev = [ | ||
"nonebot2[fastapi]>=2.3.2", | ||
"nonebot-adapter-console>=0.6.0", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,20 @@ | |
import nonebot_plugin_localstore as store | ||
|
||
|
||
# Plugin paths | ||
PLUGIN_PATH: Path = Path(os.path.dirname(os.path.realpath(__file__))) | ||
ASSETS_PATH: Path = PLUGIN_PATH / "assets" | ||
|
||
# Images | ||
CHECK_IMG: ImageFile = Image.open(ASSETS_PATH / "[email protected]") | ||
DEERPIPE_IMG: ImageFile = Image.open(ASSETS_PATH / "[email protected]") | ||
|
||
# Fonts | ||
MISANS_FONT: FreeTypeFont = ImageFont.truetype( | ||
ASSETS_PATH / "MiSans-Regular.ttf", | ||
25 | ||
) | ||
|
||
USERDATA_PATH: Path = store.get_plugin_data_file("userdata.json") | ||
# Database | ||
DATABASE_PATH: Path = store.get_plugin_data_file("userdata.db") | ||
DATABASE_URL: str = f"sqlite+aiosqlite:///{DATABASE_PATH}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from .contants import DATABASE_URL | ||
|
||
from datetime import datetime | ||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine | ||
from sqlmodel import Field, SQLModel, select | ||
from sqlmodel.ext.asyncio.session import AsyncSession | ||
from typing import Sequence | ||
|
||
|
||
# Model | ||
class User(SQLModel, table=True): | ||
user_id: str = Field(primary_key=True) | ||
year: int | ||
month: int | ||
mask: str = "0" | ||
|
||
# Async engine | ||
engin: AsyncEngine = create_async_engine(DATABASE_URL) | ||
initialized: bool = False | ||
|
||
# Attendance | ||
def get_seq(mask: str) -> list[int]: | ||
return list( | ||
map( | ||
lambda x: x[0] + 1, | ||
filter( | ||
lambda x: x[1] == '1', | ||
enumerate(mask) | ||
) | ||
) | ||
) | ||
|
||
async def attend(now: datetime, user_id: str) -> tuple[bool, Sequence[int]]: | ||
if not initialized: | ||
async with engin.begin() as conn: | ||
await conn.run_sync(SQLModel.metadata.create_all) | ||
|
||
async with AsyncSession(engin) as session: | ||
user: User | None = ( | ||
await session.exec(select(User).where(User.user_id == user_id)) | ||
).one_or_none() | ||
|
||
if user == None or user.year != now.year or user.month != now.month: | ||
user = User(user_id=user_id, year=now.year, month=now.month) | ||
|
||
mask: int = int(user.mask) | ||
if (mask >> (now.day - 1)) & 1 == 1: | ||
return (False, get_seq(user.mask)) | ||
else: | ||
mask |= (1 << (now.day - 1)) | ||
user.mask = str(mask) | ||
|
||
session.add(user) | ||
await session.commit() | ||
|
||
return (True, get_seq(str(mask))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.