Skip to content

✨ 添加 unbind 指令 #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nonebot_plugin_tetris_stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'nonebot_plugin_session',
'nonebot_plugin_user',
'nonebot_plugin_userinfo',
'nonebot_plugin_waiter',
}

for i in require_plugins:
Expand Down
17 changes: 17 additions & 0 deletions nonebot_plugin_tetris_stats/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@
return status


async def remove_bind(
session: AsyncSession,
user: User,
game_platform: GameType,
) -> bool:
bind = await query_bind_info(

Check warning on line 71 in nonebot_plugin_tetris_stats/db/__init__.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/db/__init__.py#L71

Added line #L71 was not covered by tests
session=session,
user=user,
game_platform=game_platform,
)
if bind is not None:
await session.delete(bind)
await session.commit()
return True
return False

Check warning on line 80 in nonebot_plugin_tetris_stats/db/__init__.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/db/__init__.py#L76-L80

Added lines #L76 - L80 were not covered by tests


T = TypeVar('T', 'TETRIOHistoricalData', 'TOPHistoricalData', 'TOSHistoricalData')

lock = Lock()
Expand Down
3 changes: 2 additions & 1 deletion nonebot_plugin_tetris_stats/games/tetrio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_player(user_id_or_name: str) -> Player | MessageFormatError:
)


from . import bind, config, list, query, rank, record # noqa: A004, E402
from . import bind, config, list, query, rank, record, unbind # noqa: A004, E402

main_command.add(command)

Expand All @@ -35,4 +35,5 @@ def get_player(user_id_or_name: str) -> Player | MessageFormatError:
'query',
'rank',
'record',
'unbind',
]
75 changes: 75 additions & 0 deletions nonebot_plugin_tetris_stats/games/tetrio/unbind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from hashlib import md5

from nonebot_plugin_alconna import Subcommand
from nonebot_plugin_alconna.uniseg import UniMessage
from nonebot_plugin_orm import get_session
from nonebot_plugin_session import EventSession
from nonebot_plugin_session_orm import get_session_persist_id # type: ignore[import-untyped]
from nonebot_plugin_user import User
from nonebot_plugin_userinfo import BotUserInfo, UserInfo
from nonebot_plugin_waiter import suggest # type: ignore[import-untyped]
from yarl import URL

from ...db import query_bind_info, remove_bind, trigger
from ...utils.host import HostPage, get_self_netloc
from ...utils.image import get_avatar
from ...utils.render import Bind, render
from ...utils.render.schemas.base import Avatar, People
from ...utils.screenshot import screenshot
from . import alc, command
from .api import Player
from .constant import GAME_TYPE

command.add(Subcommand('unbind', help_text='解除绑定 TETR.IO 账号'))

alc.shortcut(
'(?i:io)(?i:解除绑定|解绑|unbind)',
command='tstats TETR.IO unbind',
humanized='io解绑',
)


@alc.assign('TETRIO.unbind')
async def _(nb_user: User, event_session: EventSession, bot_info: UserInfo = BotUserInfo()): # noqa: B008
async with (

Check warning on line 34 in nonebot_plugin_tetris_stats/games/tetrio/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/unbind.py#L34

Added line #L34 was not covered by tests
trigger(
session_persist_id=await get_session_persist_id(event_session),
game_platform=GAME_TYPE,
command_type='unbind',
command_args=[],
),
get_session() as session,
):
if (bind := await query_bind_info(session=session, user=nb_user, game_platform=GAME_TYPE)) is None:
await UniMessage('您还未绑定 TETR.IO 账号').finish()
resp = await suggest('您确定要解绑吗?', ['是', '否'])
if resp is None or resp.extract_plain_text() == '否':
return
player = Player(user_id=bind.game_account, trust=True)
user = await player.user
netloc = get_self_netloc()
async with HostPage(

Check warning on line 51 in nonebot_plugin_tetris_stats/games/tetrio/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/unbind.py#L43-L51

Added lines #L43 - L51 were not covered by tests
await render(
'v1/binding',
Bind(
platform='TETR.IO',
status='unlink',
user=People(
avatar=str(
URL(f'http://{netloc}/host/resource/tetrio/avatars/{user.ID}')
% {'revision': avatar_revision}
)
if (avatar_revision := (await player.avatar_revision)) is not None and avatar_revision != 0
else Avatar(type='identicon', hash=md5(user.ID.encode()).hexdigest()), # noqa: S324
name=user.name.upper(),
),
bot=People(
avatar=await get_avatar(bot_info, 'Data URI', '../../static/logo/logo.svg'),
name=bot_info.user_name,
),
command='io绑定{游戏ID}',
),
)
) as page_hash:
await UniMessage.image(raw=await screenshot(f'http://{netloc}/host/{page_hash}.html')).send()
await remove_bind(session=session, user=nb_user, game_platform=GAME_TYPE)

