Skip to content

Commit

Permalink
feat: add role metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Sep 6, 2024
1 parent c29128c commit ac02580
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 51 additions & 0 deletions scripts/roleMetadata.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
})();
13 changes: 13 additions & 0 deletions src/actions/clearAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: [] });
}
Expand Down
13 changes: 13 additions & 0 deletions src/actions/clearData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: [] });
}
Expand Down

0 comments on commit ac02580

Please sign in to comment.