Skip to content

Commit 1835316

Browse files
committed
added linked roles support
1 parent 38f1166 commit 1835316

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

discordoauth2/__init__.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class PartialAccessToken():
44
def __init__(self, access_token, client) -> None:
5-
self.client: Client = client
5+
self.client = client
66
self.token = access_token
77

88
def fetch_identify(self):
@@ -74,9 +74,24 @@ def join_guild(self, guild_id, nick = None, role_ids = None, mute = False, deaf
7474
elif response.status_code == 429: raise exceptions.RateLimited(f"You are being Rate Limited. Retry after: {response.json()['retry_after']}", retry_after=response.json()['retry_after'])
7575
else:
7676
raise exceptions.HTTPException(f"Unexpected HTTP {response.status_code}")
77+
78+
def update_role_connections(self, platform_name=None, username=None, **metadata):
79+
response = requests.put(f"https://discord.com/api/v10/users/@me/applications/{self.client.id}/role-connection", headers={
80+
"authorization": f"Bearer {self.token}"}, json={
81+
"platform_name": platform_name,
82+
"platform_username": username,
83+
"metadata": metadata
84+
})
85+
86+
if response.ok:
87+
return response.json()
88+
elif response.status_code == 401: raise exceptions.Forbidden(f"this AccessToken does not have the nessasary scope.")
89+
elif response.status_code == 429: raise exceptions.RateLimited(f"You are being Rate Limited. Retry after: {response.json()['retry_after']}", retry_after=response.json()['retry_after'])
90+
else:
91+
raise exceptions.HTTPException(f"Unexpected HTTP {response.status_code}")
7792

7893
class AccessToken(PartialAccessToken):
79-
def __init__(self, data: dict, client) -> None:
94+
def __init__(self, data, client) -> None:
8095
super().__init__(data["access_token"], client)
8196

8297
self.expires = data.get("expires_in")
@@ -91,6 +106,10 @@ def __init__(self, id, secret, redirect, bot_token=None):
91106
self.redirect_url = redirect
92107
self.__secret = secret
93108
self.__bot_token = bot_token
109+
110+
def update_linked_roles_metadata(self, metadata):
111+
requests.put(f"https://discord.com/api/v10/applications/{self.id}/role-connections/metadata", headers={
112+
"authorization": f"Bot {self.__bot_token}"}, json=metadata)
94113

95114
def from_access_token(self, access_token):
96115
return PartialAccessToken(access_token, self)

0 commit comments

Comments
 (0)