2020-04-12 05:38:19 -05:00
|
|
|
import { utils, values } from '@syuilo/aiscript';
|
2021-03-23 03:30:14 -05:00
|
|
|
import * as os from '@client/os';
|
|
|
|
import { $i } from '@client/account';
|
2020-04-12 05:38:19 -05:00
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
export function createAiScriptEnv(opts) {
|
2020-04-12 13:23:23 -05:00
|
|
|
let apiRequests = 0;
|
2020-04-12 05:38:19 -05:00
|
|
|
return {
|
2020-12-18 19:55:52 -06:00
|
|
|
USER_ID: $i ? values.STR($i.id) : values.NULL,
|
|
|
|
USER_NAME: $i ? values.STR($i.name) : values.NULL,
|
|
|
|
USER_USERNAME: $i ? values.STR($i.username) : values.NULL,
|
2020-04-12 05:38:19 -05:00
|
|
|
'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => {
|
2020-10-17 06:12:00 -05:00
|
|
|
await os.dialog({
|
2020-04-12 05:38:19 -05:00
|
|
|
type: type ? type.value : 'info',
|
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
|
|
|
}),
|
2020-07-28 05:02:28 -05:00
|
|
|
'Mk:confirm': values.FN_NATIVE(async ([title, text, type]) => {
|
2020-10-17 06:12:00 -05:00
|
|
|
const confirm = await os.dialog({
|
2020-07-28 05:02:28 -05:00
|
|
|
type: type ? type.value : 'question',
|
2020-04-12 05:38:19 -05:00
|
|
|
showCancelButton: true,
|
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
2020-05-10 03:25:16 -05:00
|
|
|
return confirm.canceled ? values.FALSE : values.TRUE;
|
2020-04-12 05:38:19 -05:00
|
|
|
}),
|
|
|
|
'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
|
2020-05-15 06:53:24 -05:00
|
|
|
if (token) utils.assertString(token);
|
2020-04-12 13:23:23 -05:00
|
|
|
apiRequests++;
|
|
|
|
if (apiRequests > 16) return values.NULL;
|
2020-10-17 06:12:00 -05:00
|
|
|
const res = await os.api(ep.value, utils.valToJs(param), token ? token.value : (opts.token || null));
|
2020-04-12 05:38:19 -05:00
|
|
|
return utils.jsToVal(res);
|
|
|
|
}),
|
2020-04-12 05:57:18 -05:00
|
|
|
'Mk:save': values.FN_NATIVE(([key, value]) => {
|
|
|
|
utils.assertString(key);
|
|
|
|
localStorage.setItem('aiscript:' + opts.storageKey + ':' + key.value, JSON.stringify(utils.valToJs(value)));
|
|
|
|
return values.NULL;
|
|
|
|
}),
|
|
|
|
'Mk:load': values.FN_NATIVE(([key]) => {
|
|
|
|
utils.assertString(key);
|
|
|
|
return utils.jsToVal(JSON.parse(localStorage.getItem('aiscript:' + opts.storageKey + ':' + key.value)));
|
|
|
|
}),
|
2020-04-12 05:38:19 -05:00
|
|
|
};
|
|
|
|
}
|