Add /extension-exists and /extension-state

1a6f0c0922b263dbdcac7904996cfe4fce47805a

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +69 -2Showing whitespace changes
public/scripts/extensions.js+69 -2
@@ -1056,7 +1056,7 @@ export async function openThirdPartyExtensionMenu(suggestUrl = '') {
10561056function getExtensionActionCallback(action) {
10571057 return async (args, extensionName) => {
10581058 if (args?.reload instanceof SlashCommandClosure) throw new Error('\'reload\' argument cannot be a closure.');
10591059 if (typeof extensionName !== 'string') throw new Error('Extension name doesmust onlybe supporta string. Closures or arrays are not allowed.');
10601060 if (!extensionName) {
10611061 toastr.warning(`Extension name must be provided as an argument to ${action} this extension.`);
10621062 return '';
@@ -1184,7 +1184,7 @@ function registerExtensionSlashCommands() {
11841184 name: 'extension-toggle',
11851185 callback: async (args, extensionName) => {
11861186 if (args?.state instanceof SlashCommandClosure) throw new Error('\'state\' argument cannot be a closure.');
11871187 if (typeof extensionName !== 'string') throw new Error('Extension name doesmust onlybe supporta string. Closures or arrays are not allowed.');
11881188
11891189 const action = isTrueBoolean(args?.state) ? 'enable' :
11901190 isFalseBoolean(args?.state) ? 'disable' :
@@ -1238,6 +1238,73 @@ function registerExtensionSlashCommands() {
12381238 </div>
12391239 `,
12401240 }));
1241+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
1242+ name: 'extension-state',
1243+ callback: async (_, extensionName) => {
1244+ if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.');
1245+ const internalExtensionName = extensionNames.find(x => equalsIgnoreCaseAndAccents(x, extensionName));
1246+ if (!internalExtensionName) {
1247+ toastr.warning(`Extension ${extensionName} does not exist.`);
1248+ return '';
1249+ }
1250+
1251+ const isEnabled = !extension_settings.disabledExtensions.includes(internalExtensionName);
1252+ return String(isEnabled);
1253+ },
1254+ unnamedArgumentList: [
1255+ SlashCommandArgument.fromProps({
1256+ description: 'Extension name',
1257+ typeList: [ARGUMENT_TYPE.STRING],
1258+ isRequired: true,
1259+ enumProvider: () => extensionNames.map(name => new SlashCommandEnumValue(name)),
1260+ forceEnum: true,
1261+ }),
1262+ ],
1263+ helpString: `
1264+ <div>
1265+ Returns the state of a specified extension (true if enabled, false if disabled).
1266+ </div>
1267+ <div>
1268+ <strong>Example:</strong>
1269+ <ul>
1270+ <li>
1271+ <pre><code class="language-stscript">/extension-state Summarize</code></pre>
1272+ </li>
1273+ </ul>
1274+ </div>
1275+ `,
1276+ }));
1277+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
1278+ name: 'extension-exists',
1279+ aliases: ['extension-installed'],
1280+ callback: async (_, extensionName) => {
1281+ if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.');
1282+ const exists = extensionNames.find(x => equalsIgnoreCaseAndAccents(x, extensionName)) !== undefined;
1283+ return exists ? 'true' : 'false';
1284+ },
1285+ unnamedArgumentList: [
1286+ SlashCommandArgument.fromProps({
1287+ description: 'Extension name',
1288+ typeList: [ARGUMENT_TYPE.STRING],
1289+ isRequired: true,
1290+ enumProvider: () => extensionNames.map(name => new SlashCommandEnumValue(name)),
1291+ forceEnum: true,
1292+ }),
1293+ ],
1294+ helpString: `
1295+ <div>
1296+ Checks if a specified extension exists.
1297+ </div>
1298+ <div>
1299+ <strong>Example:</strong>
1300+ <ul>
1301+ <li>
1302+ <pre><code class="language-stscript">/extension-exists LALib</code></pre>
1303+ </li>
1304+ </ul>
1305+ </div>
1306+ `,
1307+ }));
12411308
12421309 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
12431310 name: 'reload-page',