Skip to content

Commit

Permalink
[UPDATE] Support python 3.10 and onebot v11
Browse files Browse the repository at this point in the history
  • Loading branch information
Diving-Fish committed Sep 7, 2022
1 parent 3bea2d5 commit 3f67be3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 122 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ message:

servers:
- ws-reverse:
universal: ws://127.0.0.1:10219/cqhttp/ws
universal: ws://127.0.0.1:10219/onebot/v11/ws
```
然后设置您的 QQ 号和密码。您也可以不设置密码,选择扫码登陆的方式。
Expand Down
6 changes: 3 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import defaultdict

import nonebot
from nonebot.adapters.cqhttp import Bot as CQHTTPBot
from nonebot.adapters.onebot.v11 import Adapter


# Custom your logger
Expand All @@ -21,7 +21,7 @@
app = nonebot.get_asgi()

driver = nonebot.get_driver()
driver.register_adapter("cqhttp", CQHTTPBot)
driver.register_adapter(Adapter)
driver.config.help_text = {}


Expand All @@ -34,4 +34,4 @@


if __name__ == "__main__":
nonebot.run(app="bot:app")
nonebot.run()
Binary file modified requirements.txt
Binary file not shown.
159 changes: 60 additions & 99 deletions src/plugins/maimaidx.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from collections import defaultdict

from nonebot import on_command, on_regex
from nonebot.typing import T_State
from nonebot.adapters import Event, Bot
from nonebot.adapters.cqhttp import Message
from nonebot.params import CommandArg, EventMessage
from nonebot.adapters import Event
from nonebot.adapters.onebot.v11 import Message, MessageSegment

from src.libraries.tool import hash
from src.libraries.maimaidx_music import *
Expand All @@ -15,24 +13,15 @@

def song_txt(music: Music):
return Message([
{
"type": "text",
"data": {
"text": f"{music.id}. {music.title}\n"
}
},
{
"type": "image",
"data": {
"file": f"https://www.diving-fish.com/covers/{get_cover_len4_id(music.id)}.png"
}
},
{
"type": "text",
"data": {
"text": f"\n{'/'.join(music.level)}"
}
}
MessageSegment("text", {
"text": f"{music.id}. {music.title}\n"
}),
MessageSegment("image", {
"file": f"https://www.diving-fish.com/covers/{get_cover_len4_id(music.id)}.png"
}),
MessageSegment("text", {
"text": f"\n{'/'.join(music.level)}"
})
])


Expand All @@ -53,8 +42,8 @@ def inner_level_q(ds1, ds2=None):


@inner_level.handle()
async def _(bot: Bot, event: Event, state: T_State):
argv = str(event.get_message()).strip().split(" ")
async def _(event: Event, message: Message = CommandArg()):
argv = str(message).strip().split(" ")
if len(argv) > 2 or len(argv) == 0:
await inner_level.finish("命令格式为\n定数查歌 <定数>\n定数查歌 <定数下限> <定数上限>")
return
Expand All @@ -75,10 +64,10 @@ async def _(bot: Bot, event: Event, state: T_State):


@spec_rand.handle()
async def _(bot: Bot, event: Event, state: T_State):
async def _(event: Event, message: Message = EventMessage()):
level_labels = ['绿', '黄', '红', '紫', '白']
regex = "随个((?:dx|sd|标准))?([绿黄红紫白]?)([0-9]+\+?)"
res = re.match(regex, str(event.get_message()).lower())
res = re.match(regex, str(message).lower())
try:
if res.groups()[0] == "dx":
tp = ["DX"]
Expand All @@ -105,17 +94,17 @@ async def _(bot: Bot, event: Event, state: T_State):


@mr.handle()
async def _(bot: Bot, event: Event, state: T_State):
async def _():
await mr.finish(song_txt(total_list.random()))


search_music = on_regex(r"^查歌.+")


@search_music.handle()
async def _(bot: Bot, event: Event, state: T_State):
async def _(event: Event, message: Message = EventMessage()):
regex = "查歌(.+)"
name = re.match(regex, str(event.get_message())).groups()[0].strip()
name = re.match(regex, str(message)).groups()[0].strip()
if name == "":
return
res = total_list.filter(title_search=name)
Expand All @@ -126,10 +115,9 @@ async def _(bot: Bot, event: Event, state: T_State):
for music in sorted(res, key = lambda i: int(i['id'])):
search_result += f"{music['id']}. {music['title']}\n"
await search_music.finish(Message([
{"type": "text",
"data": {
"text": search_result.strip()
}}]))
MessageSegment("text", {
"text": search_result.strip()
})]))
else:
await search_music.send(f"结果过多({len(res)} 条),请缩小查询范围。")

Expand All @@ -138,9 +126,9 @@ async def _(bot: Bot, event: Event, state: T_State):


@query_chart.handle()
async def _(bot: Bot, event: Event, state: T_State):
async def _(event: Event, message: Message = EventMessage()):
regex = "([绿黄红紫白]?)id([0-9]+)"
groups = re.match(regex, str(event.get_message())).groups()
groups = re.match(regex, str(message)).groups()
level_labels = ['绿', '黄', '红', '紫', '白']
if groups[0] != "":
try:
Expand Down Expand Up @@ -168,24 +156,15 @@ async def _(bot: Bot, event: Event, state: T_State):
BREAK: {chart['notes'][4]}
谱师: {chart['charter']}'''
await query_chart.send(Message([
{
"type": "text",
"data": {
"text": f"{music['id']}. {music['title']}\n"
}
},
{
"type": "image",
"data": {
"file": f"{file}"
}
},
{
"type": "text",
"data": {
"text": msg
}
}
MessageSegment("text", {
"text": f"{music['id']}. {music['title']}\n"
}),
MessageSegment("image", {
"file": f"{file}"
}),
MessageSegment("text", {
"text": msg
})
]))
except Exception:
await query_chart.send("未找到该谱面")
Expand All @@ -195,24 +174,15 @@ async def _(bot: Bot, event: Event, state: T_State):
try:
file =f"https://www.diving-fish.com/covers/{get_cover_len4_id(music['id'])}.png"
await query_chart.send(Message([
{
"type": "text",
"data": {
"text": f"{music['id']}. {music['title']}\n"
}
},
{
"type": "image",
"data": {
"file": f"{file}"
}
},
{
"type": "text",
"data": {
"text": f"艺术家: {music['basic_info']['artist']}\n分类: {music['basic_info']['genre']}\nBPM: {music['basic_info']['bpm']}\n版本: {music['basic_info']['from']}\n难度: {'/'.join(music['level'])}"
}
}
MessageSegment("text", {
"text": f"{music['id']}. {music['title']}\n"
}),
MessageSegment("image", {
"file": f"{file}"
}),
MessageSegment("text", {
"text": f"艺术家: {music['basic_info']['artist']}\n分类: {music['basic_info']['genre']}\nBPM: {music['basic_info']['bpm']}\n版本: {music['basic_info']['from']}\n难度: {'/'.join(music['level'])}"
})
]))
except Exception:
await query_chart.send("未找到该乐曲")
Expand All @@ -225,7 +195,7 @@ async def _(bot: Bot, event: Event, state: T_State):


@jrwm.handle()
async def _(bot: Bot, event: Event, state: T_State):
async def _(event: Event, message: Message = CommandArg()):
qq = int(event.get_user_id())
h = hash(qq)
rp = h % 100
Expand All @@ -241,17 +211,15 @@ async def _(bot: Bot, event: Event, state: T_State):
s += f'忌 {wm_list[i]}\n'
s += "千雪提醒您:打机时不要大力拍打或滑动哦\n今日推荐歌曲:"
music = total_list[h % len(total_list)]
await jrwm.finish(Message([
{"type": "text", "data": {"text": s}}
] + song_txt(music)))
await jrwm.finish(Message([MessageSegment("text", {"text": s})] + song_txt(music)))

query_score = on_command('分数线')


@query_score.handle()
async def _(bot: Bot, event: Event, state: T_State):
async def _(event: Event, message: Message = CommandArg()):
r = "([绿黄红紫白])(id)?([0-9]+)"
argv = str(event.get_message()).strip().split(" ")
argv = str(message).strip().split(" ")
if len(argv) == 1 and argv[0] == '帮助':
s = '''此功能为查找某首歌分数线设计。
命令格式:分数线 <难度+歌曲id> <分数线>
Expand All @@ -264,12 +232,11 @@ async def _(bot: Bot, event: Event, state: T_State):
SLIDE\t3/7.5/15
TOUCH\t1/2.5/5
BREAK\t5/12.5/25(外加200落)'''
await query_score.send(Message([{
"type": "image",
"data": {
await query_score.send(Message([
MessageSegment("image", {
"file": f"base64://{str(image_to_base64(text_to_image(s)), encoding='utf-8')}"
}
}]))
})
]))
elif len(argv) == 2:
try:
grp = re.match(r, argv[0]).groups()
Expand Down Expand Up @@ -302,8 +269,8 @@ async def _(bot: Bot, event: Event, state: T_State):


@best_40_pic.handle()
async def _(bot: Bot, event: Event, state: T_State):
username = str(event.get_message()).strip()
async def _(event: Event, message: Message = CommandArg()):
username = str(message).strip()
if username == "":
payload = {'qq': str(event.get_user_id())}
else:
Expand All @@ -315,20 +282,17 @@ async def _(bot: Bot, event: Event, state: T_State):
await best_40_pic.send("该用户禁止了其他人获取数据。")
else:
await best_40_pic.send(Message([
{
"type": "image",
"data": {
"file": f"base64://{str(image_to_base64(img), encoding='utf-8')}"
}
}
MessageSegment("image", {
"file": f"base64://{str(image_to_base64(img), encoding='utf-8')}"
})
]))

best_50_pic = on_command('b50')


@best_50_pic.handle()
async def _(bot: Bot, event: Event, state: T_State):
username = str(event.get_message()).strip()
async def _(event: Event, message: Message = CommandArg()):
username = str(message).strip()
if username == "":
payload = {'qq': str(event.get_user_id()),'b50':True}
else:
Expand All @@ -340,10 +304,7 @@ async def _(bot: Bot, event: Event, state: T_State):
await best_50_pic.send("该用户禁止了其他人获取数据。")
else:
await best_50_pic.send(Message([
{
"type": "image",
"data": {
"file": f"base64://{str(image_to_base64(img), encoding='utf-8')}"
}
}
MessageSegment("image", {
"file": f"base64://{str(image_to_base64(img), encoding='utf-8')}"
})
]))
31 changes: 12 additions & 19 deletions src/plugins/public.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import random
import re

from PIL import Image
from nonebot import on_command, on_message, on_notice, require, get_driver, on_regex
from nonebot import on_command, on_notice
from nonebot.typing import T_State
from nonebot.adapters.cqhttp import Message, Event, Bot
from nonebot.adapters.onebot.v11 import Message, Event, Bot, MessageSegment
from nonebot.exception import IgnoredException
from nonebot.message import event_preprocessor
from src.libraries.image import *
from random import randint


@event_preprocessor
Expand All @@ -32,15 +27,14 @@ async def _(bot: Bot, event: Event, state: T_State):
定数查歌 <定数> 查询定数对应的乐曲
定数查歌 <定数下限> <定数上限>
分数线 <难度+歌曲id> <分数线> 详情请输入“分数线 帮助”查看'''
await help.send(Message([{
"type": "image",
"data": {
await help.send(Message([
MessageSegment("image", {
"file": f"base64://{str(image_to_base64(text_to_image(help_str)), encoding='utf-8')}"
}
}]))
})
]))


async def _group_poke(bot: Bot, event: Event, state: dict) -> bool:
async def _group_poke(bot: Bot, event: Event) -> bool:
value = (event.notice_type == "notify" and event.sub_type == "poke" and event.target_id == int(bot.self_id))
return value

Expand All @@ -52,10 +46,9 @@ async def _group_poke(bot: Bot, event: Event, state: dict) -> bool:
async def _(bot: Bot, event: Event, state: T_State):
if event.__getattribute__('group_id') is None:
event.__delattr__('group_id')
await poke.send(Message([{
"type": "poke",
"data": {
"qq": f"{event.sender_id}"
}
}]))
await poke.send(Message([
MessageSegment("poke", {
"qq": f"{event.sender_id}"
})
]))

0 comments on commit 3f67be3

Please sign in to comment.