From 4b5edd73df3cebedb86b2ddee271ddf4ca61ccb7 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Wed, 7 Sep 2022 16:29:36 +0100 Subject: [PATCH] Add a command to show the user's active event --- src/cli/nso/active-event.ts | 65 +++++++++++++++++++++++++++++++++++++ src/cli/nso/index.ts | 1 + 2 files changed, 66 insertions(+) create mode 100644 src/cli/nso/active-event.ts diff --git a/src/cli/nso/active-event.ts b/src/cli/nso/active-event.ts new file mode 100644 index 0000000..c68a7ee --- /dev/null +++ b/src/cli/nso/active-event.ts @@ -0,0 +1,65 @@ +import createDebug from 'debug'; +import type { Arguments as ParentArguments } from '../nso.js'; +import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js'; +import { initStorage } from '../../util/storage.js'; +import { getToken, Login } from '../../common/auth/coral.js'; + +const debug = createDebug('cli:nso:active-event'); + +export const command = 'active-event'; +export const desc = 'Show the user\'s current Online Lounge/voice chat event'; + +export function builder(yargs: Argv) { + return yargs.option('user', { + describe: 'Nintendo Account ID', + type: 'string', + }).option('token', { + describe: 'Nintendo Account session token', + type: 'string', + }).option('json', { + describe: 'Output raw JSON', + type: 'boolean', + }).option('json-pretty-print', { + describe: 'Output pretty-printed JSON', + type: 'boolean', + }); +} + +type Arguments = YargsArguments>; + +export async function handler(argv: ArgumentsCamelCase) { + const storage = await initStorage(argv.dataPath); + + const usernsid = argv.user ?? await storage.getItem('SelectedUser'); + const token: string = argv.token || + await storage.getItem('NintendoAccountToken.' + usernsid); + const {nso, data} = await getToken(storage, token, argv.zncProxyUrl); + + if (data[Login]) { + const announcements = await nso.getAnnouncements(); + } + + const webservices = await nso.getWebServices(); + const friends = await nso.getFriendList(); + const activeevent = await nso.getActiveEvent(); + + if ('id' in activeevent.result) { + if (argv.jsonPrettyPrint) { + console.log(JSON.stringify(activeevent.result, null, 4)); + return; + } + if (argv.json) { + console.log(JSON.stringify(activeevent.result)); + return; + } + + console.log('Active event', activeevent.result); + } else { + if (argv.json || argv.jsonPrettyPrint) { + console.log('null'); + return; + } + + console.log('No active event'); + } +} diff --git a/src/cli/nso/index.ts b/src/cli/nso/index.ts index 52b6275..a849f3b 100644 --- a/src/cli/nso/index.ts +++ b/src/cli/nso/index.ts @@ -6,6 +6,7 @@ export * as announcements from './announcements.js'; export * as webservices from './webservices.js'; export * as webservicetoken from './webservicetoken.js'; export * as friends from './friends.js'; +export * as activeEvent from './active-event.js'; export * as presence from './presence.js'; export * as notify from './notify.js'; export * as httpServer from './http-server.js';