Skip to content

Commit

Permalink
Fix replied_to being JSONDict
Browse files Browse the repository at this point in the history
Add Xp method and permission
  • Loading branch information
New-dev0 committed Jul 15, 2023
1 parent 987ee79 commit f2166ee
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/swibots/api/chat/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ async def get_replied_message(self) -> "Message":
return None
if self.replied_to is None:
self.replied_to = await self.app.get_message(self.replied_to_id)
else:
self.replied_to = self.app._bot_client.build_object(Message, self.replied_to)
return self.replied_to

### API Methods ###
Expand Down
24 changes: 21 additions & 3 deletions src/swibots/api/community/controllers/community_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional
from swibots.api.community.models import Channel, Community, Group

if TYPE_CHECKING:
Expand All @@ -14,10 +14,10 @@ class CommunityController:
def __init__(self, client: "CommunityClient"):
self.client = client

async def get_community(self, community_id: str = '', username: str = ''):
async def get_community(self, community_id: str = "", username: str = ""):
"""Get a community by id or username"""
if not (community_id or username):
raise ValueError('community_id or username must be provided.')
raise ValueError("community_id or username must be provided.")

if community_id and username:
raise ValueError("community_id and username can't be provided together.")
Expand All @@ -27,3 +27,21 @@ async def get_community(self, community_id: str = '', username: str = ''):
request_url = f"{BASE_PATH}?communityId={community_id}"
response = await self.client.get(request_url)
return self.client.build_object(Community, response.data.get("result"))

async def deduct_xp(
self,
community_id: str,
user_id: str,
xp: int = 0,
description: Optional[str] = None,
) -> bool:
response = await self.client.get(
f"{BASE_PATH}/deductXP",
data={
"communityId": community_id,
"description": description,
"userId": user_id,
"xp": xp,
},
)
return response.data.get("success", False)
28 changes: 28 additions & 0 deletions src/swibots/api/community/methods/deduct_xp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import swibots
from typing import Optional
from swibots.api.community.models import Channel


class DeductXP:
async def deduct_xp(
self: "swibots.ApiClient",
community_id: str,
user_id: str,
xp: int = 0,
description: Optional[str] = None,
) -> bool:
"""
Deducts XP from a user in a community.
Args:
community_id: The ID of the community.
user_id: The ID of the user.
xp: The amount of XP to deduct.
description: An optional description of the reason for the deduction.
Returns:
True if the deduction was successful, False otherwise.
"""
return await self.community_service.communities.deduct_xp(
community_id, user_id, xp, description
)
4 changes: 4 additions & 0 deletions src/swibots/api/community/models/rolepermission.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(
dm_permission: Optional[bool] = None,
pin_messages: Optional[bool] = None,
restrict_messaging: Optional[bool] = None,
can_deduct_xp: Optional[bool] = None,
role_id: Optional[int] = None,
created_at: Optional[str] = None,
updated_at: Optional[str] = None,
Expand All @@ -33,6 +34,7 @@ def __init__(
self.dm_permission = dm_permission
self.pin_messages = pin_messages
self.restrict_messaging = restrict_messaging
self.can_deduct_xp = can_deduct_xp
self.role_id = role_id
self.created_at = created_at
self.updated_at = updated_at
Expand All @@ -52,6 +54,7 @@ def to_json(self) -> JSONDict:
"deletePostsAndMessages": self.delete_messages,
"hasDMPermission": self.delete_messages,
"pinMessages": self.pin_messages,
"canDeductXPFromUser": self.can_deduct_xp,
"restrictMessaging": self.restrict_messaging,
},
}
Expand All @@ -70,6 +73,7 @@ def from_json(self, data: JSONDict) -> "RolePermission":
self.pin_messages = data.get("pinMessages")
self.change_info = data.get("changeCommunityInfo")
self.add_roles = data.get("addNewRoles")
self.can_deduct_xp = data.get("canDeductXPFromUser")
self.ban_users = data.get("banUsers")
self.dm_permission = data.get("hasDMPermission")
self.restrict_messaging = data.get("restrictMessaging")
Expand Down

0 comments on commit f2166ee

Please sign in to comment.