Skip to content

Commit

Permalink
Fixing some environment variables problems
Browse files Browse the repository at this point in the history
Solving and improving the code based on the comment

#9 (review)

If I forgot something, please feel free to mention me :)
  • Loading branch information
IvanGodinez21 authored and MarcDonald committed Apr 15, 2021
1 parent 7eff113 commit c37b873
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .env.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
spotify-auth-store.json
build/
node_modules/
node_modules/
.idea/
3 changes: 2 additions & 1 deletion src/auth-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions src/spotify.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ 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;
private spotifyAuth: SpotifyAuth;

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')) {
Expand Down Expand Up @@ -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`);
Expand All @@ -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`);
Expand Down Expand Up @@ -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`);
Expand All @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions src/twitch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit c37b873

Please sign in to comment.