Check warning on line 75 in nonebot_plugin_tetris_stats/games/tetrio/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/unbind.py#L74-L75

Added lines #L74 - L75 were not covered by tests
23 changes: 20 additions & 3 deletions nonebot_plugin_tetris_stats/games/top/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def get_player(name: str) -> Player | MessageFormatError:
),
help_text='绑定 TOP 账号',
),
Subcommand(
'unbind',
help_text='解除绑定 TOP 账号',
),
Subcommand(
'query',
Args(
Expand All @@ -51,9 +55,22 @@ def get_player(name: str) -> Player | MessageFormatError:
)
)

alc.shortcut('(?i:top)(?i:绑定|绑|bind)', {'command': 'tstats TOP bind', 'humanized': 'top绑定'})
alc.shortcut('(?i:top)(?i:查询|查|query|stats)', {'command': 'tstats TOP query', 'humanized': 'top查'})
alc.shortcut(
'(?i:top)(?i:绑定|绑|bind)',
command='tstats TOP bind',
humanized='top绑定',
)
alc.shortcut(
'(?i:top)(?i:解除绑定|解绑|unbind)',
command='tstats TOP unbind',
humanized='top解绑',
)
alc.shortcut(
'(?i:top)(?i:查询|查|query|stats)',
command='tstats TOP query',
humanized='top查',
)

add_block_handlers(alc.assign('TOP.query'))

from . import bind, query # noqa: E402, F401
from . import bind, query, unbind # noqa: E402, F401
63 changes: 63 additions & 0 deletions nonebot_plugin_tetris_stats/games/top/unbind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from nonebot_plugin_alconna.uniseg import UniMessage
from nonebot_plugin_orm import get_session
from nonebot_plugin_session import EventSession
from nonebot_plugin_session_orm import get_session_persist_id # type: ignore[import-untyped]
from nonebot_plugin_user import User
from nonebot_plugin_userinfo import BotUserInfo, EventUserInfo, UserInfo
from nonebot_plugin_waiter import suggest # type: ignore[import-untyped]

from ...db import query_bind_info, remove_bind, trigger
from ...utils.host import HostPage, get_self_netloc
from ...utils.image import get_avatar
from ...utils.render import Bind, render
from ...utils.render.schemas.base import People
from ...utils.screenshot import screenshot
from . import alc
from .api import Player
from .constant import GAME_TYPE


@alc.assign('TOP.unbind')
async def _(
nb_user: User,
event_session: EventSession,
event_user_info: UserInfo = EventUserInfo(), # noqa: B008
bot_info: UserInfo = BotUserInfo(), # noqa: B008
):
async with (

Check warning on line 27 in nonebot_plugin_tetris_stats/games/top/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/top/unbind.py#L27

Added line #L27 was not covered by tests
trigger(
session_persist_id=await get_session_persist_id(event_session),
game_platform=GAME_TYPE,
command_type='unbind',
command_args=[],
),
get_session() as session,
):
if (bind := await query_bind_info(session=session, user=nb_user, game_platform=GAME_TYPE)) is None:
await UniMessage('您还未绑定 TOP 账号').finish()
resp = await suggest('您确定要解绑吗?', ['是', '否'])
if resp is None or resp.extract_plain_text() == '否':
return
player = Player(user_name=bind.game_account, trust=True)
user = await player.user
netloc = get_self_netloc()
async with HostPage(

Check warning on line 44 in nonebot_plugin_tetris_stats/games/top/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/top/unbind.py#L36-L44

Added lines #L36 - L44 were not covered by tests
await render(
'v1/binding',
Bind(
platform='TOP',
status='unlink',
user=People(
avatar=await get_avatar(event_user_info, 'Data URI', None),
name=user.user_name,
),
bot=People(
avatar=await get_avatar(bot_info, 'Data URI', '../../static/logo/logo.svg'),
name=bot_info.user_name,
),
command='top绑定{游戏ID}',
),
)
) as page_hash:
await UniMessage.image(raw=await screenshot(f'http://{netloc}/host/{page_hash}.html')).send()
await remove_bind(session=session, user=nb_user, game_platform=GAME_TYPE)

Check warning on line 63 in nonebot_plugin_tetris_stats/games/top/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/top/unbind.py#L62-L63

Added lines #L62 - L63 were not covered by tests
23 changes: 20 additions & 3 deletions nonebot_plugin_tetris_stats/games/tos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def get_player(teaid_or_name: str) -> Player | MessageFormatError:
),
help_text='绑定 茶服 账号',
),
Subcommand(
'unbind',
help_text='解除绑定 TOS 账号',
),
Subcommand(
'query',
Args(
Expand All @@ -56,9 +60,22 @@ def get_player(teaid_or_name: str) -> Player | MessageFormatError:
)
)

