DEV Community

Discussion on: Add a user to a guild with Discord OAuth2 in Python using Requests

Collapse
 
ahima profile image
Ahima86 • Edited

Hi, how we get code from user if the user are not yet on our guild ?
(the code we pass to exchange_code)
Thanks a lot

EDIT : its explain at the end but how we get the user id ? I juste have something like PSEUDO#1234
EDIT2 : this is my function to get user_id :
def get_user_id(self, access_token):
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.get('https://discord.com/api/v10/users/@me', headers=headers)
response.raise_for_status()
user = response.json()
return user['id']

Now i have error for assign_role:
def assign_role(self, access_token, user_id, role_id):
url = f"{DISCORD_API_ENDPOINT}/guilds/{self.server_id}/members/{user_id}/roles/{role_id}"
headers = {
"Authorization": f"Bearer {access_token}",
'Content-Type': 'application/json'
}
response = requests.put(url=url, headers=headers)
response.raise_for_status()