Skip to content

Commit

Permalink
Feature/move helpers to lib + fixes (#79)
Browse files Browse the repository at this point in the history
* Fixed up menu position and some styles on input fields

* Updated changelogs and bumped version

* Moved some Non-Spotify related helper functions to new npm library, converted copy-build to ts just because

* Updated oceanity/firebot-helpers dependency

* Moved DbService to helper library, converted use of Db in lyrics to fs-extra because it's just raw saving/loading entire json files

* Removed live queue updating, removed announced deprecated variables, deprecated more variables

* Moved calls to firebot utils to @oceanity/firebot-helpers

* Updated mock

* Moved more methods to @oceanity/firebot-helpers

* Fixed some typos

* Updated queue variables to pull live data

* Updated description

* removed Launch

* updated changelog
  • Loading branch information
Oceanity authored Aug 9, 2024
1 parent 2bb83ab commit 0426672
Show file tree
Hide file tree
Showing 63 changed files with 356 additions and 795 deletions.
12 changes: 11 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@
"typescript.tsdk": "node_modules\\typescript\\lib",
"editor.tabSize": 2,
"editor.insertSpaces": true,
"compile-hero.disable-compile-files-on-did-save-code": true
"compile-hero.disable-compile-files-on-did-save-code": true,
"cSpell.words": [
"Audiobook",
"audiobooks",
"crowbartools",
"firebot",
"isrc",
"oceanity",
"Timecode",
"Upsell"
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Spotify Integration by Oceanity <sub style="color:gray">v0.7.4b</sub>
# Spotify Integration by Oceanity <sub style="color:gray">v0.7.5</sub>

This is a Firebot Script that will allow you to integrate Spotify functionality and information into your Firebot setup. Due to very stict limits on Spotify's API, it does require that you make your own application in Spotify's developer portal and supply your own Client ID and Secret.

Expand Down
24 changes: 24 additions & 0 deletions changelogs/changelog-0.7.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## What's Changed

**Fixes**
- Plugin will no longer Chat Feed Alert on errors
- No more errors when Non-Premium user uses plugin due to Queue pulls

**Removed Variables**
- $spotifyTrackTitle
- $spotifyTrackArtist
- $spotifyTrackArtists
- you were warned :P
- more variables have been marked for deletion, if you see warnings in chat feed alerts, you should fix them

**Removed Events**
- Queue Changed
- Honestly very unfortunate this needs to be regressed, but, blame Spotify having a weird Premium-only limitation on Get Spotify Queue in their API /shrug

**Changed Variables**
- $spotifyQueue
- $rawSpotifyQueue
- Due to the fact these need premium, they now pull every time they use the variable with auth, so I'd advise folks to save these to a custom variable and pull data from that to avoid rate limits

**Behind the Scenes**
- Moved some helper functions to a new shared library, `@oceanity/firebot-helpers`, this shouldn't have any functional effect on the plugin but means I can be lazier in my others
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"name": "oceanity-spotify",
"displayName": "Spotify (by Oceanity)",
"scriptOutputName": "oceanitySpotifyIntegration",
"version": "0.7.4b",
"description": "Adds Spotify Song Requests to Firebot",
"version": "0.7.5",
"description": "Adds Spotify events, effects and variables to Firebot",
"main": "",
"scripts": {
"build": "webpack",
"build:dev": "npm run build && npm run copy",
"copy": "node ./scripts/copy-build.js",
"copy": "ts-node ./scripts/copy-build.ts",
"test": "jest"
},
"author": "Oceanity",
"license": "GNU3",
"devDependencies": {
"@crowbartools/firebot-custom-scripts-types": "^5.60.1",
"@oceanity/firebot-helpers": "^1.1.1",
"@types/express": "^4.17.21",
"@types/fs-extra": "^11.0.4",
"@types/he": "^1.2.3",
Expand Down
30 changes: 16 additions & 14 deletions scripts/copy-build.js → scripts/copy-build.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
/**
* Copies the built script .js to Firebot's scripts folder
*/
const fs = require("fs").promises;
const path = require("path");
import * as fs from "fs/promises";
import * as path from "path";

const extractScriptName = () => {
const packageJson = require("../package.json");
return `${packageJson.scriptOutputName}.js`;
};

const getFirebotScriptsFolderPath = () => {
const home = process.env.HOME;

if (!home) {
throw new Error("Required Env var not set");
}

// determine os app data folder
let appDataFolderPath;
if (process.platform === "win32") {
appDataFolderPath = process.env.APPDATA;
} else if (process.platform === "darwin") {
appDataFolderPath = path.join(
process.env.HOME,
"/Library/Application Support"
);
appDataFolderPath = path.resolve(home, "/Library/Application Support");
} else if (process.platform === "linux") {
appDataFolderPath = path.join(
process.env.HOME,
"/.config"
);
} else {
throw new Error("Unsupported OS!");
appDataFolderPath = path.resolve(home, "/.config");
}

if (!appDataFolderPath) {
throw new Error("Unable to determine app data folder");
}

const firebotDataFolderPath = path.join(appDataFolderPath, "/Firebot/v5/");
Expand Down Expand Up @@ -57,7 +59,7 @@ const main = async () => {
const scriptName = extractScriptName();

const srcScriptFilePath = path.resolve(`./dist/${scriptName}`);
const destScriptFilePath = path.join(
const destScriptFilePath = path.resolve(
firebotScriptsFolderPath,
`${scriptName}`
);
Expand All @@ -67,4 +69,4 @@ const main = async () => {
console.log(`Successfully copied ${scriptName} to Firebot scripts folder.`);
};

main();
main();
4 changes: 2 additions & 2 deletions src/firebot/effects/spotifyCancelUserQueuesEffect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spotify } from "@/main";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import { getErrorMessage } from "@/utils/string";
import { logger } from "@/utils/firebot";
import { getErrorMessage } from "@oceanity/firebot-helpers/string";
import { logger } from "@oceanity/firebot-helpers/firebot";

type EffectParams = { username: string; amount: string };

Expand Down
2 changes: 1 addition & 1 deletion src/firebot/effects/spotifyChangePlaybackStateEffect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spotify } from "@/main";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import { getErrorMessage } from "@/utils/string";
import { getErrorMessage } from "@oceanity/firebot-helpers/string";

type EffectParams = { playbackState: string };

Expand Down
2 changes: 1 addition & 1 deletion src/firebot/effects/spotifyChangePlaybackVolumeEffect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spotify } from "@/main";
import { getErrorMessage } from "@/utils/string";
import { getErrorMessage } from "@oceanity/firebot-helpers/string";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";

type EffectParams = { volume: string };
Expand Down
9 changes: 1 addition & 8 deletions src/firebot/effects/spotifyChangeRepeatStateEffect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { spotify } from "@/main";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import { getErrorMessage } from "@/utils/string";
import { chatFeedAlert } from "@/utils/firebot";
import { getErrorMessage } from "@oceanity/firebot-helpers/string";

type EffectParams = {
repeatState: SpotifyRepeatState;
Expand Down Expand Up @@ -35,12 +34,10 @@ export const SpotifyChangeRepeatStateEffect: Firebot.EffectType<EffectParams> =
optionsTemplate: `
<eos-container header="Repeat Mode" pad-top="true">
<dropdown-select options="repeatStateOptions" selected="effect.repeatState"></dropdown-select>
<p ng-show="outOfDate" style="font-size:12px;margin-top:6px;color:pink;"><b>WARNING:</b> Please resave this effect to update it to the new save format! It is currently using an outdated version and will break in the 1.0 release when the compatibility layer is removed.</p>
</eos-container>
`,

optionsController: ($scope: EffectScope<EffectParams>) => {
$scope.outOfDate = false;
$scope.repeatStateOptions = Object.freeze({
off: "Off",
track: "Track",
Expand All @@ -49,7 +46,6 @@ export const SpotifyChangeRepeatStateEffect: Firebot.EffectType<EffectParams> =

// Convert old repeatState to new format
if (Array.isArray($scope.effect.repeatState)) {
$scope.outOfDate = true;
$scope.effect.repeatState = $scope.effect.repeatState[0];
}
},
Expand All @@ -69,9 +65,6 @@ export const SpotifyChangeRepeatStateEffect: Firebot.EffectType<EffectParams> =
let repeatState = event.effect.repeatState;

if (Array.isArray(repeatState)) {
chatFeedAlert(
"The `Spotify Premium: Change Repeat Mode` effect has had changes to how it saves data, please edit the command and save it to update it to the new format."
);
repeatState = repeatState[0];
}

Expand Down
4 changes: 2 additions & 2 deletions src/firebot/effects/spotifyFindAndEnqueueTrackEffect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spotify } from "@/main";
import { logger } from "@/utils/firebot";
import { logger } from "@oceanity/firebot-helpers/firebot";
import { trackSummaryFromDetails } from "@/utils/spotify/player/track";
import { getErrorMessage } from "@/utils/string";
import { getErrorMessage } from "@oceanity/firebot-helpers/string";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";

type EffectParams = {
Expand Down
2 changes: 1 addition & 1 deletion src/firebot/effects/spotifySeekToPositionEffect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spotify } from "@/main";
import { getErrorMessage } from "@/utils/string";
import { getErrorMessage } from "@oceanity/firebot-helpers/string";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";

type EffectParams = { seekPosition: number };
Expand Down
2 changes: 1 addition & 1 deletion src/firebot/effects/spotifySkipTrackEffect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spotify } from "@/main";
import { getErrorMessage } from "@/utils/string";
import { getErrorMessage } from "@oceanity/firebot-helpers/string";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";

export enum SpotifySkipTarget {
Expand Down
6 changes: 0 additions & 6 deletions src/firebot/events/spotifyEventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ export const SpotifyEventSource = {
description: "Currently active Spotify Playlist has changed",
cached: false,
},
{
id: "queue-changed",
name: "Spotify Queue Changed",
description: "Currently active Spotify Queue has changed",
cached: false,
},
{
id: "tick",
name: "Spotify Tick",
Expand Down
2 changes: 1 addition & 1 deletion src/firebot/variables/playlist/rawSpotifyPlaylist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spotify } from "@/main";
import { OutputDataType } from "@/shared/variable-constants";
import { objectWalkPath } from "@/utils/object";
import { objectWalkPath } from "@oceanity/firebot-helpers/object";
import { ReplaceVariable } from "@crowbartools/firebot-custom-scripts-types/types/modules/replace-variable-manager";

export const RawSpotifyPlaylistVariable: ReplaceVariable = {
Expand Down
4 changes: 2 additions & 2 deletions src/firebot/variables/playlist/spotifyPlaylist.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { spotify } from "@/main";
import { OutputDataType } from "@/shared/variable-constants";
import { objectWalkPath } from "@/utils/object";
import { objectWalkPath } from "@oceanity/firebot-helpers/object";
import { ReplaceVariable } from "@crowbartools/firebot-custom-scripts-types/types/modules/replace-variable-manager";

export const SpotifyPlaylistVariable: ReplaceVariable = {
definition: {
handle: "spotifyPlaylist",
description:
"Gets a specified field of the currently playing Spotify playlist, or the entire object if just called as $spoitfyPlaylist. See examples for all fields.",
"Gets a specified field of the currently playing Spotify playlist, or the entire object if just called as $spotifyPlaylist. See examples for all fields.",
usage: "spotifyPlaylist[field]",
//@ts-expect-error ts2322
possibleDataOutput: [OutputDataType.OBJECT],
Expand Down
34 changes: 19 additions & 15 deletions src/firebot/variables/queue/rawSpotifyQueue.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { SpotifyService } from "@/utils/spotify";
import { RawSpotifyQueueVariable } from "./rawSpotifyQueue";
import { jest } from "@jest/globals";
import { testQueue, testTrigger } from "@/testData";

// Mocking the entire @/main module to provide the mocked spotify instance
jest.mock("@/main", () => ({
spotify: {
player: {
queue: {},
queue: {
getAsync: jest.fn(),
},
},
},
}));
Expand All @@ -15,35 +18,36 @@ describe("Spotify - Raw Queue Replace Variable", () => {
let spotify: SpotifyService;

beforeEach(() => {
// Access the mocked spotify from the mocked @/main module
spotify = require("@/main").spotify;

// Mock the summary getter on the queue object
Object.defineProperty(spotify.player.queue, "raw", {
get: jest.fn(() => testQueue),
configurable: true,
});
jest
.spyOn(spotify.player.queue, "getAsync")
.mockResolvedValue(testQueue);
});

afterEach(() => {
jest.clearAllMocks();
})

it("returns all details on current queue", async () => {
const response = await RawSpotifyQueueVariable.evaluator(testTrigger);
expect(response).toEqual(testQueue);
});

it("returns empty string when queue is undefined", async () => {
Object.defineProperty(spotify.player.queue, "raw", {
get: jest.fn(() => undefined),
configurable: true,
});
jest
.spyOn(spotify.player.queue, "getAsync")
.mockResolvedValue(null);

const response = await RawSpotifyQueueVariable.evaluator(testTrigger);
expect(response).toEqual("");
});

it("returns empty string when queue is null", async () => {
Object.defineProperty(spotify.player.queue, "raw", {
get: jest.fn(() => null),
configurable: true,
});
jest
.spyOn(spotify.player.queue, "getAsync")
.mockResolvedValue(null);

const response = await RawSpotifyQueueVariable.evaluator(testTrigger);
expect(response).toEqual("");
});
Expand Down
Loading

0 comments on commit 0426672

Please sign in to comment.