Skip to content
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

feat: 支持 OneBot 12 适配器 #261

Merged
merged 6 commits into from
Jan 2, 2023
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
14 changes: 10 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ ci:
autoupdate_schedule: weekly
autoupdate_commit_msg: "chore: auto update by pre-commit hooks"
repos:
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.4
hooks:
- id: prettier
types_or: [markdown]

- repo: https://github.com/pycqa/isort
rev: 5.11.1
hooks:
Expand All @@ -21,8 +27,8 @@ repos:
- id: pyupgrade
args: [--py310-plus]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.4
- repo: https://github.com/hadialqattan/pycln
rev: v2.1.2
hooks:
- id: prettier
types_or: [markdown]
- id: pycln
args: [--config, pyproject.toml]
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"program": "${workspaceFolder}/bot.py",
"console": "integratedTerminal",
"justMyCode": false,
"envFile": "${workspaceFolder}/.env.prod"
},
{
"name": "CoolQBot Test",
Expand Down
4 changes: 0 additions & 4 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import nonebot
from nonebot.adapters.onebot.v11 import Adapter as OneBotV11Adapter
from nonebot.adapters.onebot.v12 import Adapter as OneBotV12Adapter
from nonebot.adapters.qqguild import Adapter as QQGuildAdapter
from nonebot.drivers import ForwardDriver
from nonebot.log import logger

nonebot.init()
Expand All @@ -11,8 +9,6 @@
driver = nonebot.get_driver()
driver.register_adapter(OneBotV11Adapter)
driver.register_adapter(OneBotV12Adapter)
if isinstance(driver, ForwardDriver):
driver.register_adapter(QQGuildAdapter)

# 加载插件
nonebot.load_from_toml("pyproject.toml")
Expand Down
18 changes: 1 addition & 17 deletions poetry.lock

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

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ version = "0.15.3"
python = "^3.10"
nonebot2 = { extras = ["httpx", "websockets"], version = "^2.0.0-rc.1" }
nonebot-adapter-onebot = "^2.1.0"
nonebot-adapter-qqguild = "^0.1.0"
nonebot-plugin-apscheduler = "^0.2.0"
nonebot-plugin-sentry = "^0.2.2"
nonebot-plugin-datastore = "^0.4.0"
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/ban/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
""" 自主禁言
"""
from enum import Enum
from typing import Optional

from nonebot import on_command, on_notice
from nonebot.adapters.onebot.v11 import Bot, Message, MessageSegment
Expand All @@ -19,7 +18,11 @@
__plugin_meta__ = PluginMetadata(
name="自主禁言",
description="禁言自己或解除自己的禁言",
usage="禁言自己,单位为分钟\n/ban 30 (禁言 30 分钟)\n解除禁言\n/ban 0\n如果私聊,则需要再提供群号",
usage="""禁言自己,单位为分钟
/ban 30 (禁言 30 分钟)
解除禁言
/ban 0
如果私聊,则需要再提供群号""",
)


Expand Down
1 change: 0 additions & 1 deletion src/plugins/ff14/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
""" 配置文件
"""
from typing import List

from nonebot_plugin_datastore import PluginData
from pydantic import BaseSettings, validator
Expand Down
116 changes: 67 additions & 49 deletions src/plugins/ff14/plugins/fflogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

查询副本输出数据。
"""
from dataclasses import dataclass
from typing import Any
from typing import cast

from nonebot.adapters import Event, Message, MessageSegment
from nonebot.adapters.onebot.v11 import Message as OneBotV11Message
from nonebot.adapters.onebot.v11 import MessageSegment as OneBotV11MessageSegment
from nonebot.adapters.qqguild import Message as QQGuildMessage
from nonebot.adapters.qqguild import MessageSegment as QQGuildMessageSegment
from nonebot.params import CommandArg
from nonebot.adapters.onebot.v12 import Message as OneBotV12Message
from nonebot.params import CommandArg, Depends
from nonebot.plugin import PluginMetadata

from src.utils.helpers import strtobool
Expand All @@ -22,49 +19,64 @@
__plugin_meta__ = PluginMetadata(
name="FFLogs",
description="副本输出查询",
usage="更新副本数据\n/dps update\n\n查询输出排行榜:\n/dps 副本名 职业 DPS种类(支持 rdps adps pdps,留空默认为 rdps)\n查询指定角色的排名:\n/dps 副本名 角色名 服务器名\n也可直接查询自己绑定角色的排名:\n/dps 副本名 me\n或查询他人绑定的角色排名\n/dps 副本名 @他人\n查询当前QQ号绑定的角色\n/dps me\n绑定自己的角色\n/dps me 角色名 服务器名\n查询他人绑定的角色\n/dps @他人",
usage="""更新副本数据
/dps update

查询输出排行榜:
/dps 副本名 职业 DPS种类(支持 rdps adps pdps,留空默认为 rdps)
查询指定角色的排名:
/dps 副本名 角色名 服务器名
也可直接查询自己绑定角色的排名:
/dps 副本名 me
或查询他人绑定的角色排名
/dps 副本名 @他人
查询当前QQ号绑定的角色
/dps me
绑定自己的角色
/dps me 角色名 服务器名
查询他人绑定的角色
/dps @他人""",
)

fflogs_cmd = ff14.command("dps", aliases={"dps"})


@dataclass
class User:
platform: str
user_id: str

def at(self) -> MessageSegment:
if self.platform == "qq":
return OneBotV11MessageSegment.at(self.user_id)
return QQGuildMessageSegment.mention_user(int(self.user_id))
async def get_user(
args: Message = CommandArg(),
) -> tuple[str, MessageSegment] | None:
"""获取提到的用户信息"""
if isinstance(args, OneBotV11Message) and (at := args["at"]):
at = at[0]
at = cast(MessageSegment, at)
return at.data["qq"], at
if isinstance(args, OneBotV12Message) and (mention := args["mention"]):
mention = mention[0]
mention = cast(MessageSegment, mention)
return mention.data["user_id"], mention


@fflogs_cmd.handle()
async def fflogs_handle(event: Event, args: Message = CommandArg()):
argv: list[Any] = args.extract_plain_text().split()

at_message = None
if isinstance(args, OneBotV11Message):
at_message = args["at"]
if at_message:
argv.append(User("qq", at_message[0].data["qq"]))
elif isinstance(args, QQGuildMessage):
at_message = args["mention_user"]
if at_message:
argv.append(User("qqguild", at_message[0].data["user_id"]))
async def fflogs_handle(
event: Event,
args: Message = CommandArg(),
at_user: tuple[str, MessageSegment] | None = Depends(get_user),
):
user_id = event.get_user_id()

argv: list[str | MessageSegment] = list(args.extract_plain_text().split())
if at_user:
argv.append(at_user[1])

if not argv:
await fflogs_cmd.finish(f"{__plugin_meta__.name}\n\n{__plugin_meta__.usage}")

user_id = event.get_user_id()

# 设置 Token
if argv[0] == "token" and len(argv) == 2:
# 检查是否是超级用户
if user_id not in global_config.superusers:
await fflogs_cmd.finish("抱歉,你没有权限修改 Token。")

plugin_config.fflogs_token = argv[1]
plugin_config.fflogs_token = str(argv[1])
await fflogs_cmd.finish("Token 设置完成。")

# 检查 Token 是否设置
Expand Down Expand Up @@ -96,7 +108,7 @@ async def fflogs_handle(event: Event, args: Message = CommandArg()):
# 检查是否是超级用户
if user_id not in global_config.superusers:
await fflogs_cmd.finish("抱歉,你没有权限设置缓存。")
if strtobool(argv[1]):
if strtobool(str(argv[1])):
if not fflogs.is_cache_enabled:
fflogs.enable_cache()
await fflogs_cmd.finish("已开始定时缓存。")
Expand All @@ -108,7 +120,7 @@ async def fflogs_handle(event: Event, args: Message = CommandArg()):
if argv[1] == "add":
if not plugin_config.fflogs_cache_boss:
plugin_config.fflogs_cache_boss = []
plugin_config.fflogs_cache_boss.append(argv[2])
plugin_config.fflogs_cache_boss.append(str(argv[2]))
# 触发 validator
plugin_config.fflogs_cache_boss = plugin_config.fflogs_cache_boss
await fflogs_cmd.finish(f"已添加副本 {argv[2]}。")
Expand All @@ -128,24 +140,26 @@ async def fflogs_handle(event: Event, args: Message = CommandArg()):
if argv[0] == "me" and len(argv) == 1:
if user_id not in fflogs.characters:
await fflogs_cmd.finish(
"抱歉,你没有绑定最终幻想14的角色。\n请使用\n/dps me 角色名 服务器名\n绑定自己的角色。"
"抱歉,你没有绑定最终幻想14的角色。\n请使用\n/dps me 角色名 服务器名\n绑定自己的角色。",
at_sender=True,
)
await fflogs_cmd.finish(
f"你当前绑定的角色:\n角色:{fflogs.characters[user_id][0]}\n服务器:{fflogs.characters[user_id][1]}"
f"你当前绑定的角色:\n角色:{fflogs.characters[user_id][0]}\n服务器:{fflogs.characters[user_id][1]}",
at_sender=True,
)

if isinstance(argv[0], User) and len(argv) == 1:
user_id = argv[0].user_id
if isinstance(argv[0], MessageSegment) and at_user and len(argv) == 1:
user_id = at_user[0]
if user_id not in fflogs.characters:
await fflogs_cmd.finish("抱歉,该用户没有绑定最终幻想14的角色。")
await fflogs_cmd.finish("抱歉,该用户没有绑定最终幻想14的角色。", at_sender=True)
await fflogs_cmd.finish(
argv[0].at()
at_user[1]
+ f"当前绑定的角色:\n角色:{fflogs.characters[user_id][0]}\n服务器:{fflogs.characters[user_id][1]}"
)

if argv[0] == "me" and len(argv) == 3:
fflogs.set_character(user_id, argv[1], argv[2])
await fflogs_cmd.finish("角色绑定成功!")
fflogs.set_character(user_id, str(argv[1]), str(argv[2]))
await fflogs_cmd.finish("角色绑定成功!", at_sender=True)

if argv[0] == "classes" and len(argv) == 1:
data = await fflogs.classes()
Expand All @@ -161,11 +175,18 @@ async def fflogs_handle(event: Event, args: Message = CommandArg()):
# <BOSS名> me
# <BOSS名> <@他人>
# <BOSS名> <职业名>
if not isinstance(argv[0], User) and isinstance(argv[1], User):
if (
not isinstance(argv[0], MessageSegment)
and isinstance(argv[1], MessageSegment)
and at_user
):
# @他人的格式
user_id = argv[1].user_id
data = await get_character_dps_by_user_id(argv[0], user_id)
elif not isinstance(argv[0], User) and argv[1].lower() == "me":
data = await get_character_dps_by_user_id(argv[0], at_user[0])
elif (
not isinstance(argv[0], MessageSegment)
and isinstance(argv[1], str)
and argv[1].lower() == "me"
):
data = await get_character_dps_by_user_id(argv[0], user_id)
else:
data = await fflogs.dps(*argv) # type:ignore
Expand All @@ -174,7 +195,7 @@ async def fflogs_handle(event: Event, args: Message = CommandArg()):
if len(argv) == 3:
# <BOSS名> <职业名> <DPS种类>
# <BOSS名> <角色名> <服务器名>
argv[2] = argv[2].lower()
argv[2] = str(argv[2]).lower()
if argv[2] in ["adps", "rdps", "pdps"]:
data = await fflogs.dps(*argv) # type:ignore
else:
Expand All @@ -192,6 +213,3 @@ async def get_character_dps_by_user_id(boss_nickname: str, user_id: str):
boss_nickname,
*fflogs.characters[user_id],
)


# endregion
2 changes: 1 addition & 1 deletion src/plugins/ff14/plugins/fflogs/fflogs_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import json
import math
from datetime import datetime, timedelta
from typing import List, Literal, cast
from typing import Literal, cast

import httpx
from nonebot.log import logger
Expand Down
1 change: 0 additions & 1 deletion src/plugins/ff14/plugins/fflogs/fflogs_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

副本与职业数据
"""
from typing import Optional

from pydantic import BaseModel

Expand Down
2 changes: 0 additions & 2 deletions src/plugins/ff14/plugins/fflogs/fflogs_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from pydantic import BaseModel


Expand Down
4 changes: 3 additions & 1 deletion src/plugins/ff14/plugins/gate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
__plugin_meta__ = PluginMetadata(
name="藏宝选门",
description="告诉我该选哪个门吧",
usage="选择门的数量\n/gate 2\n/gate 3",
usage="""选择门的数量
/gate 2
/gate 3""",
)

gate_cmd = ff14.command("gate", aliases={"gate"})
Expand Down
6 changes: 2 additions & 4 deletions src/plugins/ff14/plugins/nuannuan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
__plugin_meta__ = PluginMetadata(
name="时尚品鉴",
description="获取最新的满分攻略",
usage="获取最新的满分攻略\n/时尚品鉴",
usage="""获取最新的满分攻略
/时尚品鉴""",
)
nuannuan_cmd = ff14.command("nuannuan", aliases={"时尚品鉴"})

Expand All @@ -20,6 +21,3 @@ async def nuannuan_handle():
await nuannuan_cmd.finish(latest)
else:
await nuannuan_cmd.finish("抱歉,没有找到最新的满分攻略。")


# endregion
1 change: 0 additions & 1 deletion src/plugins/ff14/plugins/nuannuan/data_source.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
""" 时尚品鉴 """
import re
from typing import Optional

import httpx

Expand Down
Loading