diff --git a/.env.types b/.env.types index 2680783..eb74f2b 100644 --- a/.env.types +++ b/.env.types @@ -2,7 +2,7 @@ TWITCH_CHANNEL=string SPOTIFY_CLIENT_ID=string SPOTIFY_CLIENT_SECRET=string SPOTIFY_PLAYLIST_ID=string -TWITCH_TOKEN=boolean +TWITCH_TOKEN=string BOT_USERNAME=string CHAT_FEEDBACK=boolean ADD_TO_QUEUE=boolean diff --git a/.gitignore b/.gitignore index e7b9674..4ec77b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .env spotify-auth-store.json build/ -node_modules/ \ No newline at end of file +node_modules/ +.idea/ \ No newline at end of file diff --git a/src/auth-server.ts b/src/auth-server.ts index cf7501f..cdff643 100644 --- a/src/auth-server.ts +++ b/src/auth-server.ts @@ -2,10 +2,11 @@ import express from 'express'; import 'dotenv/config' import env from 'env-smart'; env.load(); +const { AUTH_SERVER_PORT } = process.env export const waitForCode = (onCodeReceived: Function) => { const app = express(); - const port = process.env.AUTH_SERVER_PORT; + const port = AUTH_SERVER_PORT; const server = app.listen(port, (err: Error) => { if(err) return console.error(err); diff --git a/src/spotify.service.ts b/src/spotify.service.ts index 19b4ac3..d954b43 100644 --- a/src/spotify.service.ts +++ b/src/spotify.service.ts @@ -6,6 +6,7 @@ import SpotifyAuth from './spotify-auth'; import fs from 'fs'; import { response } from 'express'; env.load(); +const { SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, AUTH_SERVER_PORT, ADD_TO_QUEUE, ADD_TO_PLAYLIST, SPOTIFY_PLAYLIST_ID} = process.env export default class SpotifyService { private spotifyApi: SpotifyWebApi; @@ -13,9 +14,9 @@ export default class SpotifyService { constructor() { this.spotifyApi = new SpotifyWebApi({ - clientId: process.env.SPOTIFY_CLIENT_ID, - clientSecret: process.env.SPOTIFY_CLIENT_SECRET, - redirectUri: `http://localhost:${process.env.AUTH_SERVER_PORT}/spotifyAuth`, + clientId: SPOTIFY_CLIENT_ID, + clientSecret: SPOTIFY_CLIENT_SECRET, + redirectUri: `http://localhost:${AUTH_SERVER_PORT}/spotifyAuth`, }); if (!fs.existsSync('./spotify-auth-store.json')) { @@ -54,7 +55,7 @@ export default class SpotifyService { const addSong = async () => { console.log(`Attempting to add ${trackId}`); const songInfo = await this.spotifyApi.getTrack(trackId); - if (process.env.ADD_TO_QUEUE) { + if (ADD_TO_QUEUE) { try { await this.addToQueue(trackId, songInfo?.body.name); chatFeedback(`Success: ${songInfo?.body.name} added to queue`); @@ -70,7 +71,7 @@ export default class SpotifyService { } } - if (process.env.ADD_TO_PLAYLIST) { + if (ADD_TO_PLAYLIST) { try { await this.addToPlaylist(trackId, songInfo?.body.name); chatFeedback(`Success: ${songInfo?.body.name} added to playlist`); @@ -109,12 +110,12 @@ export default class SpotifyService { } private async addToPlaylist(trackId: string, songName: string) { - if (process.env.SPOTIFY_PLAYLIST_ID) { + if (SPOTIFY_PLAYLIST_ID) { if (await this.doesPlaylistContainTrack(trackId)) { console.log(`${songName} is already in the playlist`); throw new Error('Duplicate Track'); } else { - await this.spotifyApi.addTracksToPlaylist(process.env.SPOTIFY_PLAYLIST_ID, [ + await this.spotifyApi.addTracksToPlaylist(SPOTIFY_PLAYLIST_ID, [ this.createTrackURI(trackId), ]); console.log(`Added ${songName} to playlist`); @@ -131,7 +132,7 @@ export default class SpotifyService { private async doesPlaylistContainTrack(trackId: string) { const playlistInfo = await this.spotifyApi.getPlaylist( - process.env.SPOTIFY_PLAYLIST_ID! + SPOTIFY_PLAYLIST_ID! ); let i; diff --git a/src/twitch.service.ts b/src/twitch.service.ts index 2ff6709..239a2f8 100644 --- a/src/twitch.service.ts +++ b/src/twitch.service.ts @@ -3,8 +3,8 @@ import { getTrackIdFromLink, SPOTIFY_LINK_START } from './messageUtils'; import SpotifyService from './spotify.service'; import 'dotenv/config' import env from 'env-smart'; -import e from 'express'; env.load(); +const { TWITCH_CHANNEL, COMMAND_PREFIX, SUBSCRIBERS_ONLY, TWITCH_TOKEN, BOT_USERNAME, CHAT_FEEDBACK } = process.env interface TwitchOptions { channels: string[]; @@ -21,16 +21,16 @@ export default class TwitchService { public async connectToChat() { let twitchOptions: TwitchOptions = { - channels: [process.env.TWITCH_CHANNEL], + channels: [TWITCH_CHANNEL], }; - if (process.env.CHAT_FEEDBACK) { - if (process.env.TWITCH_TOKEN && process.env.BOT_USERNAME) { + if (CHAT_FEEDBACK) { + if (TWITCH_TOKEN && BOT_USERNAME) { twitchOptions = { ...twitchOptions, identity: { - username: process.env.BOT_USERNAME, - password: process.env.TWITCH_TOKEN, + username: BOT_USERNAME, + password: TWITCH_TOKEN, }, }; } else { @@ -44,7 +44,7 @@ export default class TwitchService { this.twitchClient = tmi.client(twitchOptions); this.twitchClient.on('connected', (_addr: string, _port: number) => { - console.log(`Connected to ${process.env.TWITCH_CHANNEL}'s chat`); + console.log(`Connected to ${TWITCH_CHANNEL}'s chat`); }); this.twitchClient.on( @@ -75,15 +75,15 @@ export default class TwitchService { return; } - if (process.env.COMMAND_PREFIX && msg.startsWith(process.env.COMMAND_PREFIX)) { - if (process.env.SUBSCRIBERS_ONLY) { + if (COMMAND_PREFIX && msg.startsWith(COMMAND_PREFIX)) { + if (SUBSCRIBERS_ONLY) { if (!userState.subscriber) { return; } } console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); - msg = msg.substring(`${process.env.COMMAND_PREFIX} `.length); + msg = msg.substring(`${COMMAND_PREFIX} `.length); if (msg.startsWith(SPOTIFY_LINK_START)) { await this.handleSpotifyLink(msg, target); } else { @@ -110,7 +110,7 @@ export default class TwitchService { } private chatFeedback(target: string, message: string) { - if (process.env.CHAT_FEEDBACK) { + if (CHAT_FEEDBACK) { this.twitchClient?.say(target, message); } }