-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf853bc
commit cbb6db0
Showing
6 changed files
with
198 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules/ | ||
|
||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# backend | ||
The backend for DisGitHook which handles settings & webhooks | ||
# DisGitHook Backend | ||
|
||
This repository contains the backend code for the DisGitHook project. | ||
|
||
It contains the API for the website and the code for handling webhooks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { botId, botSecret, userAgent } = require("../config.json") | ||
|
||
const userCache = {} | ||
module.exports.getUser = async token => { | ||
if (userCache[token]) return userCache[token] | ||
const res = await fetch("https://discord.com/api/v10/users/@me", { | ||
headers: { | ||
"User-Agent": userAgent, | ||
Authorization: "Bearer " + token, | ||
Accept: "application/json" | ||
} | ||
}) | ||
if (!res.ok) return new Error("Couldnt get user data, failed with " + res.status + " " + res.statusText) | ||
const json = await res.json() | ||
userCache[token] = json | ||
setTimeout(() => delete userCache[token], 1000 * 60 * 10) | ||
return json | ||
} | ||
|
||
const guildCache = {} | ||
module.exports.getUserGuilds = async token => { | ||
if (guildCache[token]) return guildCache[token] | ||
const res = await fetch("https://discord.com/api/v10/users/@me/guilds", { | ||
headers: { | ||
"User-Agent": userAgent, | ||
Authorization: "Bearer " + token, | ||
Accept: "application/json" | ||
} | ||
}) | ||
if (!res.ok) { | ||
console.log("Couldnt get guild data, failed with " + res.status + " " + res.statusText) | ||
return new Error("Couldnt get guild data, failed with " + res.status + " " + res.statusText) | ||
} | ||
const json = await res.json() | ||
guildCache[token] = json | ||
setTimeout(() => delete guildCache[token], 1000 * 60 * 10) | ||
return json | ||
} | ||
|
||
module.exports.getAccessToken = async (userId, tokens, bot) => { | ||
if (Date.now() > tokens.expires_at) { | ||
const body = new URLSearchParams({ | ||
client_id: botId, | ||
client_secret: botSecret, | ||
grant_type: "refresh_token", | ||
refresh_token: tokens.refresh_token | ||
}) | ||
const res = await fetch("https://discord.com/api/v10/oauth2/token", { | ||
method: "POST", | ||
body, | ||
headers: { | ||
"User-Agent": userAgent, | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
Accept: "application/json" | ||
} | ||
}) | ||
if (res.ok) { | ||
const newtokens = await res.json() | ||
newtokens.expires_at = Date.now() + newtokens.expires_in * 1000 | ||
newtokens.type = tokens.type | ||
if (tokens.username) newtokens.username = tokens.username | ||
delete newtokens.expires_in | ||
|
||
// TODO: Store "newtokens" in the database | ||
return newtokens.access_token | ||
} else { | ||
console.log("Error refreshing access token: " + res.status + " " + res.statusText) | ||
const json = await res.json() | ||
console.log(json) | ||
} | ||
} | ||
return tokens.access_token | ||
} |