Skip to content

Commit

Permalink
Add an example demonstrating sending message to user after they autho…
Browse files Browse the repository at this point in the history
…rize the application
  • Loading branch information
weibeu committed Apr 20, 2021
1 parent 1c81a95 commit 11fcdbb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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"))
Expand Down
11 changes: 11 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
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:
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 11fcdbb

Please sign in to comment.