summaryrefslogtreecommitdiff
path: root/packages/client/src/scripts/aiscript/api.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
commit0e4a111f81cceed275d9bec2695f6e401fb654d8 (patch)
tree40874799472fa07416f17b50a398ac33b7771905 /packages/client/src/scripts/aiscript/api.ts
parentupdate deps (diff)
downloadsharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.gz
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.bz2
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.zip
refactoring
Resolve #7779
Diffstat (limited to 'packages/client/src/scripts/aiscript/api.ts')
-rw-r--r--packages/client/src/scripts/aiscript/api.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/packages/client/src/scripts/aiscript/api.ts b/packages/client/src/scripts/aiscript/api.ts
new file mode 100644
index 0000000000..20c15d809e
--- /dev/null
+++ b/packages/client/src/scripts/aiscript/api.ts
@@ -0,0 +1,44 @@
+import { utils, values } from '@syuilo/aiscript';
+import * as os from '@/os';
+import { $i } from '@/account';
+
+export function createAiScriptEnv(opts) {
+ let apiRequests = 0;
+ return {
+ 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,
+ 'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => {
+ await os.dialog({
+ type: type ? type.value : 'info',
+ title: title.value,
+ text: text.value,
+ });
+ }),
+ 'Mk:confirm': values.FN_NATIVE(async ([title, text, type]) => {
+ const confirm = await os.dialog({
+ type: type ? type.value : 'question',
+ showCancelButton: true,
+ title: title.value,
+ text: text.value,
+ });
+ return confirm.canceled ? values.FALSE : values.TRUE;
+ }),
+ 'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
+ if (token) utils.assertString(token);
+ apiRequests++;
+ if (apiRequests > 16) return values.NULL;
+ const res = await os.api(ep.value, utils.valToJs(param), token ? token.value : (opts.token || null));
+ return utils.jsToVal(res);
+ }),
+ '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)));
+ }),
+ };
+}