vd-plugins/plugins/Last.fm/src/index.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-02-12 20:31:51 +08:00
import { plugin } from "@vendetta";
2023-02-11 23:09:54 +08:00
import { findByProps, findByStoreName } from '@vendetta/metro';
import { FluxDispatcher } from '@vendetta/metro/common';
2023-02-12 20:31:51 +08:00
import Settings from "./Settings";
import { flush, initialize, patchActivityType } from "./utils";
2023-02-11 23:09:54 +08:00
2023-02-12 20:31:51 +08:00
export const global = {} as {
stopped?: Boolean,
lastActivity?: Activity,
updateInterval?: NodeJS.Timer,
lastTrackUrl?: string,
activityTypeUnpatch?: () => boolean
2023-02-11 23:09:54 +08:00
}
2023-02-12 18:18:46 +08:00
export const currentSettings = { ...plugin.storage } as PluginSettings;
2023-02-11 23:09:54 +08:00
// Discord modules that we need
2023-02-12 20:31:51 +08:00
export const UserStore = findByStoreName("UserStore")
export const SET_ACTIVITY = findByProps('SET_ACTIVITY').SET_ACTIVITY;
2023-02-11 23:09:54 +08:00
2023-02-12 20:31:51 +08:00
export const verboseLog = (...message: any) => currentSettings.verboseLogging && console.log(...message);
2023-02-11 23:09:54 +08:00
// Plugin entry point
export default {
onLoad: () => {
console.log("Starting last.fm plugin..");
2023-02-12 20:31:51 +08:00
global.activityTypeUnpatch = patchActivityType();
global.stopped = false;
2023-02-11 23:09:54 +08:00
if (UserStore.getCurrentUser()) {
console.log("User is already logged in, initializing...");
initialize().catch(console.error);
}
FluxDispatcher.subscribe("CONNECTION_OPEN", () => {
2023-02-12 20:31:51 +08:00
initialize().catch(console.error);
2023-02-11 23:09:54 +08:00
});
},
onUnload: () => {
console.log("Stopping last.fm...");
2023-02-12 20:31:51 +08:00
global.stopped = true;
2023-02-11 23:09:54 +08:00
2023-02-12 18:18:46 +08:00
flush();
2023-02-12 20:31:51 +08:00
global.activityTypeUnpatch?.();
global.activityTypeUnpatch = null;
2023-02-12 16:33:21 +08:00
},
settings: Settings
2023-02-11 23:09:54 +08:00
}