Skip to content

Commit a8ea5e4

Browse files
SaucePlumGLGDLY
andauthored
fix: 修复删除频道成员API无法拉黑的问题 (#107)
* feat: 完善ws事件中Message数据的构建 * feat: 完善ws事件中User-Member数据的构建 * feat: 完善ws事件中Reaction、Audio、DirectMessage 以及 Forum(Thread类)数据的构建 feat: 去除EmbedField的value项(实际仅有name) * feat: 完善ws事件中监听文档并优化格式 refactor: 优化forum thread的事件数据名称 * fix: forum thread的事件数据名称更变后同步数据更改到其他文件 * refactor: 优化ext下的扩展工具 * refactor: 优化ext的convert_color模块 * refactor: 优化ext下的扩展工具 * refactor: 优化ext下的扩展工具 * docs: 修改一处表述不正确的地方 * feat: 修改一处表述不正确的地方 * fix: 帖子api中route方式错误的问题 * feat: 补齐频道成员中的删除频道成员api * feat: 更新文档并新增定时框架 * fix: 修复Member构建的部分字段取值错误的问题 * docs: 更新botpy文档 * fix: 修复删除频道成员API无法拉黑的问题 * fix: rst文件回滚 * feat: 为http请求成功添加trace_id Co-authored-by: GLGDLY <[email protected]>
1 parent 3dbb62a commit a8ea5e4

File tree

8 files changed

+50
-148
lines changed

8 files changed

+50
-148
lines changed

README.md

Lines changed: 24 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
# botpy
1+
<div align="center">
22

3-
**botpy** 是基于[机器人开放平台API](https://bot.q.qq.com/wiki/develop/api/) 实现的机器人框架,目的提供一个易使用、开发效率高的开发框架。
3+
![botpy](https://socialify.git.ci/tencent-connect/botpy/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&language=1&logo=https%3A%2F%2Fgithub.com%2Ftencent-connect%2Fbot-docs%2Fblob%2Fmain%2Fdocs%2F.vuepress%2Fpublic%2Ffavicon-64px.png%3Fraw%3Dtrue&owner=1&pattern=Circuit%20Board&pulls=1&stargazers=1&theme=Light)
44

5+
[![Language](https://img.shields.io/badge/language-python-green.svg?style=plastic)](https://www.python.org/)
6+
[![License](https://img.shields.io/badge/license-MIT-orange.svg?style=plastic)](https://github.com/tencent-connect/botpy/blob/master/LICENSE)
7+
![Python](https://img.shields.io/badge/python-3.8+-blue)
58
![PyPI](https://img.shields.io/pypi/v/qq-botpy)
69
[![BK Pipelines Status](https://api.bkdevops.qq.com/process/api/external/pipelines/projects/qq-guild-open/p-713959939bdc4adca0eea2d4420eef4b/badge?X-DEVOPS-PROJECT-ID=qq-guild-open)](https://devops.woa.com/process/api-html/user/builds/projects/qq-guild-open/pipelines/p-713959939bdc4adca0eea2d4420eef4b/latestFinished?X-DEVOPS-PROJECT-ID=qq-guild-open)
710

11+
_✨ 基于 [机器人开放平台API](https://bot.q.qq.com/wiki/develop/api/) 实现的机器人框架 ✨_
12+
13+
_✨ 为开发者提供一个易使用、开发效率高的开发框架 ✨_
14+
15+
[文档](https://bot.q.qq.com/wiki/develop/pythonsdk/)
16+
·
17+
[下载](https://github.com/tencent-connect/botpy/tags)
18+
·
19+
[安装](https://bot.q.qq.com/wiki/develop/pythonsdk/#sdk-安装)
20+
21+
</div>
22+
823
## 准备工作
924

1025
### 安装
@@ -43,12 +58,9 @@ import botpy
4358

4459
```python
4560
import botpy
46-
from botpy.types.message import Message
61+
from botpy.message import Message
4762

4863
class MyClient(botpy.Client):
49-
async def on_ready(self):
50-
print(f"robot 「{self.robot.name}」 on_ready!")
51-
5264
async def on_at_message_create(self, message: Message):
5365
await message.reply(content=f"机器人{self.robot.name}收到你的@消息了: {message.content}")
5466
```
@@ -61,7 +73,7 @@ class MyClient(botpy.Client):
6173

6274
```python
6375
import botpy
64-
from botpy.types.message import Message
76+
from botpy.message import Message
6577

6678
class MyClient(botpy.Client):
6779
async def on_at_message_create(self, message: Message):
@@ -89,7 +101,7 @@ intents.public_guild_messages=True
89101

90102
```python
91103
import botpy
92-
from botpy.types.message import Message
104+
from botpy.message import Message
93105

94106
class MyClient(botpy.Client):
95107
async def on_at_message_create(self, message: Message):
@@ -122,125 +134,6 @@ class MyClient(botpy.Client):
122134
├── demo_recall.py # 机器人消息撤回示例
123135
├── demo_schedule.py # 机器人日程相关示例
124136

125-
## 日志打印
126-
127-
基于自带的 logging 模块封装的日志模块,提供了日志写入以及美化了打印格式,并支持调整打印级别(默认打印级别为 `INFO`)。
128-
129-
### 使用方法
130-
131-
引用模块,并获取 `logger` 实例:
132-
133-
```python
134-
from botpy import logging
135-
136-
logger = logging.get_logger()
137-
```
138-
139-
或者通过`botpy.logger`也可以获取logger对象
140-
141-
然后就可以愉快地使用 logger 进行打印。例如:
142-
143-
```python
144-
logger.info("hello world!")
145-
```
146-
147-
### 日志设置
148-
149-
SDK的日志设置集成在`bot.Client`的实例化阶段,也可通过[`logging.configure_logging`](botpy/logging.py)修改(均为可选)
150-
151-
```python
152-
import botpy
153-
154-
# 示例,非默认值
155-
botpy.Client(
156-
log_level=10,
157-
log_format="new format",
158-
bot_log=None,
159-
ext_handlers=False,
160-
log_config="log_config.json"
161-
)
162-
163-
```
164-
165-
### log_level
166-
167-
日志级别,默认为`INFO`
168-
169-
命令行启动py可增加参数`-d``--debug`快捷打开debug日志
170-
171-
```bash
172-
python3 demo_at_reply.py -d
173-
```
174-
175-
几个可选取值(参考了[logging模块的取值](https://docs.python.org/3/library/logging.html#levels)):
176-
177-
| Level | 取值 |
178-
| -------- | --- |
179-
| CRITICAL | 50 |
180-
| ERROR | 40 |
181-
| WARNING | 30 |
182-
| INFO | 20 |
183-
| DEBUG | 10 |
184-
| NOTSET | 0 |
185-
186-
### log_format
187-
188-
日志控制台输出格式,默认为 `"\033[1;33m[%(levelname)s]\t(%(filename)s:%(lineno)s)%(funcName)s\t\033[0m%(message)s"`
189-
190-
### bot_log
191-
192-
是否启用`botpy`日志,默认为`True`
193-
194-
`True` 启用
195-
`None` 禁用 拓展
196-
`False` 禁用 拓展+控制台输出
197-
198-
### ext_handlers
199-
200-
日志Handler拓展,为`True`使用默认拓展,`False`不添加拓展,可用list添加多个拓展。默认为`True`
201-
202-
[默认拓展](./botpy/logging.py)
203-
204-
```python
205-
import os
206-
import logging
207-
from logging.handlers import TimedRotatingFileHandler
208-
209-
DEFAULT_FILE_HANDLER = {
210-
# 要实例化的Handler
211-
"handler": TimedRotatingFileHandler,
212-
# 可选 Default to DEFAULT_FILE_FORMAT
213-
"format": "%(asctime)s\t[%(levelname)s]\t(%(filename)s:%(lineno)s)%(funcName)s\t%(message)s",
214-
# 可选 Default to DEBUG
215-
"level": logging.DEBUG,
216-
# 以下是Handler相关参数
217-
"when": "D",
218-
"backupCount": 7,
219-
"encoding": "utf-8",
220-
# *特殊* 对于filename参数,其中如有 %(name)s 会在实例化阶段填入相应的日志name
221-
"filename": os.path.join(os.getcwd(), "%(name)s.log"),
222-
}
223-
```
224-
225-
#### 修改默认拓展
226-
227-
```python
228-
import os
229-
import botpy
230-
from botpy.logging import DEFAULT_FILE_HANDLER
231-
232-
# 修改日志路径
233-
DEFAULT_FILE_HANDLER["filename"] = os.path.join(os.getcwd(), "log", "%(name)s.log")
234-
# 修改日志格式
235-
DEFAULT_FILE_HANDLER["format"] = "new format"
236-
237-
botpy.Client(ext_handlers=DEFAULT_FILE_HANDLER)
238-
```
239-
240-
### log_config
241-
242-
该参数将传入`logging.config.dictConfig`(内置logging而非botpy.logging),如果为.json/.yaml文件路径将从文件中读取配置,无默认值
243-
244137
# 参与开发
245138

246139
## 环境配置
@@ -271,9 +164,11 @@ pytest
271164

272165
## 致谢
273166

274-
感谢参与内测、开发和提出宝贵意见的开发者们(排名不分先后)
167+
感谢感谢以下开发者对 `botpy` 作出的贡献
275168

276-
[小念](https://github.com/ReadSmall), [Neutron](https://github.com/Huang1220), [晚柒载](https://github.com/wqzai)
169+
<a href="https://github.com/tencent-connect/botpy/graphs/contributors">
170+
<img src="https://contrib.rocks/image?repo=tencent-connect/botpy" />
171+
</a>
277172

278173
# 加入官方社区
279174

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ botpy
4444

4545
..
4646
47-
原机器人的老版本\ ``qq-bot``\ 仍然可以使用,但新接口的支持上会逐渐暂停,此次升级不会影响线上使用的机器人
47+
原机器人的老版本\ ``qq-bot``\ 仍然可以使用,但新接口的支持上会逐渐暂停,此次升级不会影响线上使用的机器人
4848

4949

5050
使用方式
@@ -56,7 +56,7 @@ botpy
5656
步骤1
5757
~~~~~
5858

59-
通过继承实现\ ``bot.Client``\ , 实现自己的机器人Client
59+
通过继承实现\ ``bot.Client``\ , 实现自己的机器人Client
6060

6161
步骤2
6262
~~~~~
@@ -93,7 +93,7 @@ botpy
9393
async def on_at_message_create(self, message: Message):
9494
await self.api.post_message(channel_id=message.channel_id, content="content")
9595
96-
intents = botpy.Intents(public_guild_messages=True)
96+
intents = botpy.Intents(public_guild_messages=True)
9797
client = MyClient(intents=intents)
9898
client.run(appid="12345", token="xxxx")
9999
@@ -126,7 +126,7 @@ botpy
126126
示例机器人
127127
----------
128128

129-
`\ ``examples`` <./examples/>`_ 目录下存放示例机器人,具体使用可参考\ `\ ``Readme.md`` <./examples/README.md>`_
129+
`\ ``examples`` <./examples/>`_ 目录下存放示例机器人,具体使用可参考\ `\ ``Readme.md`` <./examples/README.md>`_
130130

131131
.. code-block::
132132

botpy/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ async def get_delete_member(
216216
# 注:消息撤回时间范围仅支持固定的天数:3,7,15,30。 特殊的时间范围:-1: 撤回全部消息。默认值为0不撤回任何消息。
217217
if delete_history_msg_days not in (3, 7, 15, 30, 0, -1):
218218
delete_history_msg_days = 0
219-
params = {'add_blacklist': str(add_blacklist).lower(), 'delete_history_msg_days': delete_history_msg_days}
219+
payload = {"add_blacklist": add_blacklist, "delete_history_msg_days": delete_history_msg_days}
220220
route = Route(
221221
"DELETE",
222222
"/guilds/{guild_id}/members/{user_id}",
223223
guild_id=guild_id,
224224
user_id=user_id,
225225
)
226-
return await self._http.request(route, params=params)
226+
return await self._http.request(route, json=payload)
227227

228228
async def get_guild_members(self, guild_id: str, after: str = "0", limit: int = 1) -> List[user.Member]:
229229
"""
@@ -268,7 +268,7 @@ async def get_voice_members(self, channel_id: str) -> List[user.Member]:
268268
# 子频道相关接口
269269
async def get_channel(self, channel_id: str) -> channel.ChannelPayload:
270270
"""
271-
它获取频道信息。
271+
获取频道信息
272272
273273
Args:
274274
channel_id (str): 子频道 ID。
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from botpy import logging
4+
5+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
6+
7+
_log = logging.get_logger()
8+
9+
scheduler = AsyncIOScheduler()
10+
scheduler.configure({"apscheduler.timezone": "Asia/Shanghai"})
11+
12+
scheduler.start()
13+
_log.debug("[加载插件] APScheduler 定时任务")

botpy/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def _handle_response(url, response: ClientResponse) -> Union[Dict[str, Any
2828
except (KeyError, JSONDecodeError):
2929
data = None
3030
if response.status in HTTP_OK_STATUS:
31-
_log.debug(f"[botpy] 请求成功, 请求连接: {url}, 返回内容: {data}")
31+
_log.debug(f"[botpy] 请求成功, 请求连接: {url}, 返回内容: {data}, trace_id:{response.headers.get(X_TPS_TRACE_ID)}")
3232
return data
3333
else:
3434
_log.error(

botpy/user.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@
33

44

55
class Member:
6-
__slots__ = (
7-
"_api",
8-
"_ctx",
9-
"user",
10-
"nick",
11-
"roles",
12-
"joined_at",
13-
"event_id")
6+
__slots__ = ("_api", "_ctx", "user", "nick", "roles", "joined_at", "event_id")
147

158
def __init__(self, api: BotAPI, ctx: gateway.WsContext, data: user.GuildMemberPayload):
169
self._api = api
@@ -23,7 +16,7 @@ def __init__(self, api: BotAPI, ctx: gateway.WsContext, data: user.GuildMemberPa
2316

2417
class _User:
2518
def __init__(self, data):
26-
self.id = data.get("user")
19+
self.id = data.get("id")
2720
self.username = data.get("username")
2821
self.avatar = data.get("avatar")
2922
self.bot = data.get("bot")

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pre-commit
22
PyYAML
3-
aiohttp>=3.7.4,<4
3+
aiohttp>=3.7.4,<4
4+
APScheduler

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# 执照
1818
license="Tencent",
1919
# 安装依赖
20-
install_requires=["aiohttp>=3.7.4,<4", "PyYAML"],
20+
install_requires=["aiohttp>=3.7.4,<4", "PyYAML", "APScheduler"],
2121
# 分类
2222
classifiers=[
2323
# 发展时期,常见的如下

0 commit comments

Comments
 (0)