Skip to content

Commit

Permalink
refactor: match model changes to latest docs
Browse files Browse the repository at this point in the history
  • Loading branch information
eunwoo1104 committed Aug 27, 2023
1 parent 2e84a94 commit 0d9c988
Show file tree
Hide file tree
Showing 14 changed files with 551 additions and 20 deletions.
117 changes: 114 additions & 3 deletions dico/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
ApplicationCommandOption,
ApplicationCommandPermissions,
ApplicationCommandTypes,
ApplicationRoleConnection,
ApplicationRoleConnectionMetadata,
Attachment,
AuditLog,
AuditLogEvents,
Expand Down Expand Up @@ -47,6 +49,8 @@
ListThreadsResponse,
Message,
MessageReference,
Onboarding,
OnboardingMode,
Overwrite,
PermissionFlags,
PrivacyLevel,
Expand Down Expand Up @@ -122,6 +126,32 @@ def __init__(
application_id
)

# Application Role Connection Metadata

def request_application_role_connection_metadata_records(
self, application: Application.TYPING
) -> ApplicationRoleConnectionMetadata.RESPONSE_AS_LIST:
resp = self.http.request_application_role_connection_metadata_records(
int(application)
)
if isinstance(resp, list):
return [ApplicationRoleConnectionMetadata(x) for x in resp]
return wrap_to_async(
ApplicationRoleConnectionMetadata, None, resp, as_create=False
)

def update_application_role_connection_metadata_records(
self, application: Application.TYPING, data: List[dict]
) -> ApplicationRoleConnectionMetadata.RESPONSE_AS_LIST:
resp = self.http.update_application_role_connection_metadata_records(
int(application), data
)
if isinstance(resp, list):
return [ApplicationRoleConnectionMetadata(x) for x in resp]
return wrap_to_async(
ApplicationRoleConnectionMetadata, None, resp, as_create=False
)

# Audit Log

