Add an example demonstrating sending message to user after they authorize the application

This commit is contained in:
weibeu
2021-04-20 14:43:37 +05:30
parent 1c81a9516a
commit 11fcdbb9a5
2 changed files with 18 additions and 0 deletions

View File

@@ -65,6 +65,11 @@ in exchange for fetching user's details and display them on web page.
discord = DiscordOAuth2Session(app)
def welcome_user(user):
dm_channel = discord.bot_request("/users/@me/channels", "POST", json={"recipient_id": user.id})
return discord.bot_request(
f"/channels/{dm_channel['id']}/messages", "POST", json={"content": "Thanks for authorizing the app!"}
)
@app.route("/login/")
def login():
@@ -74,6 +79,8 @@ in exchange for fetching user's details and display them on web page.
@app.route("/callback/")
def callback():
discord.callback()
user = discord.fetch_user()
welcome_user(user)
return redirect(url_for(".me"))

View File

@@ -20,6 +20,13 @@ discord = DiscordOAuth2Session(app)
HYPERLINK = '<a href="{}">{}</a>'
def welcome_user(user):
dm_channel = discord.bot_request("/users/@me/channels", "POST", json={"recipient_id": user.id})
return discord.bot_request(
f"/channels/{dm_channel['id']}/messages", "POST", json={"content": "Thanks for authorizing the app!"}
)
@app.route("/")
def index():
if not discord.authorized:
@@ -62,6 +69,10 @@ def invite_oauth():
def callback():
data = discord.callback()
redirect_to = data.get("redirect", "/")
user = discord.fetch_user()
welcome_user(user)
return redirect(redirect_to)