Skip to content

Commit a4e2cc4

Browse files
committed
updated examples and the fixed the join guild bug for this branch too
1 parent e9b6686 commit a4e2cc4

File tree

5 files changed

+58
-15
lines changed

5 files changed

+58
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
main.py
2-
rewrite.py
2+
rewrite.py
3+
discordoauth2/__pycache__/

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,38 @@ I've finally published the library to PyPi! So now you can use pip.
1313
pip install discord-oauth2.py
1414
```
1515
### Example With Flask
16-
Don't forget to replace all the client information on line 20 and 21 with your application's own information. You can leave bot token empty if your not adding members to guilds.
16+
Don't forget to replace all the client information with your application's own information. You can leave bot token empty if your not adding members to guilds.
1717
```py
1818
import discordoauth2
1919
from flask import Flask, request
2020

2121
client = discordoauth2.Client(849930878276993044, secret="very-secret-code",
22-
redirect="https://findingfakeurlsisprettyhard.tv/oauth2", bot_token="bot-token-only-required-for-guild-joining")
22+
redirect="https://findingfakeurlsisprettyhard.tv/oauth2", bot_token="bot-token-only-required-for-guild-joining-or-updating-linked-roles-metadata")
2323
app = Flask(__name__)
2424

25+
client.update_linked_roles_metadata([
26+
{
27+
"type": 2,
28+
"key": "level",
29+
"name": "Level",
30+
"description": "The level the user is on"
31+
},
32+
{
33+
"type": 7,
34+
"key": "supporter",
35+
"name": "Supporter",
36+
"description": "Spent money to help the game"
37+
}
38+
])
39+
2540
@app.route("/oauth2")
2641
def oauth2():
2742
code = request.args.get("code")
2843

2944
access = client.exchange_code(code)
3045

46+
access.update_metadata("Platform Name", "Username", level=69, supporter=True)
47+
3148
identify = access.fetch_identify()
3249
connections = access.fetch_connections()
3350
guilds = access.fetch_guilds()

discordoauth2/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def fetch_guild_member(self, guild_id):
5858
else:
5959
raise exceptions.HTTPException(f"Unexpected HTTP {response.status_code}")
6060

61-
def join_guild(self, guild_id, nick = None, role_ids = None, mute = False, deaf = False):
62-
response = requests.put(f"https://discord.com/api/v10/guilds/{guild_id}/members/621878678405775379", headers={
61+
def join_guild(self, guild_id, user_id, nick = None, role_ids = None, mute = False, deaf = False):
62+
response = requests.put(f"https://discord.com/api/v10/guilds/{guild_id}/members/{user_id}", headers={
6363
"authorization": f"Bot {self.client._Client__bot_token}"
6464
}, json={
6565
"access_token": self.token,
-8.44 KB
Binary file not shown.

example.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
from flask import Flask, redirect, request
2-
from discordoauth2 import discordOauth2
2+
import discordoauth2
33
import os
44

55
app = Flask('Discord OAuth2 Example')
6-
client = discordOauth2(client=your-client-id, secret="your-client-secret",
6+
client = discordoauth2.Client(client=your-client-id, secret="your-client-secret",
77
redirect="your-redirect-url", token="your-bot-token (optional)")
88
# Replace your-client-id with your application's client id, replace your-client-secret with your client secret and replace your-redirect-url with the url that discord will redirect users to once they complete OAuth2.
9-
# If you want to add users to a guild, insert a bot token with CREATE_INSTANT_INVITE permissions in the guilds you want to add users to.
9+
# If you want to updated linked roles metadata or add users to a guild, insert a bot token with CREATE_INSTANT_INVITE permissions in the guilds you want to add users to.
10+
11+
client.update_linked_roles_metadata([
12+
{
13+
"type": 2,
14+
"key": "level",
15+
"name": "Level",
16+
"description": "The level the user is on"
17+
},
18+
{
19+
"type": 2,
20+
"key": "wins",
21+
"name": "Wins",
22+
"description": "The number of times the user has won"
23+
},
24+
{
25+
"type": 7,
26+
"key": "supporter",
27+
"name": "Supporter",
28+
"description": "Spent money to help the game"
29+
}
30+
])
1031

1132
@app.route('/')
1233
def main():
@@ -15,19 +36,23 @@ def main():
1536

1637
@app.route('/oauth2')
1738
def oauth():
18-
tokenObject = client.exchange_code(token=request.args.get('code'))
19-
print("refresh token: "+tokenObject.refresh_token)
39+
access_token = client.exchange_code(token=request.args.get('code'))
40+
print("refresh token: "+access_token.refresh)
2041

2142
# returns basic data about the user, including username, avatar and badges, if the email scope was parsed, it will also return their email.
22-
identify = tokenObject.access.identify()
43+
identify = access_token.fetch_identify()
2344
# returns visible and hidden connections such as GitHub, YouTube or Twitter.
24-
connections = tokenObject.access.connections()
45+
connections = access_token.fetch_connections()
2546
# returns a list of guilds that the user is in
26-
guilds = tokenObject.access.guilds()
47+
guilds = access_token.fetch_guilds()
2748
# returns a member object for the provided guild
28-
guilds_member = tokenObject.access.guilds_member(guilds[0]["id"])
49+
guilds_member = access_token.fetch_guild_member(guilds[0]["id"])
2950
# makes a user join a guild, bot token provided must have CREATE_INSTANT_INVITE in that guild
30-
tokenObject.access.guilds_join(guild-id-here)
51+
access_token.join_guild(guild-id-here, identify["id"])
52+
# this update's the user's metadata for linked roles.
53+
access_token.update_metadata("Platform Name", "Username", level=69, wins=420, supporter=True)
54+
# when you're done with the token, you can revoke it: note this revokes both the access token and refresh token.
55+
access_token.revoke()
3156

3257
return f"{identify}<br><br>{connections}<br><br>{guilds}<br><br>guild data for the first guild: {guilds_member}"
3358

0 commit comments

Comments
 (0)