From 85128f389a717b7f59f3f18a6a5ddbe3c05c3722 Mon Sep 17 00:00:00 2001 From: Rik Berkelder Date: Mon, 13 Jan 2025 23:06:06 +0100 Subject: [PATCH 1/2] cleanup. handle some potential undefined values --- index.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 885801a..fa29f65 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,6 @@ const apiId = parseInt(process.env.API_ID); const apiHash = process.env.API_HASH; const songPrefix = process.env.SONG_PREFIX || ""; -console.log(process.env); const sessionStore = new StoreSession("tg_session"); async function getCurrentSong() { @@ -35,7 +34,8 @@ async function getCurrentSong() { // if a player exists, get it's song if (sortedPlayers.length !== 0) { - const playData = sortedPlayers[0].play.data; + const playData = sortedPlayers[0]?.play?.data; + if (!playData) return ""; nowPlaying = `${playData.artists.join(', ')} - ${playData.track}`; } @@ -72,7 +72,6 @@ async function initTelegram() { onError: (err) => console.error(err) }); - // save the session so we don't have to log in every time client.session.save(); @@ -80,13 +79,9 @@ async function initTelegram() { } -prompt.start(); - - // store client here const tg = await initTelegram(); - // do we actually have a client? if (!!tg) { let song = ""; @@ -99,6 +94,8 @@ if (!!tg) { console.log(`new song: ${song}`); + const ytURL = await getYoutube(song); + // update bio with new song tg.invoke(new TgApi.account.UpdateProfile({ about: songPrefix + song From 0acef577a33930aa06fabf5bc8705505adfbdbdb Mon Sep 17 00:00:00 2001 From: Rik Berkelder Date: Mon, 13 Jan 2025 23:33:18 +0100 Subject: [PATCH 2/2] manually handle storing session in file --- index.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fa29f65..5c1dde0 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,30 @@ import fetch from 'node-fetch'; import { TelegramClient, Api as TgApi } from 'telegram'; -import { StoreSession } from 'telegram/sessions/index.js'; +import { StringSession } from 'telegram/sessions/index.js'; import 'dotenv/config'; import prompt from 'prompt'; +import fs from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const apiId = parseInt(process.env.API_ID); const apiHash = process.env.API_HASH; const songPrefix = process.env.SONG_PREFIX || ""; -const sessionStore = new StoreSession("tg_session"); +const sessionFile = path.join(__dirname, 'tg_session', 'session'); + +let sessionStore = new StringSession(""); +try { + const readSession = await fs.readFile(sessionFile, "utf8"); + if (readSession) { + sessionStore = new StringSession(readSession); + } +} catch (e) { + console.error(e); +} async function getCurrentSong() { return fetch('https://multi-scrobbler.riksolo.com/api/status').then((data) => { @@ -75,6 +91,8 @@ async function initTelegram() { // save the session so we don't have to log in every time client.session.save(); + await fs.writeFile(sessionFile, sessionStore.save()); + return client; }