Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit fcfeda3

Browse files
committed
switch to js :3
1 parent 9c560d9 commit fcfeda3

File tree

14 files changed

+204
-2736
lines changed

14 files changed

+204
-2736
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
node_modules/
2-
/index.js
32

43
dist
54

package.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@
22
"name": "prism-linked-roles",
33
"version": "1.0.0",
44
"license": "GPL-3.0",
5-
"main": "server.ts",
5+
"main": "server.js",
6+
"type": "module",
67
"scripts": {
7-
"dev": "NODE_ENV=development tsx watch src/index.ts",
8-
"start": "tsx src/server.ts",
9-
"reupload": "tsx src/_reupload.ts",
8+
"dev": "NODE_ENV=development tsx watch src/index.js",
9+
"start": "tsx src/server.js",
10+
"reupload": "tsx src/_reupload.js",
1011
"lint": "eslint .",
1112
"preinstall": "npx only-allow yarn"
1213
},
1314
"dependencies": {
14-
"@types/cookie-parser": "^1.4.3",
15-
"@types/express": "^4.17.17",
16-
"@types/pg": "^8.6.6",
1715
"cookie-parser": "^1.4.6",
1816
"discord-interactions": "^3.3.0",
1917
"express": "^4.18.2",
2018
"node-fetch": "^3.3.1"
2119
},
2220
"devDependencies": {
23-
"@types/node": "^18.15.3",
24-
"@typescript-eslint/eslint-plugin": "5.55.0",
25-
"@typescript-eslint/parser": "5.55.0",
2621
"dotenv": "16.0.3",
2722
"esbuild": "0.17.11",
2823
"eslint": "8.36.0",
2924
"gray-matter": "4.0.3",
30-
"prettier": "2.8.4",
31-
"typescript": "4.9.5"
25+
"prettier": "2.8.4"
3226
}
3327
}

src/commands.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/common.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as dotenv from 'dotenv'
2+
3+
/**
4+
* Load environment variables from a .env file, if it exists.
5+
*/
6+
7+
dotenv.config()
8+
9+
const config = {
10+
DISCORD_TOKEN: process.env.DISCORD_TOKEN,
11+
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
12+
DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
13+
DISCORD_REDIRECT_URI: process.env.DISCORD_REDIRECT_URI,
14+
COOKIE_SECRET: process.env.COOKIE_SECRET,
15+
};
16+
17+
export default config;

src/discord.ts renamed to src/discord.js

Lines changed: 21 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,6 @@ import * as storage from './storage.js';
77
* Code specific to communicating with the Discord API.
88
*/
99

