2021-04-04 14:29:20 +00:00
// Also set Powercord global var stuff here since entities import is needed to use Plugin (which every PC plugin uses)
2021-04-05 09:31:08 +00:00
const sendMessage = goosemodScope . webpackModules . findByProps ( 'sendMessage' , 'receiveMessage' ) . sendMessage ;
const getChannelId = goosemod . webpackModules . findByProps ( 'getChannelId' ) . getChannelId ;
2021-04-04 18:00:26 +00:00
export const powercord = {
2021-04-04 14:29:20 +00:00
api : {
commands : {
2021-04-04 14:57:28 +00:00
registerCommand : ( { command , alias , description , usage , executor } ) => {
// TODO: implement alias
2021-04-04 14:29:20 +00:00
goosemodScope . patcher . commands . add ( command , description ,
2021-04-04 21:17:50 +00:00
async ( { args : [ { text } ] } ) => {
const out = await executor ( text . split ( ' ' ) ) ; // Run original executor func (await incase it's an async function)
2021-04-05 09:31:08 +00:00
if ( ! out . send ) {
goosemodScope . patcher . internalMessage ( out . result ) ; // PC impl. sends internal message when out.send === false, so we also do the same via our previous Patcher API function
return ;
}
// When send is true, we send it as a message via sendMessage
sendMessage ( getChannelId ( ) , {
content : out . result ,
2021-04-04 20:30:43 +00:00
2021-04-05 09:31:08 +00:00
tts : false ,
invalidEmojis : [ ] ,
validNonShortcutEmojis : [ ]
} ) ;
2021-04-04 14:29:20 +00:00
} , [
{ type : 3 , required : false , name : 'args' , description : 'Arguments for PC command' } // Argument for any string for compat. with PC's classical commands
2021-04-04 16:16:56 +00:00
] ) ;
2021-04-04 14:30:48 +00:00
} ,
unregisterCommand : ( command ) => {
goosemodScope . patcher . commands . remove ( command ) ;
2021-04-04 14:29:20 +00:00
}
2021-04-04 14:37:08 +00:00
} ,
notices : {
sendToast : ( _id , { header , content , type , buttons } ) => {
// TODO: implement full toast in future instead of just small current GM toast
goosemodScope . showToast ( content ) ;
}
2021-04-06 16:21:58 +00:00
} ,
settings : {
registerSettings : ( id , { label , render , category } ) => {
const { React } = goosemodScope . webpackModules . common ;
const SettingsView = goosemodScope . webpackModules . findByDisplayName ( 'SettingsView' ) ;
const FormTitle = goosemodScope . webpackModules . findByDisplayName ( 'FormTitle' ) ;
const FormSection = goosemodScope . webpackModules . findByDisplayName ( 'FormSection' ) ;
goosemodScope . patcher . inject ( id , SettingsView . prototype , 'getPredicateSections' , ( _ , sections ) => {
if ( ! sections . find ( c => c . section === 'changelog' ) ) return sections ;
const dividers = sections . filter ( c => c . section === 'DIVIDER' ) ;
const finalLabel = typeof label === 'function' ? label ( ) : label ;
sections . splice ( sections . indexOf ( dividers [ dividers . length - 2 ] ) - 2 , 0 ,
{
section : finalLabel ,
label : finalLabel ,
predicate : ( ) => { } ,
element : ( ) => React . createElement ( FormSection , { } ,
React . createElement ( FormTitle , { tag : 'h2' } , finalLabel ) ,
render ( {
getSetting : settingStores [ category ] . getSetting ,
updateSetting : settingStores [ category ] . updateSetting
} )
)
}
) ;
return sections ;
} ) ;
} ,
unregisterSettings : ( id ) => {
goosemodScope . patcher . uninject ( id ) ;
}
2021-04-04 14:29:20 +00:00
}
}
} ;
2021-04-06 16:21:58 +00:00
const settingStores = { } ;
class SimpleStore {
constructor ( ) {
this . store = { } ;
}
getSetting = ( key , defaultValue ) => {
return this . store [ key ] ? ? defaultValue ;
}
updateSetting = ( key , value ) => {
this . store [ key ] = value ;
}
}
2021-04-04 12:50:12 +00:00
export class Plugin {
constructor ( ) {
2021-04-04 12:55:38 +00:00
}
2021-04-06 16:21:58 +00:00
loadStylesheet ( path ) {
const url = ` https://raw.githubusercontent.com/ ${ this . github . repo } /main/ ${ path } ` ;
return url ;
}
delayedConstructor ( ) {
if ( this . delayedConstructed ) return ;
this . delayedConstructed = true ;
settingStores [ this . entityID ] = new SimpleStore ( ) ;
}
get entityID ( ) {
return this . name ;
}
get settings ( ) {
return settingStores [ this . entityID ] ;
}
2021-04-04 12:55:38 +00:00
get goosemodHandlers ( ) {
return {
2021-04-06 16:21:58 +00:00
onImport : ( ) => {
this . delayedConstructor ( ) ;
this . startPlugin . bind ( this ) ( ) ;
} ,
2021-04-05 10:15:50 +00:00
onRemove : this . pluginWillUnload . bind ( this )
2021-04-04 12:55:38 +00:00
} ;
2021-04-04 12:50:12 +00:00
}
2021-04-06 16:21:58 +00:00
}