diff --git a/index.ts b/index.ts index 2196127..1b7444a 100644 --- a/index.ts +++ b/index.ts @@ -19,7 +19,7 @@ const app = new App({ }); app.event("app_home_opened", async (ctx) => { - const slackUser = (await ctx.client.users.info({ user: ctx.context.userId! })).user!.profile; + const slackUser = (await ctx.client.users.info({ user: ctx.context.userId! })).user!; const [ link = null ] = await sql`SELECT * FROM links WHERE slackid = ${ctx.context.userId!};`; @@ -46,7 +46,7 @@ app.event("app_home_opened", async (ctx) => { "type": "section", "text": { "type": "mrkdwn", - "text": `Hey *${slackUser?.display_name_normalized}* - you're already linked to a Nintendo Switch account (${me.name}). To unlink yourself, DM <@U06TBP41C3E>.` + "text": `Hey *${slackUser.profile?.display_name_normalized}* - you're already linked to a Nintendo Switch account (${me.name}). To unlink yourself, DM <@U06TBP41C3E>.` }, "accessory": { "type": "image", @@ -78,7 +78,7 @@ app.event("app_home_opened", async (ctx) => { "type": "section", "text": { "type": "mrkdwn", - "text": `Hey *${slackUser?.display_name_normalized}*! To sync your Nintendo Switch and Slack status, give me your friend code!` + "text": `Hey *${slackUser.profile?.display_name_normalized}*! To sync your Nintendo Switch and Slack status, give me your friend code!` }, "accessory": { "type": "button", @@ -90,7 +90,17 @@ app.event("app_home_opened", async (ctx) => { "value": "click_me_123", "action_id": "get-friend-code" } - } + }, + ...(slackUser.is_admin || slackUser.is_owner ? [ + { + type: 'section', + // @ts-expect-error this feels dumb, but it said text wasn't a value on Block... what + text: { + type: 'mrkdwn', + text: `By the way, *${slackUser.profile?.display_name_normalized}*... I can't actually EDIT your status because of how Slack works. I know that sounds annoying, but hopefully that should be fixed whenever I get an admin API token!` + } + } + ] : []) ] } }); @@ -242,4 +252,52 @@ app.action('check-synced', async (ctx) => { await app.start(53371); console.log('⚡️ Bolt app is running!'); + + setInterval(async () => { + const frens = getFriends(); + const links = await sql`SELECT * FROM links;`; + + for (let fren of frens) { + const dbFren = links.find(frend => frend.nsaid == fren.nsaId)!; + const slackUser = (await app.client.users.info({ user: dbFren.slackid })).user!; + + if (slackUser.is_admin || slackUser.is_owner) return; + + switch (fren.presence.state) { + // Console offline + case "OFFLINE": + // Console online; but not playing a game + case "INACTIVE": + if (dbFren.last_status != null) { + await app.client.users.profile.set({ + token: process.env.SLACK_USER_OAUTH_TOKEN, + user: dbFren.slackid, + // @ts-expect-error This is correct, Bolt has incorrect typing + profile: { + status_text: "", + status_emoji: "" + } + }) + await sql`UPDATE links SET last_status = NULL WHERE nsaId = ${fren.nsaId};`; + } + break; + + // Playing a game + case "ONLINE": + if (dbFren.last_status != `Playing ${fren.presence.game.name}`) { + await app.client.users.profile.set({ + token: process.env.SLACK_USER_OAUTH_TOKEN, + user: dbFren.slackid, + // @ts-expect-error This is correct, Bolt has incorrect typing + profile: { + status_text: `Playing ${fren.presence.game.name}`, + status_emoji: ":nintendo:" + } + }) + await sql`UPDATE links SET last_status = ${`Playing ${fren.presence.game.name}`} WHERE nsaId = ${fren.nsaId};`; + } + break; + } + } + }, 60_000) })(); \ No newline at end of file