From ba90e16f5eda907b36fbe4ebc8c9d45a451f8996 Mon Sep 17 00:00:00 2001 From: DaInfLoop Date: Tue, 23 Jul 2024 11:14:51 +0100 Subject: [PATCH] you can now link properly!! --- README.md | 2 + index.ts | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 127 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8ed4012..0cf4aeb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # nxslack +Slack bot to set your Nintendo Switch as your status! + To install dependencies: ```bash diff --git a/index.ts b/index.ts index b940044..2196127 100644 --- a/index.ts +++ b/index.ts @@ -1,8 +1,10 @@ import { name, version, repository } from "./package.json"; const USER_AGENT = `${name}/${version} (+${repository.url})`; -const { App, ExpressReceiver } = (await import("@slack/bolt"));import postgres from 'postgres'; +const { App, ExpressReceiver } = (await import("@slack/bolt")); +import postgres from 'postgres'; import "dotenv/config"; +import { spawnSync } from "child_process"; const sql = postgres({ host: '/var/run/postgresql', @@ -10,18 +12,54 @@ const sql = postgres({ username: 'haroon' }) -const receiver = new ExpressReceiver({ signingSecret: process.env.SLACK_SIGNING_SECRET! }); - const app = new App({ token: process.env.SLACK_BOT_TOKEN, signingSecret: process.env.SLACK_SIGNING_SECRET, - clientSecret: process.env.SLACK_CLIENT_SECRET, - receiver + clientSecret: process.env.SLACK_CLIENT_SECRET }); app.event("app_home_opened", async (ctx) => { const slackUser = (await ctx.client.users.info({ user: ctx.context.userId! })).user!.profile; + const [ link = null ] = await sql`SELECT * FROM links WHERE slackid = ${ctx.context.userId!};`; + + if (link) { + const frens = getFriends(); + const me = frens.find(x => x.nsaId == link.nsaid)!; + + console.log(me, frens) + + await ctx.client.views.publish({ + user_id: ctx.event.user, + view: { + type: 'home', + blocks: [ + { + type: 'header', + text: { + type: 'plain_text', + text: "You're linked!", + emoji: true + } + }, + { + "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>.` + }, + "accessory": { + "type": "image", + "image_url": me.imageUri, + "alt_text": "switch profile picture" + } + } + ] + } + }); + return + } + await ctx.client.views.publish({ // Use the user ID associated with the event user_id: ctx.event.user, @@ -32,7 +70,7 @@ app.event("app_home_opened", async (ctx) => { "type": "header", "text": { "type": "plain_text", - "text": "Not authenticated", + "text": "You aren't linked", "emoji": true } }, @@ -112,13 +150,92 @@ app.view("friend-code-validation", async (ctx) => { const friendCode = Object.values(ctx.view.state.values)[0]['code'].value!; - if (FRIEND_CODE_REGEX.test(friendCode)) return await ctx.ack(); - else return await ctx.ack({ + if (!FRIEND_CODE_REGEX.test(friendCode)) return await ctx.ack({ response_action: 'errors', errors: { "FRIEND_CODE": "This isn't a valid friend code. (SW-xxxx-xxxx-xxxx or xxxx-xxxx-xxxx)" } }); + + const realCode = friendCode.replace("SW-", ""); + + spawnSync("/home/haroon/nxslack/node_modules/.bin/nxapi", ["nso", "add-friend", realCode], { cwd: process.cwd(), env: { + 'NXAPI_USER_AGENT': USER_AGENT + } }); + + await ctx.ack(); + + ctx.client.chat.postMessage({ + channel: ctx.body.user.id, + text: "I've added you on Nintendo Switch! Find my friend request and accept it or add me yourself (SW-2973-7062-1423) and then click this button to finish the process!", + blocks: [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: "I've added you on Nintendo Switch! Find my friend request and accept it or add me back (`SW-2973-7062-1423`) and then click this button to finish the process!" + } + }, + { + "type": "actions", + "elements": [ + { + "type": "button", + "text": { + "type": "plain_text", + "text": "I've accepted!", + "emoji": true + }, + "value": realCode, + "action_id": "check-synced" + } + ] + } + ] + }) +}) + +type NSOUser = { + id: number, + nsaId: string, + imageUri: string, + name: string, + isFriend: boolean, + isFavouriteFriend: boolean, + isServiceUser: boolean, + friendCreatedAt: number, + presence: any +} + +const getFriends = function getFriends(): NSOUser[] { + const { stdout } = spawnSync("/home/haroon/nxslack/node_modules/.bin/nxapi", ["nso", "friends", "--json"], { cwd: process.cwd(), env: { + 'NXAPI_USER_AGENT': USER_AGENT + } }); + + return JSON.parse(stdout.toString('utf-8').slice()); +}; + +app.action('check-synced', async (ctx) => { + if (ctx.action.type != "button") return; + await ctx.ack(); + + const friends = getFriends(); + + const { stdout } = spawnSync("/home/haroon/nxslack/node_modules/.bin/nxapi", ["nso", "lookup", ctx.action.value!, "--json"], { cwd: process.cwd(), env: { + 'NXAPI_USER_AGENT': USER_AGENT + } }); + + console.log(stdout.toString()) + + const user: NSOUser = JSON.parse(stdout.toString('utf-8').slice()); + + if (!!friends.find(userA => userA.nsaId == user.nsaId)) { + await sql`INSERT INTO links VALUES (${ctx.context.userId!}, ${user.nsaId})` + + ctx.respond({ + text: `Congrats! You've successfully linked your Slack account to your Nintendo Switch user (${user.name})!` + }) + } }) ; (async () => {