Skip to content

Commit 01dae97

Browse files
authored
feat: friend related APIs (#206)
* feat(friend): friend * feat(friend): friend * feat(friend): friend * feat(friend): friend * feat(friend): friend * feat(friend): friend
1 parent 3dbc217 commit 01dae97

File tree

5 files changed

+115
-4
lines changed

5 files changed

+115
-4
lines changed

khl/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
BadgeTypes,
1515
MessageFlagModes,
1616
GameTypes,
17+
FriendTypes
1718
)
1819
from .cert import Cert
1920
from .receiver import Receiver, WebhookReceiver, WebsocketReceiver
@@ -23,7 +24,7 @@
2324

2425
# concepts
2526
from .role import Role
26-
from .user import User, GuildUser
27+
from .user import User, GuildUser, Friend, FriendRequest
2728
from .intimacy import Intimacy
2829
from .channel import Channel, PublicTextChannel, PublicVoiceChannel, PrivateChannel, PublicChannel
2930
from .game import Game

khl/_types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,10 @@ class GameTypes(Enum):
153153
ALL = '0'
154154
USER_CREATED = '1'
155155
SYSTEM_CREATED = '2'
156+
157+
158+
class FriendTypes(Enum):
159+
"""types of friends"""
160+
REQUEST = "request"
161+
FRIEND = "friend"
162+
BLOCKED = "blocked"

khl/api.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ class Friend:
675675
def request(
676676
user_code,
677677
_from,
678-
guild_id,
678+
guild_id=None,
679679
):
680680
...
681681

@@ -693,3 +693,17 @@ def delete(
693693
user_id
694694
):
695695
...
696+
697+
@staticmethod
698+
@req('POST')
699+
def block(
700+
user_id
701+
):
702+
...
703+
704+
@staticmethod
705+
@req('POST')
706+
def unblock(
707+
user_id
708+
):
709+
...

khl/client.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .interface import AsyncRunnable
1515
from .message import RawMessage, Message, Event, PublicMessage, PrivateMessage
1616
from ._types import SoftwareTypes, MessageTypes, SlowModeTypes, GameTypes
17-
from .user import User
17+
from .user import User, Friend, FriendRequest
1818
from .util import unpack_id, unpack_value
1919

2020
log = logging.getLogger(__name__)
@@ -346,5 +346,20 @@ async def fetch_guild_boost(self,
346346
api.GuildBoost.history(guild_id=unpack_id(guild), start_time=start_time, end_time=end_time), **kwargs)
347347
return [GuildBoost(**item, _gate_=self.gate) for item in boost_list]
348348

349+
async def fetch_friends(self) -> List[Friend]:
350+
"""list friends who have been added to friend list"""
351+
friends = (await self.gate.exec_req(api.friend(type='friend')))['friend']
352+
return [Friend(_gate_=self.gate, user_id=i['friend_info']['id'], **i) for i in friends]
353+
354+
async def fetch_friend_requests(self) -> List[FriendRequest]:
355+
"""list friends requests received"""
356+
friends = (await self.gate.exec_req(api.friend(type='request')))['request']
357+
return [FriendRequest(_gate_=self.gate, user_id=i['friend_info']['id'], **i) for i in friends]
358+
359+
async def fetch_blocked_friends(self) -> List[Friend]:
360+
"""list friends who are blocked"""
361+
friends = (await self.gate.exec_req(api.friend(type='blocked')))['blocked']
362+
return [Friend(_gate_=self.gate, user_id=i['friend_info']['id'], **i) for i in friends]
363+
349364
async def start(self):
350365
await asyncio.gather(self.handle_pkg(), self.gate.run(self._pkg_queue))

khl/user.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
from typing import List, Union
33

44
from . import api
5+
from ._types import MessageTypes, FriendTypes
56
from .gateway import Requestable, Gateway
67
from .interface import LazyLoadable
78
from .intimacy import Intimacy
89
from .role import Role
9-
from ._types import MessageTypes
1010

1111

1212
class User(LazyLoadable, Requestable):
@@ -85,6 +85,14 @@ async def update_intimacy(self, score: int = 0, social_info: str = None, img_id:
8585
params['img_id'] = img_id
8686
return await self.gate.exec_req(api.Intimacy.update(**params))
8787

88+
async def add_friend(self):
89+
"""send friend request to the user"""
90+
await self.gate.exec_req(api.Friend.request(user_code=f'{self.username}#{self.identify_num}', _from=0))
91+
92+
async def block(self):
93+
"""block the user"""
94+
await self.gate.exec_req(api.Friend.block(user_id=self.id))
95+
8896

8997
class GuildUser(User):
9098
"""a user in guild
@@ -127,3 +135,69 @@ async def set_nickname(self, nickname: str):
127135
Set user's nickname
128136
"""
129137
await self.gate.exec_req(api.Guild.nickname(guild_id=self.guild_id, nickname=nickname, user_id=self.id))
138+
139+
async def add_friend(self):
140+
await self.gate.exec_req(
141+
api.Friend.request(user_code=f'{self.username}#{self.identify_num}', _from=2, guild_id=self.guild_id))
142+
143+
144+
class Friend:
145+
"""
146+
Friend with specific friend id and friend info
147+
"""
148+
149+
gate: Gateway
150+
151+
id: int
152+
user_id: str
153+
154+
_user: User
155+
_type: FriendTypes
156+
157+
def __init__(self, _gate_: Gateway, **kwargs):
158+
self.gate = _gate_
159+
self.id = kwargs.get('id')
160+
self.user_id = kwargs.get('user_id')
161+
self._type = kwargs.get('type')
162+
163+
async def fetch_user(self) -> User:
164+
"""get user"""
165+
if self._user is None:
166+
self._user = User(_gate_=self.gate, **(await self.gate.exec_req(api.User.view(user_id=self.user_id))))
167+
return self._user
168+
169+
async def delete(self):
170+
"""delete the friend"""
171+
await self.gate.exec_req(api.Friend.delete(user_id=self.user_id))
172+
173+
async def block(self):
174+
"""block the user"""
175+
await self.gate.exec_req(api.Friend.block(user_id=self.user_id))
176+
177+
async def unblock(self):
178+
"""unblock the blocked user"""
179+
await self.gate.exec_req(api.Friend.unblock(user_id=self.user_id))
180+
181+
@property
182+
def type(self) -> FriendTypes:
183+
"""the type of the friend"""
184+
return self._type
185+
186+
187+
class FriendRequest(Friend):
188+
"""
189+
Friend request with specific id and user info
190+
"""
191+
192+
async def accept(self):
193+
"""accept the friend request"""
194+
await self.gate.exec_req(api.Friend.handleRequest(id=self.id, accept=1))
195+
return Friend(_gate_=self.gate, user_id=self.user_id)
196+
197+
async def deny(self):
198+
"""deny the friend request"""
199+
await self.gate.exec_req(api.Friend.handleRequest(id=self.id, accept=0))
200+
201+
@property
202+
def type(self) -> FriendTypes:
203+
return FriendTypes.REQUEST

0 commit comments

Comments
 (0)