def request_guild_audit_log(
Expand Down Expand Up @@ -910,7 +940,8 @@ def start_thread(
message: Optional[Message.TYPING] = None,
*,
name: str,
auto_archive_duration: int,
auto_archive_duration: Optional[int] = None,
rate_limit_per_user: Optional[int] = EmptyObject,
thread_type: Union[ChannelTypes, int] = None,
invitable: bool = None,
reason: Optional[str] = None
Expand All @@ -925,15 +956,21 @@ def start_thread(
:param message: Message to create thread from.
:param str name: Name of the thread.
:param int auto_archive_duration: When to archive thread in minutes.
:param rate_limit_per_user:
:param thread_type: Type of thread.
:type thread_type: Union[ChannelTypes, int]
:param bool invitable: Whether this thread can be invitable.
:param Optional[str] reason: Reason of the action.
:return: :class:`~.Channel`
"""
channel = (
self.http.start_thread_with_message(
int(channel), int(message), name, auto_archive_duration, reason=reason
self.http.start_thread_from_message(
int(channel),
int(message),
name,
auto_archive_duration,
rate_limit_per_user,
reason=reason,
)
if message
else self.http.start_thread_without_message(
Expand All @@ -942,13 +979,38 @@ def start_thread(
auto_archive_duration,
int(thread_type),
invitable,
rate_limit_per_user,
reason=reason,
)
)
if isinstance(channel, dict):
channel = Channel.create(self, channel)
return wrap_to_async(Channel, self, channel)

def start_thread_in_forum_channel(
self,
channel: Channel.TYPING,
name: str,
message: dict,
auto_archive_duration: Optional[int] = None,
rate_limit_per_user: Optional[int] = EmptyObject,
applied_tags: Optional[List[Snowflake]] = None,
*,
reason: Optional[str] = None
) -> Channel.RESPONSE:
channel = self.http.start_thread_in_forum_channel(
int(channel),
name,
message,
auto_archive_duration,
rate_limit_per_user,
applied_tags,
reason=reason,
)
if isinstance(channel, dict):
channel = Channel.create(self, channel)
return wrap_to_async(Channel, self, channel)

def join_thread(self, channel: Channel.TYPING):
"""
Joins to thread.
Expand Down Expand Up @@ -2134,6 +2196,33 @@ def modify_guild_welcome_screen(
return WelcomeScreen(resp)
return wrap_to_async(WelcomeScreen, None, resp, as_create=False)

def request_guild_onboarding(self, guild: Guild.TYPING) -> Onboarding.RESPONSE:
resp = self.http.request_guild_onboarding(int(guild))
if isinstance(resp, dict):
return Onboarding(self, resp)
return wrap_to_async(Onboarding, self, resp, as_create=False)

def modify_guild_onboarding(
self,
guild: Guild.TYPING,
prompts: List[dict],
default_channels: List[Channel.TYPING],
enabled: bool,
mode: Union[OnboardingMode, int],
reason: Optional[str] = None,
) -> Onboarding.RESPONSE:
resp = self.http.modify_guild_onboarding(
int(guild),
prompts,
[str(int(x)) for x in default_channels],
enabled,
int(mode),
reason=reason,
)
if isinstance(resp, dict):
return Onboarding(self, resp)
return wrap_to_async(Onboarding, self, resp, as_create=False)

def modify_user_voice_state(
self,
guild: Guild.TYPING,
Expand Down Expand Up @@ -2852,6 +2941,28 @@ def request_user_connections(self) -> Connection.RESPONSE_AS_LIST:
return [Connection(self, x) for x in resp]
return wrap_to_async(Connection, self, resp)

def request_user_application_role_connections(
self, application: Application.TYPING
) -> ApplicationRoleConnection.RESPONSE:
resp = self.http.request_user_application_role_connections(int(application))
if isinstance(resp, dict):
return ApplicationRoleConnection(resp)
return wrap_to_async(ApplicationRoleConnection, None, resp, as_create=False)

def update_user_application_role_connections(
self,
application: Application.TYPING,
platform_name: str = EmptyObject,
platform_username: str = EmptyObject,
metadata: dict = EmptyObject,
) -> ApplicationRoleConnection.RESPONSE:
resp = self.http.update_user_application_role_connections(
int(application), platform_name, platform_username, metadata
)
if isinstance(resp, dict):
return ApplicationRoleConnection(resp)
return wrap_to_async(ApplicationRoleConnection, None, resp, as_create=False)

# Voice

def list_voice_regions(self) -> VoiceRegion.RESPONSE_AS_LIST:
Expand Down
114 changes: 109 additions & 5 deletions dico/base/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ def request(
"""
pass

# Application Role Connection Metadata

def request_application_role_connection_metadata_records(self, application_id):
return self.request(
f"/applications/{application_id}/role-connections/metadata", "GET"
)

def update_application_role_connection_metadata_records(
self, application_id, data: typing.List[dict]
):
return self.request(
f"/applications/{application_id}/role-connections/metadata", "PUT", data
)

# Audit Log Requests

def request_guild_audit_log(
Expand Down Expand Up @@ -716,12 +730,13 @@ def group_dm_remove_recipient(self, channel_id, user_id) -> RESPONSE:
"""
return self.request(f"/channels/{channel_id}/recipients/{user_id}", "DELETE")

def start_thread_with_message(
def start_thread_from_message(
self,
channel_id,
message_id,
name: str,
auto_archive_duration: int,
auto_archive_duration: int = None,
rate_limit_per_user: typing.Optional[int] = EmptyObject,
reason: str = None,
) -> RESPONSE:
"""
Expand All @@ -731,9 +746,14 @@ def start_thread_with_message(
:param message_id: ID of the message to create thread.
:param name: Name of the thread channel.
:param auto_archive_duration: Duration in minute to automatically close after recent activity.
:param rate_limit_per_user:
:param reason: Reason of the action.
"""
body = {"name": name, "auto_archive_duration": auto_archive_duration}
body = {"name": name}
if auto_archive_duration is not None:
body["auto_archive_duration"] = auto_archive_duration
if rate_limit_per_user is not EmptyObject:
body["rate_limit_per_user"] = rate_limit_per_user
return self.request(
f"/channels/{channel_id}/messages/{message_id}/threads",
"POST",
Expand All @@ -746,9 +766,10 @@ def start_thread_without_message(
self,
channel_id,
name: str,
auto_archive_duration: int,
auto_archive_duration: typing.Optional[int] = None,
thread_type: int = None,
invitable: bool = None,
rate_limit_per_user: typing.Optional[int] = EmptyObject,
reason: str = None,
) -> RESPONSE:
"""
Expand All @@ -759,9 +780,14 @@ def start_thread_without_message(
:param auto_archive_duration: Duration in minute to automatically close after recent activity.
:param thread_type: Type of the thread.
:param invitable: Whether this thread is invitable.
:param rate_limit_per_user:
:param reason: Reason of the action.
"""
body = {"name": name, "auto_archive_duration": auto_archive_duration}
body = {"name": name}
if auto_archive_duration is not None:
body["auto_archive_duration"] = auto_archive_duration
if rate_limit_per_user is not EmptyObject:
body["rate_limit_per_user"] = rate_limit_per_user
if thread_type is not None:
body["type"] = thread_type
if invitable is not None:
Expand All @@ -774,6 +800,31 @@ def start_thread_without_message(
reason_header=reason,
)

def start_thread_in_forum_channel(
self,
channel_id,
name: str,
message: dict,
auto_archive_duration: typing.Optional[int] = None,
rate_limit_per_user: typing.Optional[int] = EmptyObject,
applied_tags: typing.List[str] = None,
reason: str = None,
):
body = {"name": name, "message": message}
if auto_archive_duration is not None:
body["auto_archive_duration"] = auto_archive_duration
if rate_limit_per_user is not EmptyObject:
body["rate_limit_per_user"] = rate_limit_per_user
if applied_tags is not None:
body["applied_tags"] = applied_tags
return self.request(
f"/channels/{channel_id}/threads",
"POST",
body,
is_json=True,
reason_header=reason,
)

def join_thread(self, channel_id) -> RESPONSE:
"""
Sends join thread request.
Expand Down Expand Up @@ -1790,6 +1841,33 @@ def modify_guild_welcome_screen(
reason_header=reason,
)

def request_guild_onboarding(self, guild_id):
return self.request(f"/guilds/{guild_id}/onboarding", "GET")

def modify_guild_onboarding(
self,
guild_id,
prompts: list,
default_channel_ids: list,
enabled: bool,
mode: int,
reason: str = None,
):
body = {
"prompts": prompts,
"default_channel_ids": default_channel_ids,
"enabled": enabled,
"mode": mode,
"reason": reason,
}
return self.request(
f"/guilds/{guild_id}/onboarding",
"PUT",
body,
is_json=True,
reason_header=reason,
)

def modify_user_voice_state(
self,
guild_id,
Expand Down Expand Up @@ -2355,6 +2433,32 @@ def request_user_connections(self) -> RESPONSE:
"""
return self.request("/users/@me/connections", "GET")

def request_user_application_role_connections(self, application_id) -> RESPONSE:
return self.request(
f"/users/@me/connections/{application_id}/role-connection", "GET"
)

def update_user_application_role_connections(
self,
application_id,
platform_name: str = EmptyObject,
platform_username: str = EmptyObject,
metadata: dict = EmptyObject,
) -> RESPONSE:
body = {}
if platform_name is not EmptyObject:
body["platform_name"] = platform_name
if platform_username is not EmptyObject:
body["platform_username"] = platform_username
if metadata is not EmptyObject:
body["metadata"] = metadata
return self.request(
f"/users/@me/connections/{application_id}/role-connection",
"PUT",
body,
is_json=True,
)

# Voice Requests

def list_voice_regions(self) -> RESPONSE:
Expand Down
5 changes: 4 additions & 1 deletion dico/handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing
import traceback
import typing

from .model.event import *
from .utils import ensure_coro
Expand Down Expand Up @@ -33,6 +33,8 @@ def process_response(self, name: str, resp: dict):
# "APPLICATION_COMMAND_CREATE": ApplicationCommandCreate,
# "APPLICATION_COMMAND_UPDATE": ApplicationCommandUpdate,
# "APPLICATION_COMMAND_DELETE": ApplicationCommandDelete,
"APPLICATION_COMMAND_PERMISSIONS_UPDATE": ApplicationCommandPermissionsUpdate,
# todo: auto moderation rule events
"CHANNEL_CREATE": ChannelCreate,
"CHANNEL_UPDATE": ChannelUpdate,
"CHANNEL_DELETE": ChannelDelete,
Expand All @@ -46,6 +48,7 @@ def process_response(self, name: str, resp: dict):
"GUILD_CREATE": GuildCreate,
"GUILD_UPDATE": GuildUpdate,
"GUILD_DELETE": GuildDelete,
"GUILD_AUDIT_LOG_ENTRY_CREATE": GuildAuditLogEntryCreate,
"GUILD_BAN_ADD": GuildBanAdd,
"GUILD_BAN_REMOVE": GuildBanRemove,
"GUILD_EMOJIS_UPDATE": GuildEmojisUpdate,
Expand Down
1 change: 1 addition & 0 deletions dico/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .application import *
from .application_role_connection_metadata import *
from .audit_log import *
from .channel import *
from .emoji import *
Expand Down
Loading

0 comments on commit 0d9c988

Please sign in to comment.