alc.shortcut('(?i:tos|茶服)(?i:绑定|绑|bind)', {'command': 'tstats TOS bind', 'humanized': '茶服绑定'})
alc.shortcut('(?i:tos|茶服)(?i:查询|查|query|stats)', {'command': 'tstats TOS query', 'humanized': '茶服查'})
alc.shortcut(
'(?i:tos|茶服)(?i:绑定|绑|bind)',
command='tstats TOS bind',
humanized='茶服绑定',
)
alc.shortcut(
'(?i:tos|茶服)(?i:解除绑定|解绑|unbind)',
command='tstats TOS unbind',
humanized='茶服解绑',
)
alc.shortcut(
'(?i:tos|茶服)(?i:查询|查|query|stats)',
command='tstats TOS query',
humanized='茶服查',
)

add_block_handlers(alc.assign('TOS.query'))

from . import bind, query # noqa: E402, F401
from . import bind, query, unbind # noqa: E402, F401
66 changes: 66 additions & 0 deletions nonebot_plugin_tetris_stats/games/tos/unbind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from nonebot_plugin_alconna.uniseg import UniMessage
from nonebot_plugin_orm import get_session
from nonebot_plugin_session import EventSession
from nonebot_plugin_session_orm import get_session_persist_id # type: ignore[import-untyped]
from nonebot_plugin_user import User
from nonebot_plugin_userinfo import BotUserInfo, EventUserInfo, UserInfo
from nonebot_plugin_waiter import suggest # type: ignore[import-untyped]

from ...db import query_bind_info, remove_bind, trigger
from ...utils.host import HostPage, get_self_netloc
from ...utils.image import get_avatar
from ...utils.render import Bind, render
from ...utils.render.avatar import get_avatar as get_random_avatar
from ...utils.render.schemas.base import People
from ...utils.screenshot import screenshot
from . import alc
from .api import Player
from .constant import GAME_TYPE


@alc.assign('TOP.unbind')
async def _(
nb_user: User,
event_session: EventSession,
event_user_info: UserInfo = EventUserInfo(), # noqa: B008
bot_info: UserInfo = BotUserInfo(), # noqa: B008
):
async with (

Check warning on line 28 in nonebot_plugin_tetris_stats/games/tos/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tos/unbind.py#L28

Added line #L28 was not covered by tests
trigger(
session_persist_id=await get_session_persist_id(event_session),
game_platform=GAME_TYPE,
command_type='unbind',
command_args=[],
),
get_session() as session,
):
if (bind := await query_bind_info(session=session, user=nb_user, game_platform=GAME_TYPE)) is None:
await UniMessage('您还未绑定 TOP 账号').finish()
resp = await suggest('您确定要解绑吗?', ['是', '否'])
if resp is None or resp.extract_plain_text() == '否':
return
player = Player(user_name=bind.game_account, trust=True)
user = await player.user
netloc = get_self_netloc()
async with HostPage(

Check warning on line 45 in nonebot_plugin_tetris_stats/games/tos/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tos/unbind.py#L37-L45

Added lines #L37 - L45 were not covered by tests
await render(
'v1/binding',
Bind(
platform='TOS',
status='unlink',
user=People(
avatar=await get_avatar(event_user_info, 'Data URI', None)
if event_user_info is not None
else get_random_avatar(user.teaid),
name=user.name,
),
bot=People(
avatar=await get_avatar(bot_info, 'Data URI', '../../static/logo/logo.svg'),
name=bot_info.user_name,
),
command='茶服绑定{游戏ID}',
),
)
) as page_hash:
await UniMessage.image(raw=await screenshot(f'http://{netloc}/host/{page_hash}.html')).send()
await remove_bind(session=session, user=nb_user, game_platform=GAME_TYPE)

Check warning on line 66 in nonebot_plugin_tetris_stats/games/tos/unbind.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tos/unbind.py#L65-L66

Added lines #L65 - L66 were not covered by tests
2 changes: 1 addition & 1 deletion nonebot_plugin_tetris_stats/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Number: TypeAlias = float | int
GameType: TypeAlias = Literal['IO', 'TOP', 'TOS']
BaseCommandType: TypeAlias = Literal['bind', 'query']
BaseCommandType: TypeAlias = Literal['bind', 'unbind', 'query']
TETRIOCommandType: TypeAlias = BaseCommandType | Literal['rank', 'config', 'list', 'record']
AllCommandType: TypeAlias = BaseCommandType | TETRIOCommandType
Me: TypeAlias = Literal[
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies = [
"nonebot-plugin-session-orm>=0.2.0",
"nonebot-plugin-user>=0.4.4",
"nonebot-plugin-userinfo>=0.2.6",
"nonebot-plugin-waiter>=0.8.0",
"nonebot2[fastapi]>=2.3.3",
"pandas>=2.2.3",
"pillow>=11.0.0",
Expand Down
8 changes: 5 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading