Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
实现群成员禁言/解除禁言API及事件、实现群成员加入事件 (#3)
Browse files Browse the repository at this point in the history
* ✨ 实现群成员禁言/解除禁言事件、群成员加入事件

* ✨ 实现群成员禁言/解除禁言
  • Loading branch information
ssttkkl authored Sep 28, 2023
1 parent effb21d commit 02c7f4c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 25 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
nonebot-adapter-onebot-pretender
======
# nonebot-adapter-onebot-pretender

With some magic ✨

Expand All @@ -17,23 +16,24 @@ With some magic ✨
- [ ] 合并转发(调不通orz)
- [ ] 消息发送者角色判断(消息上报少字段orz)
- [ ] 其他OB11事件
- [x] 群成员禁言/解除禁言事件
- [x] 群成员加入事件 (包括旧版受邀请入群)
- [ ] 群名称改动事件
- [ ] 群成员禁言/解除禁言事件
- [ ] 群成员加入事件 (包括旧版受邀请入群)
- [ ] 其他OB11 API
- [x] 获取自身资料get_login_info
- [x] 获取好友、群组get_friend_list/get_group_list
- [x] 消息撤回
- [ ] 获取群组内群员资料get_group_member_list(调不通orz [https://github.com/chrononeko/bugtracker/issues/12]
- [x] 禁言/解禁群员
- [x] 全体禁言
- [ ] 获取群公告
- [ ] 禁言/解禁群员
- [ ] 全体禁言
- [ ] 获取历史消息

# 测试过的版本

- nonebot2 2.1.0
- nonebot-adapter-onebot 2.3.0
- nonebot-adapter-red 0.5.1
- nonebot2 2.1.0
- nonebot-adapter-onebot 2.3.0
- nonebot-adapter-red 0.5.1

由于本项目的特殊性,不保证在其他版本的nonebot/red适配器中也能工作。

Expand All @@ -42,6 +42,7 @@ With some magic ✨
1、创建一个bot.py

2、写入下列内容

```python
import nonebot
from nonebot import on_command
Expand Down Expand Up @@ -72,4 +73,3 @@ if __name__ == "__main__":
```

3、开润

107 changes: 92 additions & 15 deletions src/nonebot_adapter_onebot_pretender/v11/impl/red/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import json
from pathlib import Path
from base64 import b64decode
from urllib.parse import urlencode, urlunsplit
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Type, Union, Optional
from urllib.parse import urlencode, urlunsplit

from nonebot.adapters.red import Bot as RedBot
from nonebot.adapters.red import Message as RedMsg
from nonebot.adapters.red import event as red_event
from nonebot.adapters.red.api.model import ChatType
from nonebot.adapters.onebot.v11 import ActionFailed
from nonebot.adapters.red import Adapter as RedAdapter
from nonebot.adapters.red import MessageSegment as RedMS
from nonebot.adapters.onebot.v11 import Message as OB11Msg
from nonebot.utils import DataclassEncoder, logger_wrapper
from nonebot.adapters.onebot.v11 import MessageSegment as OB11MS
from nonebot.adapters.onebot.v11 import event as ob11_event
from nonebot.adapters.onebot.v11.event import Reply, Sender
from nonebot.adapters.onebot.v11 import MessageSegment as OB11MS
from nonebot.adapters.red import Adapter as RedAdapter
from nonebot.adapters.red import Bot as RedBot
from nonebot.adapters.red import Message as RedMsg
from nonebot.adapters.red import MessageSegment as RedMS
from nonebot.adapters.red import event as red_event
from nonebot.adapters.red.api.model import ChatType
from nonebot.adapters.red.message import ForwardNode, MediaMessageSegment
from nonebot.utils import DataclassEncoder, logger_wrapper

from ....webapi import red # noqa: F401
from ...factory import register_ob11_pretender
from ...pretender import OB11Pretender, event_handler, api_call_handler
from ....data.ob11_msg import OB11MsgModel, load_ob11_msg, save_ob11_msg
from ....webapi import red # noqa: F401

log = logger_wrapper("OneBot V11 Pretender (RedProtocol)")

Expand Down Expand Up @@ -293,8 +294,8 @@ async def _send_forward_msg(
ForwardNode(
uin=str(data.get("uin") or data.get("user_id")),
name=data.get("name")
or data.get("nickname")
or str(data.get("uin") or data.get("user_id")),
or data.get("nickname")
or str(data.get("uin") or data.get("user_id")),
group=0,
message=self.convert_outgoing_msg(
OB11Msg(OB11MS(**raw_seg) for raw_seg in data["content"])
Expand Down Expand Up @@ -364,7 +365,7 @@ async def get_group_list(self, bot: RedBot, **data: Dict) -> List:
async def get_group_member_list(
self, bot: RedBot, *, group_id: int, **data: Dict
) -> List:
members = await bot.get_members(group_id, 2**16 - 1)
members = await bot.get_members(group_id, 2 ** 16 - 1)
return [
{
"group_id": group_id,
Expand Down Expand Up @@ -406,6 +407,35 @@ async def get_msg(self, bot: RedBot, *, message_id: int, **data: Dict) -> Dict:
else:
raise ActionFailed(msg="消息不存在")

@api_call_handler()
async def set_group_ban(
self,
bot: RedBot,
*,
group_id: int,
user_id: int,
duration: int = 30 * 60,
**data: Dict,
) -> None:
if duration > 0:
await bot.mute_member(group_id, user_id, duration=duration)
else:
await bot.unmute_member(group_id, user_id)

@api_call_handler()
async def set_group_whole_ban(
self,
bot: RedBot,
*,
group_id: int,
enable: bool = True,
**data: Dict,
) -> None:
if enable:
await bot.mute_everyone(group_id)
else:
await bot.unmute_everyone(group_id)

@event_handler(red_event.GroupMessageEvent)
async def handle_group_message_event(
self, bot: RedBot, event: red_event.GroupMessageEvent
Expand Down Expand Up @@ -498,7 +528,7 @@ async def handle_private_message_event(
time=int(event.msgTime or "0"),
self_id=int(bot.self_id or "0"),
post_type="message",
sub_type="normal",
sub_type="friend",
user_id=int(event.senderUin or "0"),
message_id=int(event.msgId or "0"),
message=msg,
Expand All @@ -515,3 +545,50 @@ async def handle_private_message_event(
to_me=event.to_me,
reply=reply,
)

@event_handler(red_event.MemberAddEvent)
async def handle_member_add_event(
self, bot: RedBot, event: red_event.MemberAddEvent
) -> ob11_event.GroupIncreaseNoticeEvent:
return ob11_event.GroupIncreaseNoticeEvent(
time=int(event.msgTime or "0"),
self_id=int(bot.self_id or "0"),
post_type="notice",
notice_type="group_increase",
sub_type="approve",
user_id=int(event.memberUid or "0"),
group_id=int(event.peerUin or event.peerUid or "0"),
operator_id=int(event.operatorUid or "0"),
)

@event_handler(red_event.MemberMutedEvent)
async def handle_member_muted_event(
self, bot: RedBot, event: red_event.MemberMutedEvent
) -> ob11_event.GroupBanNoticeEvent:
return ob11_event.GroupBanNoticeEvent(
time=int(datetime.now().timestamp()),
self_id=int(bot.self_id or "0"),
post_type="notice",
notice_type="group_ban",
sub_type="ban",
user_id=int(event.member.uin or event.member.uid or "0"),
group_id=int(event.peerUin or event.peerUid or "0"),
operator_id=int(event.operator.uin or event.operator.uid or "0"),
duration=int(event.duration.total_seconds()),
)

@event_handler(red_event.MemberUnmuteEvent)
async def handle_member_unmuted_event(
self, bot: RedBot, event: red_event.MemberUnmuteEvent
) -> ob11_event.GroupBanNoticeEvent:
return ob11_event.GroupBanNoticeEvent(
time=int(datetime.now().timestamp()),
self_id=int(bot.self_id or "0"),
post_type="notice",
notice_type="group_ban",
sub_type="lift_ban",
user_id=int(event.member.uin or event.member.uid or "0"),
group_id=int(event.peerUin or event.peerUid or "0"),
operator_id=int(event.operator.uin or event.operator.uid or "0"),
duration=int(event.duration.total_seconds()),
)

0 comments on commit 02c7f4c

Please sign in to comment.