Skip to content

Commit ac02580

Browse files
committed
feat: add role metadata
1 parent c29128c commit ac02580

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"lint": "eslint --ext .ts ./src",
2525
"lint:fix": "eslint --ext .ts ./src --fix",
2626
"generate": "prisma generate",
27-
"migrate": "prisma migrate deploy"
27+
"migrate": "prisma migrate deploy",
28+
"role-metadata": "tsx scripts/roleMetadata"
2829
},
2930
"dependencies": {
3031
"@influxdata/influxdb-client": "1.33.2",

scripts/roleMetadata.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'dotenv/config';
2+
3+
(async () => {
4+
if (!process.env.DISCORD_APP_ID || !process.env.DISCORD_BOT_TOKEN) {
5+
console.error('No token and/or app ID provided.');
6+
process.exit(1);
7+
}
8+
9+
const ROLE_METADATA_URL = `https://discord.com/api/v10/applications/${process.env.DISCORD_APP_ID}/role-connections/metadata`;
10+
11+
const response = await fetch(
12+
ROLE_METADATA_URL,
13+
{ headers: { 'Authorization': `Bot ${process.env.DISCORD_BOT_TOKEN}` } }
14+
);
15+
16+
if (response.status !== 200) {
17+
console.error('Failed to fetch role metadata', await response.text());
18+
process.exit(1);
19+
}
20+
21+
const json = await response.json();
22+
if (json.length !== 0) {
23+
console.log('Role metadata already applied.')
24+
} else {
25+
const putResponse = await fetch(
26+
ROLE_METADATA_URL,
27+
{
28+
method: 'PUT',
29+
headers: {
30+
'Authorization': `Bot ${process.env.DISCORD_BOT_TOKEN}`,
31+
'Content-Type': 'application/json'
32+
},
33+
body: JSON.stringify([
34+
{
35+
key: "connected",
36+
type: 7,
37+
name: "Connected",
38+
description: "The user has connected a Trello account"
39+
}
40+
])
41+
}
42+
);
43+
44+
if (putResponse.status === 200) {
45+
console.log('Updated role metadata.')
46+
} else {
47+
console.error('Failed to update role metadata', await response.text());
48+
process.exit(1);
49+
}
50+
}
51+
})();

src/actions/clearAuth.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ComponentContext } from 'slash-create';
33
import { getData } from '../util';
44
import { ActionFunction, ActionType } from '../util/actions';
55
import { prisma } from '../util/prisma';
6+
import { VERSION } from '../util/constants';
67

78
export const action: ActionFunction = {
89
type: ActionType.USER_CLEAR_AUTH,
@@ -20,6 +21,18 @@ export const action: ActionFunction = {
2021
where: { memberID: userData.trelloID },
2122
data: { active: false, memberID: null }
2223
});
24+
if (userData.discordToken)
25+
await fetch(`https://discord.com/api/users/@me/applications/${process.env.DISCORD_APP_ID}/role-connection`, {
26+
method: 'PUT',
27+
headers: {
28+
Authorization: `Bearer ${userData.discordToken}`,
29+
'User-Agent': `Taco (https://github.com/trello-talk/TacoInteractions, v${VERSION})`,
30+
'Content-Type': 'application/json'
31+
},
32+
body: JSON.stringify({
33+
metadata: { connected: false }
34+
})
35+
});
2336

2437
return void ctx.editParent({ content: t('clearauth.done'), components: [] });
2538
}

src/actions/clearData.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ComponentContext } from 'slash-create';
33
import { getData } from '../util';
44
import { ActionFunction, ActionType } from '../util/actions';
55
import { prisma } from '../util/prisma';
6+
import { VERSION } from '../util/constants';
67

78
export const action: ActionFunction = {
89
type: ActionType.USER_CLEAR_DATA,
@@ -21,6 +22,18 @@ export const action: ActionFunction = {
2122
where: { memberID: userData.trelloID },
2223
data: { active: false, memberID: null }
2324
});
25+
if (userData.discordToken)
26+
await fetch(`https://discord.com/api/users/@me/applications/${process.env.DISCORD_APP_ID}/role-connection`, {
27+
method: 'PUT',
28+
headers: {
29+
Authorization: `Bearer ${userData.discordToken}`,
30+
'User-Agent': `Taco (https://github.com/trello-talk/TacoInteractions, v${VERSION})`,
31+
'Content-Type': 'application/json'
32+
},
33+
body: JSON.stringify({
34+
metadata: { connected: false }
35+
})
36+
});
2437

2538
return void ctx.editParent({ content: t('cleardata.done'), components: [] });
2639
}

0 commit comments

Comments
 (0)