Completed Merge StatsFM> LastFM + Streaming status added. (#201)

* Update index.tsx

* Add Back AlwaysHideArt

---------

Co-authored-by: thororen <78185467+thororen1234@users.noreply.github.com>
This commit is contained in:
Crxaw 2025-04-03 00:54:47 +01:00 committed by GitHub
parent a1cc1bb826
commit 3a42b0ebb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,7 +19,7 @@
import { definePluginSettings } from "@api/Settings";
import { getUserSettingLazy } from "@api/UserSettings";
import { Link } from "@components/Link";
import { Devs } from "@utils/constants";
import { Devs, EquicordDevs } from "@utils/constants";
import { Logger } from "@utils/Logger";
import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy } from "@webpack";
@ -44,10 +44,12 @@ interface Activity {
timestamps?: {
start?: number;
};
assets?: ActivityAssets;
assets?: ActivityAssets; // LastFM
assets2?: ActivityAssets; // StatsFM
buttons?: Array<string>;
name: string;
application_id: string;
application_id: string; // LastFM
application_id2: string; // StatsFM
metadata?: {
button_urls?: Array<string>;
};
@ -67,39 +69,91 @@ interface TrackData {
const enum ActivityType {
PLAYING = 0,
LISTENING = 2,
STREAMING = 1, // Added Streaming ig as a alt to listening
}
const enum ActivityFlag {
INSTANCE = 1 << 0,
}
const enum NameFormat {
const enum NameFormat { // (2) = Statsfm config
StatusName = "status-name",
ArtistFirst = "artist-first",
SongFirst = "song-first",
ArtistOnly = "artist",
SongOnly = "song",
AlbumName = "album"
AlbumName = "album",
AlbumName2 = "album2", // ^
ArtistOnly2 = "artist2", // ^
ArtistFirst2 = "ArtistFirst2", // ^
SongFirst2 = "SongFirst2", // ^
SongOnly2 = "SongOnly2", // ^
}
interface Albums {
id: number;
name: string;
image: string;
}
interface Artists {
id: number;
name: string;
image: string;
}
interface ExternalIds {
spotify: string[];
appleMusic: string[];
}
interface Track {
albums: Albums[];
artists: Artists[];
durationMs: number;
explicit: boolean;
externalIds: ExternalIds;
id: number;
name: string;
spotifyPopularity: number;
spotifyPreview: string;
appleMusicPreview: string;
}
interface Item {
date: string;
isPlaying: boolean;
progressMs: number;
deviceName: string;
track: Track;
platform: string;
}
interface SFMR {
item: Item;
}
const ShowCurrentGame = getUserSettingLazy<boolean>("status", "showCurrentGame")!;
const applicationId = "1108588077900898414";
const applicationId = "1108588077900898414"; // LastFM Appid
const applicationId2 = "1325126169179197500"; // StatsFM Appid
const placeholderId = "2a96cbd8b46e442fc41c2b86b821562f";
const logger = new Logger("LastFMRichPresence");
const logger = new Logger("LastFMRichPresence + StatsfmFMRichPresence");
const PresenceStore = findByPropsLazy("getLocalPresence");
async function getApplicationAsset(key: string): Promise<string> {
return (await ApplicationAssetUtils.fetchAssetIds(applicationId, [key]))[0];
}
async function getApplicationAsset2(key: string): Promise<string> {
return (await ApplicationAssetUtils.fetchAssetIds(applicationId2, [key]))[0];
}
function setActivity(activity: Activity | null) {
FluxDispatcher.dispatch({
type: "LOCAL_ACTIVITY_UPDATE",
activity,
socketId: "LastFM",
socketId: "LastFM + Statsfm",
});
}
@ -108,6 +162,10 @@ const settings = definePluginSettings({
description: "last.fm username",
type: OptionType.STRING,
},
statsfmusername: {
description: "stats.fm username",
type: OptionType.STRING,
},
apiKey: {
description: "last.fm api key",
type: OptionType.STRING,
@ -117,26 +175,51 @@ const settings = definePluginSettings({
type: OptionType.BOOLEAN,
default: false,
},
shareUsernameStatsfm: {
description: "show link to stats.fm profile",
type: OptionType.BOOLEAN,
default: false,
},
shareSong: {
description: "show link to song on last.fm",
type: OptionType.BOOLEAN,
default: true,
},
StatsfmSong: {
description: "show link to song on stats.fm",
type: OptionType.BOOLEAN,
default: true,
},
hideWithSpotify: {
description: "hide last.fm presence if spotify is running",
type: OptionType.BOOLEAN,
default: true,
},
hidewithSpotifySFM: {
description: "hide stats.fm presence if spotify is running",
type: OptionType.BOOLEAN,
default: true,
},
hideWithActivity: {
description: "Hide Last.fm presence if you have any other presence",
type: OptionType.BOOLEAN,
default: false,
},
hideStatsfmWithExternalRPC: {
description: "Hide Stats.fm presence if you have any other presence",
type: OptionType.BOOLEAN,
default: false,
},
enableGameActivity: {
description: "Enable game activity for last.fm",
type: OptionType.BOOLEAN,
default: false,
},
enableGameActivitySFM: {
description: "Enable game activity for stats.fm",
type: OptionType.BOOLEAN,
default: false,
},
statusName: {
description: "custom status text",
type: OptionType.STRING,
@ -178,6 +261,11 @@ const settings = definePluginSettings({
type: OptionType.BOOLEAN,
default: false,
},
useStreamingStatus: {
description: 'show "Streaming" status instead of "Playing"',
type: OptionType.BOOLEAN,
default: false,
},
missingArt: {
description: "When album or album art is missing",
type: OptionType.SELECT,
@ -187,6 +275,11 @@ const settings = definePluginSettings({
value: "lastfmLogo",
default: true
},
{
label: "Use Large Stats.fm logo",
value: "StatsFMLogo",
default: false
},
{
label: "Use generic placeholder",
value: "placeholder"
@ -198,6 +291,11 @@ const settings = definePluginSettings({
type: OptionType.BOOLEAN,
default: true,
},
showStatsFMLogo: {
description: "show the Stats.fm logo by the album cover",
type: OptionType.BOOLEAN,
default: false,
},
alwaysHideArt: {
description: "Disable downloading album art",
type: OptionType.BOOLEAN,
@ -207,8 +305,8 @@ const settings = definePluginSettings({
export default definePlugin({
name: "LastFMRichPresence",
description: "Little plugin for Last.fm rich presence",
authors: [Devs.dzshn, Devs.RuiNtD, Devs.blahajZip, Devs.archeruwu],
description: "Little plugin for Last.fm rich presence + Stats.fm rich presence",
authors: [Devs.dzshn, Devs.RuiNtD, Devs.blahajZip, Devs.archeruwu, EquicordDevs.Crxa],
settingsAboutComponent: () => (
<>
@ -223,6 +321,12 @@ export default definePlugin({
And copy the API key (not the shared secret!)
</Forms.FormText>
<Forms.FormTitle tag="h3">How to get Stats.fm Presence!</Forms.FormTitle>
<Forms.FormText>
STATSFM ONLY:
If you want to use stats.fm, you will need an account linked @ <Link href="https://stats.fm/login"></Link> and have your listening history public.
</Forms.FormText>
</>
),
@ -236,7 +340,7 @@ export default definePlugin({
stop() {
clearInterval(this.updateInterval);
},
// Last.fm Fetching
async fetchTrackData(): Promise<TrackData | null> {
if (!settings.store.username || !settings.store.apiKey)
return null;
@ -278,6 +382,33 @@ export default definePlugin({
return null;
}
},
// Stats.fm Fetching
async fetchTrackDataStatsfm(): Promise<TrackData | null> {
if (!settings.store.statsfmusername)
return null;
try {
const res2 = await fetch(`https://api.stats.fm/api/v1/user/${settings.store.statsfmusername}/recent`);
if (!res2.ok) throw `${res2.status} ${res2.statusText}`;
const json = await res2.json() as SFMR;
if (!json.item) {
logger.error("Error from Stats.fm API"), json;
return null;
}
const trackData2 = json.item.track;
if (!trackData2) return null;
return {
name: trackData2.name || "Unknown",
album: trackData2.albums.map(a => a.name).join(", ") || "Unknown",
artist: trackData2.artists[0].name ?? "Unknown",
url: `https://stats.fm/track/${trackData2.id}`,
imageUrl: trackData2.albums[0].image
};
} catch (e) {
logger.error("Failed to query Stats.fm API, Report to Equicord Discord. https://discord.gg/equicord", e);
return null;
}
},
async updatePresence() {
setActivity(await this.getActivity());
@ -293,13 +424,14 @@ export default definePlugin({
async getActivity(): Promise<Activity | null> {
if (settings.store.hideWithActivity) {
if (PresenceStore.getActivities().some(a => a.application_id !== applicationId)) {
if (PresenceStore.getActivities().some(a => a.application_id !== applicationId && (!a.application_id2 || a.application_id2 !== applicationId2)))
return null;
{
}
}
if (settings.store.hideWithSpotify) {
if (PresenceStore.getActivities().some(a => a.type === ActivityType.LISTENING && a.application_id !== applicationId)) {
if (PresenceStore.getActivities().some(a => (a.type === ActivityType.LISTENING || a.type === ActivityType.STREAMING) && a.application_id !== applicationId)) {
// there is already music status because of Spotify or richerCider (probably more)
return null;
}
@ -326,7 +458,21 @@ export default definePlugin({
large_image: await getApplicationAsset("lastfm-large"),
large_text: trackData.album || undefined,
};
// Stats.fm image stuff
const largeImage2 = this.getLargeImage(trackData);
const assets2: ActivityAssets = largeImage2 ?
{
large_image: await getApplicationAsset2(largeImage2),
large_text: trackData.album || undefined,
...(settings.store.showStatsFMLogo && {
small_image: await getApplicationAsset2("statsfm-large"),
small_text: "Stats.fm"
}),
} : {
large_image: await getApplicationAsset2("statsfm-large"),
large_text: trackData.album || undefined,
};
const trackData2 = await this.fetchTrackDataStatsfm();
const buttons: ActivityButton[] = [];
if (settings.store.shareUsername)
@ -334,14 +480,24 @@ export default definePlugin({
label: "Last.fm Profile",
url: `https://www.last.fm/user/${settings.store.username}`,
});
// Stats.fm settings
if (settings.store.shareUsernameStatsfm)
buttons.push({
label: "Stats.fm Profile",
url: `https://stats.fm/user/${settings.store.statsfmusername}`,
});
if (settings.store.StatsfmSong)
buttons.push({
label: "View Song",
url: trackData.url,
});
if (settings.store.shareSong)
buttons.push({
label: "View Song",
url: trackData.url,
});
const statusName = (() => {
const statusName = (() => { // Last.FM satus stuff dont touch
switch (settings.store.nameFormat) {
case NameFormat.ArtistFirst:
return trackData.artist + " - " + trackData.name;
@ -353,6 +509,17 @@ export default definePlugin({
return trackData.name;
case NameFormat.AlbumName:
return trackData.album || settings.store.statusName;
// Stats.fm Status stuff
case NameFormat.ArtistFirst2: // hoping my code works
return trackData2?.artist ? `${trackData2.artist} - ${trackData2.name}` : settings.store.statusName;
case NameFormat.SongFirst2:
return trackData2?.name ? `${trackData2.name} - ${trackData2.artist}` : settings.store.statusName;
case NameFormat.ArtistOnly2:
return trackData2?.artist ?? settings.store.statusName;
case NameFormat.SongOnly2:
return trackData2?.name ?? settings.store.statusName;
case NameFormat.AlbumName2:
return trackData2?.album ?? settings.store.statusName;
default:
return settings.store.statusName;
}
@ -360,18 +527,19 @@ export default definePlugin({
return {
application_id: applicationId,
application_id2: applicationId2, // StatsFM Appid
name: statusName,
details: trackData.name,
state: trackData.artist,
assets,
assets2, // StatsFM assets
buttons: buttons.length ? buttons.map(v => v.label) : undefined,
metadata: {
button_urls: buttons.map(v => v.url),
},
type: settings.store.useListeningStatus ? ActivityType.LISTENING : ActivityType.PLAYING,
type: settings.store.useStreamingStatus ? ActivityType.STREAMING : settings.store.useListeningStatus ? ActivityType.LISTENING : ActivityType.PLAYING,
flags: ActivityFlag.INSTANCE,
};
}