Skip to content

Commit

Permalink
feat: 迁移表情表态用户列表的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
DaiJiChen authored and veehou committed May 31, 2022
1 parent 3a7ae78 commit e64209b
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,5 +1,5 @@
# botpy
**botpy** 是基于[机器人开放平台API](https://bot.q.qq.com/wiki/develop/api/) 实现的机器人框架,目的提供一个易使用、开发效率高的开发框架
**botpy** 是基于[机器人开放平台API](https://bot.q.qq.com/wiki/develop/api/) 实现的机器人框架,目的提供一个易使用、开发效率高地开发框架

![PyPI](https://img.shields.io/pypi/v/qq-botpy)
[![BK Pipelines Status](https://api.bkdevops.qq.com/process/api/external/pipelines/projects/qq-guild-open/p-f17c900164974f5785c436b359876877/badge?X-DEVOPS-PROJECT-ID=qq-guild-open)](http://devops.oa.com/process/api-html/user/builds/projects/qq-guild-open/pipelines/p-f17c900164974f5785c436b359876877/latestFinished?X-DEVOPS-PROJECT-ID=qq-guild-open)
Expand Down
43 changes: 39 additions & 4 deletions botpy/api.py
Expand Up @@ -5,7 +5,7 @@

from .flags import Permission
from .http import BotHttp, Route
from .types import guild, user, channel, message, audio, announce, permission, schedule, emoji, pins_message
from .types import guild, user, channel, message, audio, announce, permission, schedule, emoji, pins_message, reaction


def _handle_message_parameters(
Expand Down Expand Up @@ -516,7 +516,6 @@ async def create_dms(self, guild_id: str, user_id: str) -> message.DmsPayload:
route = Route("POST", "/users/@me/dms")
return await self._http.request(route, json=payload)

# 私信消息
async def post_dms(
self,
guild_id: str,
Expand Down Expand Up @@ -957,7 +956,7 @@ async def delete_schedule(self, channel_id: str, schedule_id: str) -> str:
)
return await self._http.request(route)

# 异步表情表态接口
# 表情表态接口
async def put_reaction(self, channel_id: str, message_id: str, emoji_type: emoji.EmojiType, emoji_id: str) -> str:
"""
对一条消息进行表情表态。
Expand All @@ -982,7 +981,7 @@ async def put_reaction(self, channel_id: str, message_id: str, emoji_type: emoji
)
return await self._http.request(route)

async def delete_reaction(self, channel_id: str, message_id: str, emoji_type: int, emoji_id: str):
async def delete_reaction(self, channel_id: str, message_id: str, emoji_type: emoji.EmojiType, emoji_id: str):
"""
删除消息的表情表态。
Expand All @@ -1006,6 +1005,42 @@ async def delete_reaction(self, channel_id: str, message_id: str, emoji_type: in
)
return await self._http.request(route)

async def get_reaction_users(
self,
channel_id: str,
message_id: str,
emoji_type: emoji.EmojiType,
emoji_id: str,
cookie: str = None,
limit: int = 20,
) -> reaction.ReactionUsers:
"""
获取表情表态用户列表
Args:
channel_id (str): 消息所在子频道的 ID。
message_id (str): 要从中获取表情表态的消息的 ID。
emoji_type (emoji.EmojiType): 表情符号的类型。1: 系统表情, 2: emoji表情
emoji_id (str): 表情符号的 ID。
cookie (str): cookie 上次请求返回的cookie,第一次请求无需填写。
limit (int): 返回的最大用户数 (1-100)。. Defaults to 20
Returns:
对带有特定表情符号的消息做出反应的用户列表。
"""
route = Route(
"GET",
"/channels/{channel_id}/messages/{message_id}/reactions/{type}/{id}",
channel_id=channel_id,
message_id=message_id,
type=emoji_type,
id=emoji_id,
)
path = {"limit": limit}
if cookie:
path.update({"cookie": cookie})
return await self._http.request(route, params=path)

# 精华消息API
async def put_pin(self, channel_id: str, message_id: str) -> pins_message.PinsMessage:
"""
Expand Down
2 changes: 1 addition & 1 deletion botpy/gateway.py
Expand Up @@ -83,7 +83,7 @@ async def on_message(self, ws, message):
if event == "RESUMED":
# 心跳检查
self._connection.loop.create_task(self._send_heart(interval=30))
_log.info("[botpy]机器人重连成功")
_log.info("[botpy]机器人重连成功! ")

if event and opcode == self.WS_DISPATCH_EVENT:
event = msg["t"].lower()
Expand Down
14 changes: 3 additions & 11 deletions botpy/logging.py
Expand Up @@ -2,7 +2,6 @@

import logging
import os
import platform
import sys
from logging.handlers import TimedRotatingFileHandler

Expand Down Expand Up @@ -70,16 +69,9 @@ def get_logger(name=None):
if name is None:
name = "botpy"
log_file = log_path % {"name": name}
file_handler = None
if platform.system().lower() != "windows":
# save last 7 days log
file_handler = TimedRotatingFileHandler(
filename=log_file,
when="D",
backupCount=7,
)
else:
file_handler = logging.FileHandler(log_file, encoding="utf-8")
# save last 7 days log
file_handler = TimedRotatingFileHandler(filename=log_file, when="D", backupCount=7, encoding="utf-8")

if len(logger.handlers) == 0:
file_handler.setLevel(level=logging.DEBUG)
file_handler.setFormatter(formatter)
Expand Down
12 changes: 9 additions & 3 deletions botpy/types/reaction.py
@@ -1,7 +1,7 @@
from typing import TypedDict, Literal

from botpy.types.emoji import Emoji
from typing import TypedDict, Literal, List

from .emoji import Emoji
from .user import User

# 0: 消息 1: 帖子 2: 评论 3: 回复
ReactionTargetType = Literal[0, 1, 2, 3]
Expand All @@ -18,3 +18,9 @@ class Reaction(TypedDict):
channel_id: str
target: ReactionTarget
emoji: Emoji


class ReactionUsers(TypedDict):
users: List[User]
cookie: str # 分页参数,用于拉取下一页
is_end: bool # 是否已拉取完成到最后一页,true代表完成
2 changes: 1 addition & 1 deletion examples/qq-bot/README.md → examples/README.md
Expand Up @@ -20,7 +20,7 @@ examples/
## 环境安装

``` bash
pip install qq-bot
pip install qq-botpy
```

## 使用方法
Expand Down
50 changes: 50 additions & 0 deletions examples/demo_get_reaction_users.py
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os.path
from typing import List

import botpy
from botpy.message import Message
from botpy.types import reaction
from botpy.types.user import User
from botpy.utils import YamlUtil

test_config = YamlUtil.read(os.path.join(os.path.dirname(__file__), "config.yaml"))


class MyClient(botpy.Client):
async def on_at_message_create(self, message: Message):
users: List[User] = []
cookie = ""
while True:
reactionUsers: reaction.ReactionUsers = await self.api.get_reaction_users(
"2568610",
"088de19cbeb883e7e97110a2e39c0138d80d48acfc879406",
1,
"4",
cookie=cookie,
)

if not reactionUsers:
break

users.extend(reactionUsers["users"])

if reactionUsers["is_end"]:
break
else:
cookie = reactionUsers["cookie"]

print(len(users))
for user in users:
print(user["username"])


if __name__ == "__main__":
# 通过预设置的类型,设置需要监听的事件通道
# intents = botpy.Intents.none()
# intents.public_guild_messages=True
# 通过kwargs,设置需要监听的事件通道
intents = botpy.Intents(public_guild_messages=True)
client = MyClient(intents)
client.run(appid=test_config["appid"], token=test_config["token"])
6 changes: 5 additions & 1 deletion tests/test_api.py
Expand Up @@ -198,7 +198,7 @@ def test_get_schedules(self):
schedules = self.loop.run_until_complete(self.api.get_schedules(CHANNEL_SCHEDULE_ID))
self.assertEqual(None, schedules)

def test_delete_reaction(self):
def test_put_and_delete_reaction(self):
result = self.loop.run_until_complete(self.api.put_reaction(CHANNEL_ID, MESSAGE_ID, 1, "4"))
self.assertEqual("", result)

Expand All @@ -207,6 +207,10 @@ def test_delete_reaction(self):
result = self.loop.run_until_complete(self.api.delete_reaction(CHANNEL_ID, MESSAGE_ID, 1, "4"))
self.assertEqual("", result)

def test_get_reaction_users(self):
result = self.loop.run_until_complete(self.api.get_reaction_users(CHANNEL_ID, MESSAGE_ID, 1, "4"))
self.assertEqual(result["is_end"], True)

def test_put_pin(self):
result = self.loop.run_until_complete(self.api.put_pin(CHANNEL_ID, MESSAGE_ID))
self.assertIsNotNone(result)
Expand Down

0 comments on commit e64209b

Please sign in to comment.