|
11 | 11 | from astrbot.api.event import AstrMessageEvent, filter |
12 | 12 | from astrbot.api.star import Context, Star, StarTools, register |
13 | 13 |
|
14 | | -from .core import JMBrowser, JMConfigManager, JMDownloadManager, JMPacker |
| 14 | +from .core import JMAuthManager, JMBrowser, JMConfigManager, JMDownloadManager, JMPacker |
15 | 15 | from .utils import MessageFormatter |
16 | 16 |
|
17 | 17 | # 插件名称常量 |
@@ -53,6 +53,9 @@ def __init__(self, context: Context, config: AstrBotConfig = None): |
53 | 53 | # 初始化浏览查询器 |
54 | 54 | self.browser = JMBrowser(self.config_manager) |
55 | 55 |
|
| 56 | + # 初始化认证管理器 |
| 57 | + self.auth_manager = JMAuthManager(self.config_manager) |
| 58 | + |
56 | 59 | # 调试模式 |
57 | 60 | self.debug_mode = self.config_manager.debug_mode |
58 | 61 | if self.debug_mode: |
@@ -395,3 +398,82 @@ async def ranking_command( |
395 | 398 | except Exception as e: |
396 | 399 | logger.error(f"获取排行榜失败: {e}") |
397 | 400 | yield event.plain_result(MessageFormatter.format_error("network", str(e))) |
| 401 | + |
| 402 | + @filter.command("jmlogin") |
| 403 | + async def login_command( |
| 404 | + self, event: AstrMessageEvent, username: str = None, password: str = None |
| 405 | + ): |
| 406 | + """ |
| 407 | + 登录JM账号 |
| 408 | +
|
| 409 | + 用法: /jmlogin <用户名> <密码> |
| 410 | + 示例: /jmlogin myuser mypass |
| 411 | + """ |
| 412 | + # 权限检查 |
| 413 | + has_perm, error_msg = self._check_permission(event) |
| 414 | + if not has_perm: |
| 415 | + yield event.plain_result(error_msg) |
| 416 | + return |
| 417 | + |
| 418 | + # 参数检查 |
| 419 | + if username is None or password is None: |
| 420 | + yield event.plain_result( |
| 421 | + "❌ 请提供用户名和密码\n用法: /jmlogin <用户名> <密码>\n示例: /jmlogin myuser mypass" |
| 422 | + ) |
| 423 | + return |
| 424 | + |
| 425 | + try: |
| 426 | + yield event.plain_result("🔐 正在登录...") |
| 427 | + |
| 428 | + success, message = await self.auth_manager.login(username, password) |
| 429 | + |
| 430 | + if success: |
| 431 | + yield event.plain_result(f"✅ {message}") |
| 432 | + else: |
| 433 | + yield event.plain_result(f"❌ {message}") |
| 434 | + |
| 435 | + except Exception as e: |
| 436 | + logger.error(f"登录失败: {e}") |
| 437 | + yield event.plain_result(MessageFormatter.format_error("network", str(e))) |
| 438 | + |
| 439 | + @filter.command("jmlogout") |
| 440 | + async def logout_command(self, event: AstrMessageEvent): |
| 441 | + """ |
| 442 | + 登出JM账号 |
| 443 | +
|
| 444 | + 用法: /jmlogout |
| 445 | + """ |
| 446 | + # 权限检查 |
| 447 | + has_perm, error_msg = self._check_permission(event) |
| 448 | + if not has_perm: |
| 449 | + yield event.plain_result(error_msg) |
| 450 | + return |
| 451 | + |
| 452 | + success, message = self.auth_manager.logout() |
| 453 | + |
| 454 | + if success: |
| 455 | + yield event.plain_result(f"✅ {message}") |
| 456 | + else: |
| 457 | + yield event.plain_result(f"❌ {message}") |
| 458 | + |
| 459 | + @filter.command("jmstatus") |
| 460 | + async def status_command(self, event: AstrMessageEvent): |
| 461 | + """ |
| 462 | + 查看登录状态 |
| 463 | +
|
| 464 | + 用法: /jmstatus |
| 465 | + """ |
| 466 | + # 权限检查 |
| 467 | + has_perm, error_msg = self._check_permission(event) |
| 468 | + if not has_perm: |
| 469 | + yield event.plain_result(error_msg) |
| 470 | + return |
| 471 | + |
| 472 | + status = self.auth_manager.get_login_status() |
| 473 | + |
| 474 | + if status["logged_in"]: |
| 475 | + yield event.plain_result(f"✅ 已登录\n👤 用户名: {status['username']}") |
| 476 | + else: |
| 477 | + yield event.plain_result( |
| 478 | + "❌ 当前未登录\n💡 使用 /jmlogin <用户名> <密码> 登录" |
| 479 | + ) |
0 commit comments