From 11fcdbb9a55c8b83dac22df112c884ab9e23ad89 Mon Sep 17 00:00:00 2001 From: weibeu Date: Tue, 20 Apr 2021 14:43:37 +0530 Subject: [PATCH] Add an example demonstrating sending message to user after they authorize the application --- docs/introduction.rst | 7 +++++++ tests/test_app.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/docs/introduction.rst b/docs/introduction.rst index bc8482b..5355692 100644 --- a/docs/introduction.rst +++ b/docs/introduction.rst @@ -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")) diff --git a/tests/test_app.py b/tests/test_app.py index 08c6458..ff591ac 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -20,6 +20,13 @@ discord = DiscordOAuth2Session(app) HYPERLINK = '{}' +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)