10-
export interface OAuth2TokenResponse {
11-
access_token: string;
12-
expires_at: number;
13-
expires_in: number;
14-
refresh_token: string;
15-
scope: string;
16-
token_type: string;
17-
}
18-
19-
export interface OAuth2UserInfo {
20-
application: {
21-
id: string;
22-
name: string;
23-
icon: string | null;
24-
description: string;
25-
summary: string;
26-
type: string | null;
27-
hook: boolean;
28-
bot_public: boolean;
29-
bot_require_code_grant: boolean;
30-
verify_key: string;
31-
flags: number;
32-
};
33-
scopes: string[];
34-
expires: string;
35-
user: {
36-
id: string;
37-
username: string;
38-
avatar: string;
39-
avatar_decoration: string | null;
40-
discriminator: string;
41-
public_flags: number;
42-
};
43-
}
44-
4510
/**
4611
* The following methods all facilitate OAuth2 communication with Discord.
4712
* See https://discord.com/developers/docs/topics/oauth2 for more details.
@@ -55,8 +20,8 @@ export function getOAuthUrl() {
5520
const state = crypto.randomUUID();
5621

5722
const url = new URL('https://discord.com/api/oauth2/authorize');
58-
url.searchParams.set('client_id', process.env.DISCORD_TOKEN!);
59-
url.searchParams.set('redirect_uri', process.env.DISCORD_REDIRECT_URI!);
23+
url.searchParams.set('client_id', process.env.DISCORD_CLIENT_ID);
24+
url.searchParams.set('redirect_uri', process.env.DISCORD_REDIRECT_URI);
6025
url.searchParams.set('response_type', 'code');
6126
url.searchParams.set('state', state);
6227
url.searchParams.set('scope', 'role_connections.write identify');
@@ -68,16 +33,14 @@ export function getOAuthUrl() {
6833
* Given an OAuth2 code from the scope approval page, make a request to Discord's
6934
* OAuth2 service to retrieve an access token, refresh token, and expiration.
7035
*/
71-
export async function getOAuthTokens(
72-
code: string
73-
): Promise<OAuth2TokenResponse> {
36+
export async function getOAuthTokens(code) {
7437
const url = 'https://discord.com/api/v10/oauth2/token';
7538
const body = new URLSearchParams({
76-
client_id: process.env.DISCORD_CLIENT_ID!,
77-
client_secret: process.env.DISCORD_CLIENT_SECRET!,
39+
client_id: process.env.DISCORD_CLIENT_ID,
40+
client_secret: process.env.DISCORD_CLIENT_SECRET,
7841
grant_type: 'authorization_code',
7942
code,
80-
redirect_uri: process.env.DISCORD_REDIRECT_URI!,
43+
redirect_uri: process.env.DISCORD_REDIRECT_URI,
8144
});
8245

8346
const response = await fetch(url, {
@@ -88,31 +51,26 @@ export async function getOAuthTokens(
8851
},
8952
});
9053
if (response.ok) {
91-
const data = await response.json() as OAuth2TokenResponse;
54+
const data = await response.json();
9255
return data;
9356
} else {
9457
throw new Error(`Error fetching OAuth tokens: [${response.status}] ${response.statusText}`);
9558
}
9659
}
9760

98-
9961
/**
10062
* The initial token request comes with both an access token and a refresh
10163
* token. Check if the access token has expired, and if it has, use the
10264
* refresh token to acquire a new, fresh access token.
10365
*/
104-
export async function getAccessToken(
105-
userId: string,
106-
data: storage.DiscordData
107-
) {
108-
let tokens: OAuth2TokenResponse; // Define tokens outside the if block
109-
if (Date.now() > data.expires_at) {
66+
export async function getAccessToken(userId, tokens) {
67+
if (Date.now() > tokens.expires_at) {
11068
const url = 'https://discord.com/api/v10/oauth2/token';
11169
const body = new URLSearchParams({
112-
client_id: process.env.DISCORD_CLIENT_ID!,
113-
client_secret: process.env.DISCORD_CLIENT_SECRET!,
70+
client_id: process.env.DISCORD_CLIENT_ID,
71+
client_secret: process.env.DISCORD_CLIENT_SECRET,
11472
grant_type: 'refresh_token',
115-
refresh_token: data.refresh_token,
73+
refresh_token: tokens.refresh_token,
11674
});
11775
const response = await fetch(url, {
11876
body,
@@ -122,47 +80,21 @@ export async function getAccessToken(
12280
},
12381
});
12482
if (response.ok) {
125-
tokens = await response.json() as OAuth2TokenResponse;
83+
const tokens = await response.json();
12684
tokens.expires_at = Date.now() + tokens.expires_in * 1000;
12785
await storage.storeDiscordTokens(userId, tokens);
86+
return tokens.access_token;
12887
} else {
12988
throw new Error(`Error refreshing access token: [${response.status}] ${response.statusText}`);
13089
}
131-
} else {
132-
tokens = data as OAuth2TokenResponse; // Assign the value of data to tokens
13390
}
13491
return tokens.access_token;
13592
}
13693

137-
/**
138-
* Revoke the given user's Discord access and refresh tokens.
139-
* @param userId The Discord User ID
140-
*/
141-
export async function revokeAccess(userId: string) {
142-
const tokens = await storage.getDiscordTokens(userId);
143-
const url = 'https://discord.com/api/v10/oauth2/token';
144-
const body = new URLSearchParams({
145-
client_id: process.env.DISCORD_CLIENT_ID!,
146-
client_secret: process.env.DISCORD_CLIENT_SECRET!,
147-
token: tokens.refresh_token,
148-
token_type_hint: 'refresh_token',
149-
});
150-
await fetch(url, {
151-
body,
152-
method: 'POST',
153-
headers: {
154-
'Content-Type': 'application/x-www-form-urlencoded',
155-
},
156-
});
157-
158-
// remove the tokens from storage
159-
await storage.deleteDiscordTokens(userId);
160-
}
161-
16294
/**
16395
* Given a user based access token, fetch profile information for the current user.
16496
*/
165-
export async function getUserData(tokens: OAuth2TokenResponse) {
97+
export async function getUserData(tokens) {
16698
const url = 'https://discord.com/api/v10/oauth2/@me';
16799
const response = await fetch(url, {
168100
headers: {
@@ -181,16 +113,12 @@ export async function getUserData(tokens: OAuth2TokenResponse) {
181113
* Given metadata that matches the schema, push that data to Discord on behalf
182114
* of the current user.
183115
*/
184-
export async function pushMetadata(
185-
userId: string,
186-
data: storage.DiscordData,
187-
metadata: Record<string, string>
188-
) {
116+
export async function pushMetadata(userId, tokens, metadata) {
189117
// PUT /users/@me/applications/:id/role-connection
190118
const url = `https://discord.com/api/v10/users/@me/applications/${process.env.DISCORD_CLIENT_ID}/role-connection`;
191-
const accessToken = await getAccessToken(userId, data);
119+
const accessToken = await getAccessToken(userId, tokens);
192120
const body = {
193-
platform_name: 'PrismBot Linked Roles',
121+
platform_name: 'Prism Linked Roles',
194122
metadata,
195123
};
196124
const response = await fetch(url, {
@@ -210,13 +138,10 @@ export async function pushMetadata(
210138
* Fetch the metadata currently pushed to Discord for the currently logged
211139
* in user, for this specific bot.
212140
*/
213-
export async function getMetadata(
214-
userId: string,
215-
data: storage.DiscordData
216-
) {
141+
export async function getMetadata(userId, tokens) {
217142
// GET /users/@me/applications/:id/role-connection
218143
const url = `https://discord.com/api/v10/users/@me/applications/${process.env.DISCORD_CLIENT_ID}/role-connection`;
219-
const accessToken = await getAccessToken(userId, data);
144+
const accessToken = await getAccessToken(userId, tokens);
220145
const response = await fetch(url, {
221146
headers: {
222147
Authorization: `Bearer ${accessToken}`,
@@ -228,4 +153,4 @@ export async function getMetadata(
228153
} else {
229154
throw new Error(`Error getting discord metadata: [${response.status}] ${response.statusText}`);
230155
}
231-
}
156+
}

src/register.ts renamed to src/register.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,22 @@ const url = `https://discord.com/api/v10/applications/${process.env.DISCORD_CLIE
88
// supported types: number_lt=1, number_gt=2, number_eq=3 number_neq=4, datetime_lt=5, datetime_gt=6, boolean_eq=7, boolean_neq=8
99
const body = [
1010
{
11-
key: 'ispackager',
12-
name: 'Is Packager',
13-
description: 'Is a Prism Packager',
14-
type: 7,
11+
key: 'cookieseaten',
12+
name: 'Cookies Eaten',
13+
description: 'Cookies Eaten Greater Than',
14+
type: 2,
1515
},
1616
{
17-
key: 'iscoder',
18-
name: 'Is Coder',
19-
description: 'Is a Prism Code Contributor',
17+
key: 'allergictonuts',
18+
name: 'Allergic To Nuts',
19+
description: 'Is Allergic To Nuts',
2020
type: 7,
2121
},
2222
{
23-
key: 'isdocumentation',
24-
name: 'Is Documentation',
25-
description: 'Is a Prism Documentation Contributor',
26-
type: 7,
27-
},
28-
{
29-
key: 'istranslator',
30-
name: 'Is Translator',
31-
description: 'Is a Prism Translations Contributor',
32-
type: 7,
23+
key: 'bakingsince',
24+
name: 'Baking Since',
25+
description: 'Days since baking their first cookie',
26+
type: 6,
3327
},
3428
];
3529

@@ -48,4 +42,4 @@ if (response.ok) {
4842
//throw new Error(`Error pushing discord metadata schema: [${response.status}] ${response.statusText}`);
4943
const data = await response.text();
5044
console.log(data);
51-
}
45+
}

0 commit comments

Comments
 (0)