diff --git a/package.json b/package.json index 756b328..bf42065 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "lint": "eslint --ext .ts ./src", "lint:fix": "eslint --ext .ts ./src --fix", "generate": "prisma generate", - "migrate": "prisma migrate deploy" + "migrate": "prisma migrate deploy", + "role-metadata": "tsx scripts/roleMetadata" }, "dependencies": { "@influxdata/influxdb-client": "1.33.2", diff --git a/scripts/roleMetadata.ts b/scripts/roleMetadata.ts new file mode 100644 index 0000000..87e79dc --- /dev/null +++ b/scripts/roleMetadata.ts @@ -0,0 +1,51 @@ +import 'dotenv/config'; + +(async () => { + if (!process.env.DISCORD_APP_ID || !process.env.DISCORD_BOT_TOKEN) { + console.error('No token and/or app ID provided.'); + process.exit(1); + } + + const ROLE_METADATA_URL = `https://discord.com/api/v10/applications/${process.env.DISCORD_APP_ID}/role-connections/metadata`; + + const response = await fetch( + ROLE_METADATA_URL, + { headers: { 'Authorization': `Bot ${process.env.DISCORD_BOT_TOKEN}` } } + ); + + if (response.status !== 200) { + console.error('Failed to fetch role metadata', await response.text()); + process.exit(1); + } + + const json = await response.json(); + if (json.length !== 0) { + console.log('Role metadata already applied.') + } else { + const putResponse = await fetch( + ROLE_METADATA_URL, + { + method: 'PUT', + headers: { + 'Authorization': `Bot ${process.env.DISCORD_BOT_TOKEN}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify([ + { + key: "connected", + type: 7, + name: "Connected", + description: "The user has connected a Trello account" + } + ]) + } + ); + + if (putResponse.status === 200) { + console.log('Updated role metadata.') + } else { + console.error('Failed to update role metadata', await response.text()); + process.exit(1); + } + } +})(); diff --git a/src/actions/clearAuth.ts b/src/actions/clearAuth.ts index a76c7c8..50db6f9 100644 --- a/src/actions/clearAuth.ts +++ b/src/actions/clearAuth.ts @@ -3,6 +3,7 @@ import { ComponentContext } from 'slash-create'; import { getData } from '../util'; import { ActionFunction, ActionType } from '../util/actions'; import { prisma } from '../util/prisma'; +import { VERSION } from '../util/constants'; export const action: ActionFunction = { type: ActionType.USER_CLEAR_AUTH, @@ -20,6 +21,18 @@ export const action: ActionFunction = { where: { memberID: userData.trelloID }, data: { active: false, memberID: null } }); + if (userData.discordToken) + await fetch(`https://discord.com/api/users/@me/applications/${process.env.DISCORD_APP_ID}/role-connection`, { + method: 'PUT', + headers: { + Authorization: `Bearer ${userData.discordToken}`, + 'User-Agent': `Taco (https://github.com/trello-talk/TacoInteractions, v${VERSION})`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + metadata: { connected: false } + }) + }); return void ctx.editParent({ content: t('clearauth.done'), components: [] }); } diff --git a/src/actions/clearData.ts b/src/actions/clearData.ts index 562dca4..817e8b4 100644 --- a/src/actions/clearData.ts +++ b/src/actions/clearData.ts @@ -3,6 +3,7 @@ import { ComponentContext } from 'slash-create'; import { getData } from '../util'; import { ActionFunction, ActionType } from '../util/actions'; import { prisma } from '../util/prisma'; +import { VERSION } from '../util/constants'; export const action: ActionFunction = { type: ActionType.USER_CLEAR_DATA, @@ -21,6 +22,18 @@ export const action: ActionFunction = { where: { memberID: userData.trelloID }, data: { active: false, memberID: null } }); + if (userData.discordToken) + await fetch(`https://discord.com/api/users/@me/applications/${process.env.DISCORD_APP_ID}/role-connection`, { + method: 'PUT', + headers: { + Authorization: `Bearer ${userData.discordToken}`, + 'User-Agent': `Taco (https://github.com/trello-talk/TacoInteractions, v${VERSION})`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + metadata: { connected: false } + }) + }); return void ctx.editParent({ content: t('cleardata.done'), components: [] }); }