mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-08 19:39:35 +02:00
Add an example demonstrating sending message to user after they authorize the application
This commit is contained in:
@@ -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"))
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user