Skip to content

Commit

Permalink
Merge pull request #6 from Oceanity/feature/better-class-structure
Browse files Browse the repository at this point in the history
Feature/better class structure
  • Loading branch information
Oceanity authored May 31, 2024
2 parents c103a9c + d11e5f7 commit 57d12b9
Show file tree
Hide file tree
Showing 18 changed files with 704 additions and 506 deletions.
5 changes: 3 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"tabWidth": 2,
"useTabs": false
"tabWidth": 2,
"useTabs": false,
"bracketSameLine": false
}
49 changes: 37 additions & 12 deletions src/firebot/effects/spotifyChangePlaybackStateEffect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Spotify from "@utils/spotify";
import { spotify } from "@/main";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import { integrationId } from "@/main";
import { getErrorMessage } from "@utils/errors";

export enum SpotifyPlaybackState {
Play = "Play",
Expand All @@ -12,7 +12,7 @@ export const spotifyChangePlaybackStateEffect: Firebot.EffectType<{
playState: SpotifyPlaybackState;
}> = {
definition: {
id: `${integrationId}:change-playback-state`,
id: "oceanity-spotify:change-playback-state",
name: "Spotify Premium: Change Playback State",
description: "Changes playback state of active Spotify device",
icon: "fab fa-spotify",
Expand All @@ -25,6 +25,12 @@ export const spotifyChangePlaybackStateEffect: Firebot.EffectType<{
"Will be true if the playback state was changed successfully, false if not.",
defaultName: "playbackStateChanged",
},
{
label: "Error Message",
description:
"If the playback volume was not changed successfully, will contain an error message.",
defaultName: "error",
},
],
},

Expand Down Expand Up @@ -63,15 +69,34 @@ export const spotifyChangePlaybackStateEffect: Firebot.EffectType<{
onTriggerEvent: async (event) => {
const { playState } = event.effect;

const spotifySuccess = await Spotify.changePlaybackStateAsync(
playState ?? SpotifyPlaybackState.Play
);
try {
switch (playState) {
case SpotifyPlaybackState.Play:
await spotify.player.playAsync();
break;
case SpotifyPlaybackState.Pause:
await spotify.player.pauseAsync();
break;
case SpotifyPlaybackState.Toggle:
await spotify.player.playPauseAsync();
break;
}

return {
success: true,
outputs: {
spotifySuccess,
},
};
return {
success: true,
outputs: {
playbackStateChanged: true,
error: null,
},
};
} catch (error) {
return {
success: false,
outputs: {
playbackStateChanged: false,
error: getErrorMessage(error),
},
};
}
},
};
37 changes: 27 additions & 10 deletions src/firebot/effects/spotifyChangePlaybackVolumeEffect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Spotify from "@utils/spotify";
import { spotify } from "@/main";
import { getErrorMessage } from "@utils/errors";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import { integrationId } from "@/main";

export const spotifyChangePlaybackVolumeEffect: Firebot.EffectType<{
volume: number;
}> = {
definition: {
id: `${integrationId}:change-playback-volume`,
id: "oceanity-spotify:change-playback-volume",
name: "Spotify Premium: Change Playback Volume",
description: "Changes playback volume of active Spotify device",
icon: "fab fa-spotify",
Expand All @@ -19,6 +19,12 @@ export const spotifyChangePlaybackVolumeEffect: Firebot.EffectType<{
"Will be true if the playback volume was changed successfully, false if not.",
defaultName: "volumeChanged",
},
{
label: "Error Message",
description:
"If the playback volume was not changed successfully, will contain an error message.",
defaultName: "error",
},
],
},

Expand Down Expand Up @@ -49,13 +55,24 @@ export const spotifyChangePlaybackVolumeEffect: Firebot.EffectType<{
onTriggerEvent: async (event) => {
const { volume } = event.effect;

const volumeChanged = await Spotify.changePlaybackVolumeAsync(volume);
try {
await spotify.player.setVolumeAsync(volume);

return {
success: true,
outputs: {
volumeChanged,
},
};
return {
success: true,
outputs: {
volumeChanged: true,
error: null,
},
};
} catch (error) {
return {
success: false,
outputs: {
volumeChanged: false,
error: getErrorMessage(error),
},
};
}
},
};
58 changes: 34 additions & 24 deletions src/firebot/effects/spotifyChangeRepeatStateEffect.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import Spotify from "@utils/spotify";
import { spotify } from "@/main";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import { integrationId } from "@/main";

export enum SpotifyRepeatState {
Track = "track",
Context = "context",
Off = "off",
}
import { getErrorMessage } from "@/utils/errors";

export const spotifyChangeRepeatStateEffect: Firebot.EffectType<{
repeatState: SpotifyRepeatState;
repeatState: [SpotifyRepeatState, string];
}> = {
definition: {
id: `${integrationId}:change-playback-state`,
id: "oceanity-spotify:change-repeat-state",
name: "Spotify Premium: Change Repeat Mode",
description: "Changes repeat mode of active Spotify device",
icon: "fab fa-spotify",
Expand All @@ -25,24 +19,30 @@ export const spotifyChangeRepeatStateEffect: Firebot.EffectType<{
"Will be true if the repeat mode was changed successfully, false if not.",
defaultName: "repeatModeChanged",
},
{
label: "Error Message",
description:
"If the repeat mode was not changed successfully, will contain an error message.",
defaultName: "error",
},
],
},

optionsTemplate: `
<eos-container header="Looping Mode">
<eos-container header="Repeat Mode">
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="list-effect-type">{{effect.repeatState ? effect.repeatState : 'Looping Mode...'}}</span> <span class="caret"></span>
<span class="list-effect-type">{{effect.repeatState ? effect.repeatState[1] : 'Repeat Mode...'}}</span> <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li ng-click="effect.repeatState = 'off'">
<li ng-click="effect.repeatState = ['off', 'Off']">
<a href>Off</a>
</li>
<li ng-click="effect.repeatState = 'track'">
<li ng-click="effect.repeatState = ['track', 'Track']">
<a href>Track</a>
</li>
<li ng-click="effect.repeatState = 'context'">
<a href>Context</a>
<li ng-click="effect.repeatState = ['context', 'All']">
<a href>All</a>
</li>
</ul>
</div>
Expand All @@ -63,15 +63,25 @@ export const spotifyChangeRepeatStateEffect: Firebot.EffectType<{
},

onTriggerEvent: async (event) => {
const { repeatState } = event.effect;
try {
const { repeatState } = event.effect;

const repeatModeChanged = await Spotify.changeRepeatStateAsync(repeatState);
await spotify.player.setRepeatStateAsync(repeatState[0]);

return {
success: true,
outputs: {
repeatModeChanged,
},
};
return {
success: true,
outputs: {
repeatModeChanged: true,
},
};
} catch (error) {
return {
success: false,
outputs: {
repeatModeChanged: false,
error: getErrorMessage(error),
},
};
}
},
};
45 changes: 30 additions & 15 deletions src/firebot/effects/spotifyFindAndEnqueueTrackEffect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spotify } from "@/main";
import { getErrorMessage } from "@/utils/errors";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import Spotify from "@utils/spotify";
import { integrationId } from "@/main";

export const spotifyFindAndEnqueueTrackEffect: Firebot.EffectType<{
query: string;
Expand All @@ -9,7 +9,7 @@ export const spotifyFindAndEnqueueTrackEffect: Firebot.EffectType<{
allowDuplicates: boolean;
}> = {
definition: {
id: `${integrationId}:request-song`,
id: "oceanity-spotify:request-song",
name: "Spotify Premium: Find and Enqueue Track",
description: "Searches for a track to add to your Spotify queue",
icon: "fab fa-spotify",
Expand Down Expand Up @@ -64,19 +64,34 @@ export const spotifyFindAndEnqueueTrackEffect: Firebot.EffectType<{
},

onTriggerEvent: async (event) => {
const encodedQuery = encodeURIComponent(event.effect.query);
const { query, queuedBy, allowDuplicates } = event.effect;

const { success, data } = await Spotify.findAndEnqueueTrackAsync(
encodedQuery,
event.effect.allowDuplicates
);
try {
const track = (await spotify.searchAsync(query, "track")).tracks.items[0];

return {
success: true,
outputs: {
trackWasEnqueued: success,
spotifyResponse: data,
},
};
if (!track) throw new Error("Track not found");

await spotify.player.queue.pushAsync(track.uri, allowDuplicates);

track.queue_position = await spotify.player.queue.findIndexAsync(
track.uri
);

return {
success: true,
outputs: {
trackWasEnqueued: true,
spotifyResponse: track,
},
};
} catch (error) {
return {
success: false,
outputs: {
trackWasEnqueued: false,
error: getErrorMessage(error),
},
};
}
},
};
57 changes: 57 additions & 0 deletions src/firebot/effects/spotifyGetCurrentlyPlayingTrackEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { spotify } from "@/main";
import { Firebot } from "@crowbartools/firebot-custom-scripts-types";
import { getErrorMessage } from "@/utils/errors";

export const spotifyGetCurrentlyPlayingEffect: Firebot.EffectType<{}> = {
definition: {
id: "oceanity-spotify:get-currently-playing-track",
name: "Spotify: Get Now Playing",
description:
"Gets all details of the currently playing track on active Spotify device",
icon: "fab fa-spotify",
categories: ["integrations"],
//@ts-expect-error ts2353
outputs: [
{
label: "Now Playing",
description:
"Will be currently playing track details if Spotify is playing, null if not.",
defaultName: "nowPlaying",
},
{
label: "Error Message",
description:
"If getting currently playing track failed, will contain an error message.",
defaultName: "error",
},
],
},

optionsTemplate: ``,

// @ts-expect-error ts6133: Variables must be named consistently
optionsController: ($scope: any, backendCommunicator: any, $q: any) => {},

optionsValidator: () => [],

onTriggerEvent: async () => {
try {
const track = await spotify.player.getCurrentlyPlaying();

return {
success: true,
outputs: {
nowPlaying: track.item,
},
};
} catch (error) {
return {
success: false,
outputs: {
nowPlaying: null,
error: getErrorMessage(error),
},
};
}
},
};
Loading

0 comments on commit 57d12b9

Please sign in to comment.