@bunker-community/plugin-sdk 0.0.0-stage → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API_DOC.md +568 -0
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/manifest.d.ts +14 -0
- package/dist/manifest.js +84 -0
- package/dist/remote.d.ts +50 -0
- package/dist/remote.js +288 -0
- package/dist/types.d.ts +291 -0
- package/dist/types.js +45 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +46 -4
package/dist/manifest.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/* bunker-plugin.json dogrulamasi (bagimliliksiz; calisma ortami ve gelistirici araci ayni kodu kullanir) */
|
|
2
|
+
import { PERMISSIONS } from "./types.js";
|
|
3
|
+
export const MANIFEST_FILE = "bunker-plugin.json";
|
|
4
|
+
/** 2-28 karakter: bot kimligi "BunkerPluginBot.<id>" en fazla 44 karakter olsun */
|
|
5
|
+
export const ID_RE = /^[a-z0-9](?:[a-z0-9-]{0,26}[a-z0-9])$/;
|
|
6
|
+
const VERSION_RE = /^\d+\.\d+\.\d+(?:-[\w.]+)?$/;
|
|
7
|
+
const SDK_RANGE_RE = /^\d+(?:\.\d+)?$/;
|
|
8
|
+
export function validateManifest(raw) {
|
|
9
|
+
const errors = [];
|
|
10
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
11
|
+
return { ok: false, errors: ["manifest bir JSON nesnesi olmalı"] };
|
|
12
|
+
const o = raw;
|
|
13
|
+
const str = (k, max, required = true) => {
|
|
14
|
+
const v = o[k];
|
|
15
|
+
if (v === undefined && !required)
|
|
16
|
+
return undefined;
|
|
17
|
+
if (typeof v !== "string" || !v.trim()) {
|
|
18
|
+
errors.push(`${k}: metin olmalı`);
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
if (v.length > max)
|
|
22
|
+
errors.push(`${k}: en fazla ${max} karakter`);
|
|
23
|
+
return v.trim();
|
|
24
|
+
};
|
|
25
|
+
const id = str("id", 28);
|
|
26
|
+
if (id && !ID_RE.test(id))
|
|
27
|
+
errors.push("id: küçük harf, rakam ve tire (2-28 karakter, tireyle başlayıp bitmez)");
|
|
28
|
+
const name = str("name", 40);
|
|
29
|
+
const version = str("version", 32);
|
|
30
|
+
if (version && !VERSION_RE.test(version))
|
|
31
|
+
errors.push("version: x.y.z biçiminde olmalı");
|
|
32
|
+
const description = str("description", 300, false);
|
|
33
|
+
const author = str("author", 80, false);
|
|
34
|
+
const main = str("main", 120, false);
|
|
35
|
+
if (main && (main.startsWith("/") || main.includes("..") || main.includes("\\")))
|
|
36
|
+
errors.push("main: plugin klasörünün içinde göreli yol olmalı");
|
|
37
|
+
const sdk = str("sdk", 10);
|
|
38
|
+
if (sdk && !SDK_RANGE_RE.test(sdk))
|
|
39
|
+
errors.push('sdk: "0" ya da "0.1" gibi ana sürüm');
|
|
40
|
+
const perms = o.permissions;
|
|
41
|
+
let permissions = [];
|
|
42
|
+
if (!Array.isArray(perms))
|
|
43
|
+
errors.push("permissions: dizi olmalı (boş olabilir)");
|
|
44
|
+
else {
|
|
45
|
+
for (const p of perms)
|
|
46
|
+
if (!PERMISSIONS.includes(p))
|
|
47
|
+
errors.push(`permissions: bilinmeyen izin "${String(p)}"`);
|
|
48
|
+
permissions = [...new Set(perms)];
|
|
49
|
+
}
|
|
50
|
+
let ui;
|
|
51
|
+
if (o.ui !== undefined) {
|
|
52
|
+
const u = o.ui;
|
|
53
|
+
if (!u || typeof u !== "object" || Array.isArray(u))
|
|
54
|
+
errors.push("ui: nesne olmalı");
|
|
55
|
+
else {
|
|
56
|
+
const title = typeof u.title === "string" && u.title.trim() ? u.title.trim().slice(0, 40) : "";
|
|
57
|
+
const page = typeof u.page === "string" ? u.page.trim() : "";
|
|
58
|
+
const icon = u.icon === undefined ? undefined : typeof u.icon === "string" && /^[a-zA-Z]{1,24}$/.test(u.icon) ? u.icon : null;
|
|
59
|
+
if (!title)
|
|
60
|
+
errors.push("ui.title: metin olmalı");
|
|
61
|
+
if (!page || page.startsWith("/") || page.includes("..") || page.includes("\\") || !/\.html?$/i.test(page))
|
|
62
|
+
errors.push("ui.page: plugin klasöründe .html dosyası olmalı");
|
|
63
|
+
if (u.scope !== "voice" && u.scope !== "guild")
|
|
64
|
+
errors.push('ui.scope: "voice" ya da "guild"');
|
|
65
|
+
if (icon === null)
|
|
66
|
+
errors.push("ui.icon: harflerden oluşan ikon adı");
|
|
67
|
+
if (title && page && icon !== null)
|
|
68
|
+
ui = { title, page, scope: u.scope, ...(icon ? { icon } : {}) };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (errors.length)
|
|
72
|
+
return { ok: false, errors };
|
|
73
|
+
return { ok: true, manifest: { id: id, name: name, version: version, description, author, main: main ?? "src/index.ts", sdk: sdk, permissions, ...(ui ? { ui } : {}) } };
|
|
74
|
+
}
|
|
75
|
+
/** Manifest'in istedigi SDK surumu bu SDK ile uyumlu mu ("0.1" -> 0.1.x; "1" -> 1.x). 0.x'te ara surum de kirici sayilir. */
|
|
76
|
+
export function sdkCompatible(want, have) {
|
|
77
|
+
const [wMaj, wMin] = want.split(".").map(Number);
|
|
78
|
+
const [hMaj, hMin] = have.split(".").map(Number);
|
|
79
|
+
if (wMaj !== hMaj)
|
|
80
|
+
return false;
|
|
81
|
+
if (wMaj === 0)
|
|
82
|
+
return wMin === undefined ? true : wMin === hMin;
|
|
83
|
+
return wMin === undefined || hMin >= wMin;
|
|
84
|
+
}
|
package/dist/remote.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { PluginDefinition, PluginManifest } from "./types.ts";
|
|
2
|
+
export declare const DEFAULT_GATEWAY = "wss://bunker.community/app/api/plugin/gateway";
|
|
3
|
+
export interface ConnectOptions {
|
|
4
|
+
/** Ayarlar -> Geliştirici'den alinan anahtar */
|
|
5
|
+
token: string;
|
|
6
|
+
/** Gecit adresi (varsayilan bunker.community) */
|
|
7
|
+
url?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Manifest (bunker-plugin.json icerigi). Verilmezse `dir` klasorundeki bunker-plugin.json okunur; o da yoksa
|
|
10
|
+
* Bunker'da kayitli manifest kullanilir. id alani yok sayilir (anahtarin plugin'i).
|
|
11
|
+
*/
|
|
12
|
+
manifest?: Partial<PluginManifest>;
|
|
13
|
+
/** bunker-plugin.json ve pencere sayfasinin klasoru (varsayilan: calisma klasoru) */
|
|
14
|
+
dir?: string;
|
|
15
|
+
/** Pencere sayfasinin HTML'i (verilmezse manifest ui.page dosyasi `dir`'den okunur) */
|
|
16
|
+
uiPage?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Baglanti (varsayilan: WebSocket(url)). Bunker'da barindirilan plugin'lerde calisma ortami kendi kanalini
|
|
19
|
+
* verir; normalde verme.
|
|
20
|
+
*/
|
|
21
|
+
transport?: (url: string) => TransportLike;
|
|
22
|
+
/** Kayit (varsayilan console) */
|
|
23
|
+
logger?: {
|
|
24
|
+
info(...a: unknown[]): void;
|
|
25
|
+
warn(...a: unknown[]): void;
|
|
26
|
+
error(...a: unknown[]): void;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** WebSocket'in kullanilan kismi */
|
|
30
|
+
export interface TransportLike {
|
|
31
|
+
onopen: ((e?: unknown) => void) | null;
|
|
32
|
+
onmessage: ((e: {
|
|
33
|
+
data: unknown;
|
|
34
|
+
}) => void) | null;
|
|
35
|
+
onclose: ((e: {
|
|
36
|
+
code: number;
|
|
37
|
+
reason: string;
|
|
38
|
+
}) => void) | null;
|
|
39
|
+
onerror: ((e?: unknown) => void) | null;
|
|
40
|
+
send(data: string): void;
|
|
41
|
+
close(code?: number, reason?: string): void;
|
|
42
|
+
}
|
|
43
|
+
export interface Connection {
|
|
44
|
+
/** Ilk baglanti kuruldu ve setup calisti */
|
|
45
|
+
readonly ready: Promise<void>;
|
|
46
|
+
readonly connected: boolean;
|
|
47
|
+
/** Baglantiyi kapat, onUnload'lari calistir */
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
export declare function connect(def: PluginDefinition, opts: ConnectOptions): Connection;
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { SDK_VERSION as SDK } from "./version.js";
|
|
2
|
+
export const DEFAULT_GATEWAY = "wss://bunker.community/app/api/plugin/gateway";
|
|
3
|
+
/** Bu kodlarla kapanirsa yeniden denenmez */
|
|
4
|
+
const FATAL = {
|
|
5
|
+
4001: "anahtar geçersiz",
|
|
6
|
+
4003: "plugin Bunker'da kapalı",
|
|
7
|
+
4004: "aynı anahtarla başka bir bağlantı açıldı",
|
|
8
|
+
4005: "manifest geçersiz",
|
|
9
|
+
};
|
|
10
|
+
export function connect(def, opts) {
|
|
11
|
+
const log = opts.logger ?? console;
|
|
12
|
+
const url = opts.url ?? DEFAULT_GATEWAY;
|
|
13
|
+
let ws = null;
|
|
14
|
+
let open = false;
|
|
15
|
+
let closed = false;
|
|
16
|
+
let started = false;
|
|
17
|
+
let seq = 0;
|
|
18
|
+
let backoff = 1000;
|
|
19
|
+
let manifest = null;
|
|
20
|
+
const pending = new Map();
|
|
21
|
+
const events = new Map();
|
|
22
|
+
const commands = new Map();
|
|
23
|
+
const unloaders = [];
|
|
24
|
+
const sessions = new Map();
|
|
25
|
+
let readyResolve, readyReject;
|
|
26
|
+
const ready = new Promise((res, rej) => { readyResolve = res; readyReject = rej; });
|
|
27
|
+
ready.catch(() => { });
|
|
28
|
+
const raw = (m) => { if (ws && open)
|
|
29
|
+
ws.send(JSON.stringify(m)); };
|
|
30
|
+
function call(method, ...args) {
|
|
31
|
+
if (!ws || !open)
|
|
32
|
+
return Promise.reject(new Error("Bunker'a bağlı değil"));
|
|
33
|
+
const id = ++seq;
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
pending.set(id, { resolve: resolve, reject });
|
|
36
|
+
raw({ op: "call", id, method, args });
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** Sonucu beklenmeyen cagri (hata kayda) */
|
|
40
|
+
const fire = (method, ...args) => { call(method, ...args).catch((e) => log.warn(`[bunker] ${method}: ${e.message}`)); };
|
|
41
|
+
const safe = (where, fn) => {
|
|
42
|
+
try {
|
|
43
|
+
const r = fn();
|
|
44
|
+
if (r && typeof r.then === "function")
|
|
45
|
+
r.catch((e) => log.error(`[bunker] ${where}:`, e));
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
log.error(`[bunker] ${where}:`, e);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
async function loadLocal() {
|
|
52
|
+
let m = opts.manifest;
|
|
53
|
+
let page = opts.uiPage;
|
|
54
|
+
if (!m || (m.ui && page === undefined)) {
|
|
55
|
+
try {
|
|
56
|
+
const fs = await import("node:fs");
|
|
57
|
+
const path = await import("node:path");
|
|
58
|
+
const dir = opts.dir ?? process.cwd();
|
|
59
|
+
if (!m) {
|
|
60
|
+
const f = path.join(dir, "bunker-plugin.json");
|
|
61
|
+
if (fs.existsSync(f))
|
|
62
|
+
m = JSON.parse(fs.readFileSync(f, "utf8"));
|
|
63
|
+
}
|
|
64
|
+
const ui = m?.ui;
|
|
65
|
+
if (ui?.page && page === undefined)
|
|
66
|
+
page = fs.readFileSync(path.join(dir, ui.page), "utf8");
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
log.warn("[bunker] manifest/pencere okunamadı:", e.message);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { m, page };
|
|
73
|
+
}
|
|
74
|
+
function resubscribe() {
|
|
75
|
+
for (const name of events.keys())
|
|
76
|
+
fire("subscribe", name);
|
|
77
|
+
for (const name of commands.keys())
|
|
78
|
+
fire("command", name);
|
|
79
|
+
}
|
|
80
|
+
const ctx = {
|
|
81
|
+
get manifest() { return manifest; },
|
|
82
|
+
log: {
|
|
83
|
+
info: (msg, c) => fire("log", "info", String(msg), c),
|
|
84
|
+
warn: (msg, c) => fire("log", "warn", String(msg), c),
|
|
85
|
+
error: (msg, c) => fire("log", "error", String(msg), c),
|
|
86
|
+
},
|
|
87
|
+
on(name, fn) {
|
|
88
|
+
let set = events.get(name);
|
|
89
|
+
if (!set) {
|
|
90
|
+
set = new Set();
|
|
91
|
+
events.set(name, set);
|
|
92
|
+
if (open)
|
|
93
|
+
fire("subscribe", name);
|
|
94
|
+
}
|
|
95
|
+
const f = fn;
|
|
96
|
+
set.add(f);
|
|
97
|
+
return () => { set.delete(f); if (!set.size) {
|
|
98
|
+
events.delete(name);
|
|
99
|
+
if (open)
|
|
100
|
+
fire("unsubscribe", name);
|
|
101
|
+
} };
|
|
102
|
+
},
|
|
103
|
+
command(name, fn) {
|
|
104
|
+
const n = String(name).replace(/^!/, "").toLocaleLowerCase("tr");
|
|
105
|
+
if (commands.has(n))
|
|
106
|
+
throw new Error(`!${n} zaten kayıtlı`);
|
|
107
|
+
commands.set(n, fn);
|
|
108
|
+
if (open)
|
|
109
|
+
fire("command", n);
|
|
110
|
+
return () => { commands.delete(n); if (open)
|
|
111
|
+
fire("uncommand", n); };
|
|
112
|
+
},
|
|
113
|
+
chat: { send: (chan, text) => call("chat.send", chan, text) },
|
|
114
|
+
storage: {
|
|
115
|
+
get: (key) => call("storage.get", key).then((v) => (v === null ? undefined : v)),
|
|
116
|
+
set: (key, value) => call("storage.set", key, value).then(() => { }),
|
|
117
|
+
delete: (key) => call("storage.delete", key).then(() => { }),
|
|
118
|
+
keys: (prefix = "") => call("storage.keys", prefix),
|
|
119
|
+
},
|
|
120
|
+
voice: {
|
|
121
|
+
async join(cid) { return sessionFor(await call("voice.join", cid)); },
|
|
122
|
+
session(cid) { return sessions.get(cid) ?? null; },
|
|
123
|
+
},
|
|
124
|
+
ui: {
|
|
125
|
+
setState: (where, state, o) => fire("ui.setState", where, state, o ?? {}),
|
|
126
|
+
onAction(fn) {
|
|
127
|
+
return ctx.on("ui.action", fn);
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
onUnload(fn) { unloaders.push(fn); },
|
|
131
|
+
};
|
|
132
|
+
function sessionFor(st) {
|
|
133
|
+
let s = sessions.get(st.cid);
|
|
134
|
+
if (!s) {
|
|
135
|
+
s = new RemoteSession(st, call);
|
|
136
|
+
sessions.set(st.cid, s);
|
|
137
|
+
}
|
|
138
|
+
s.st = st;
|
|
139
|
+
return s;
|
|
140
|
+
}
|
|
141
|
+
function onMessage(data) {
|
|
142
|
+
let m;
|
|
143
|
+
try {
|
|
144
|
+
m = JSON.parse(data);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
switch (m.op) {
|
|
150
|
+
case "ready":
|
|
151
|
+
manifest = m.manifest;
|
|
152
|
+
backoff = 1000;
|
|
153
|
+
log.info(`[bunker] bağlandı: ${manifest.name} (${manifest.id}), ${m.guilds?.length ?? 0} sunucu`);
|
|
154
|
+
if (!started) {
|
|
155
|
+
started = true;
|
|
156
|
+
Promise.resolve().then(() => def.setup(ctx)).then(() => readyResolve(), (e) => { log.error("[bunker] setup:", e); readyReject(e); });
|
|
157
|
+
}
|
|
158
|
+
else
|
|
159
|
+
resubscribe();
|
|
160
|
+
return;
|
|
161
|
+
case "result": {
|
|
162
|
+
const p = pending.get(m.id);
|
|
163
|
+
if (!p)
|
|
164
|
+
return;
|
|
165
|
+
pending.delete(m.id);
|
|
166
|
+
if (m.ok)
|
|
167
|
+
p.resolve(m.value ?? undefined);
|
|
168
|
+
else
|
|
169
|
+
p.reject(new Error(String(m.error)));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
case "event":
|
|
173
|
+
for (const fn of [...(events.get(m.name) ?? [])])
|
|
174
|
+
safe(m.name, () => fn(m.data));
|
|
175
|
+
return;
|
|
176
|
+
case "command": {
|
|
177
|
+
const fn = commands.get(m.name);
|
|
178
|
+
const c = m.call;
|
|
179
|
+
if (fn)
|
|
180
|
+
safe("!" + m.name, () => fn({ ...c, reply: async (text) => { await call("chat.send", c.chan, text); } }));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
case "voice": {
|
|
184
|
+
const s = sessions.get(m.cid);
|
|
185
|
+
if (!s)
|
|
186
|
+
return;
|
|
187
|
+
s.st = m.state;
|
|
188
|
+
if (m.ev === "leave")
|
|
189
|
+
sessions.delete(m.cid);
|
|
190
|
+
s.emit(m.ev, m.args ?? [], safe);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async function dial() {
|
|
196
|
+
if (closed)
|
|
197
|
+
return;
|
|
198
|
+
const { m, page } = await loadLocal();
|
|
199
|
+
const sock = opts.transport ? opts.transport(url) : new WebSocket(url);
|
|
200
|
+
ws = sock;
|
|
201
|
+
sock.onopen = () => {
|
|
202
|
+
open = true;
|
|
203
|
+
sock.send(JSON.stringify({ op: "hello", token: opts.token, sdk: SDK, ...(m ? { manifest: m } : {}), ...(page !== undefined ? { uiPage: page } : {}) }));
|
|
204
|
+
};
|
|
205
|
+
sock.onmessage = (e) => onMessage(String(e.data));
|
|
206
|
+
sock.onclose = (e) => {
|
|
207
|
+
open = false;
|
|
208
|
+
ws = null;
|
|
209
|
+
for (const p of pending.values())
|
|
210
|
+
p.reject(new Error("bağlantı koptu"));
|
|
211
|
+
pending.clear();
|
|
212
|
+
for (const s of sessions.values()) {
|
|
213
|
+
s.st = { ...s.st, connected: false, playing: false };
|
|
214
|
+
s.emit("leave", ["bağlantı koptu"], safe);
|
|
215
|
+
}
|
|
216
|
+
sessions.clear();
|
|
217
|
+
if (closed)
|
|
218
|
+
return;
|
|
219
|
+
const fatal = FATAL[e.code];
|
|
220
|
+
if (fatal) {
|
|
221
|
+
log.error(`[bunker] bağlantı kapandı: ${fatal}${e.reason ? ` (${e.reason})` : ""}`);
|
|
222
|
+
closed = true;
|
|
223
|
+
readyReject(new Error(e.reason || fatal));
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
log.warn(`[bunker] bağlantı koptu (${e.code}${e.reason ? " " + e.reason : ""}), ${Math.round(backoff / 1000)} sn sonra yeniden`);
|
|
227
|
+
setTimeout(() => void dial(), backoff);
|
|
228
|
+
backoff = Math.min(30_000, backoff * 2);
|
|
229
|
+
};
|
|
230
|
+
sock.onerror = () => { };
|
|
231
|
+
}
|
|
232
|
+
void dial();
|
|
233
|
+
return {
|
|
234
|
+
ready,
|
|
235
|
+
get connected() { return open; },
|
|
236
|
+
async close() {
|
|
237
|
+
if (closed && !ws)
|
|
238
|
+
return;
|
|
239
|
+
closed = true;
|
|
240
|
+
for (const u of unloaders.splice(0)) {
|
|
241
|
+
try {
|
|
242
|
+
await u();
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
log.error("[bunker] onUnload:", e);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
ws?.close(1000, "kapatıldı");
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
class RemoteSession {
|
|
253
|
+
st;
|
|
254
|
+
call;
|
|
255
|
+
fns = new Map();
|
|
256
|
+
constructor(st, call) { this.st = st; this.call = call; }
|
|
257
|
+
get gid() { return this.st.gid; }
|
|
258
|
+
get cid() { return this.st.cid; }
|
|
259
|
+
get playing() { return this.st.playing; }
|
|
260
|
+
get paused() { return this.st.paused; }
|
|
261
|
+
get volume() { return this.st.volume; }
|
|
262
|
+
get connected() { return this.st.connected; }
|
|
263
|
+
do(method, ...args) { return this.call("voice.call", this.st.cid, method, args).then((st) => { if (st)
|
|
264
|
+
this.st = st; }); }
|
|
265
|
+
quiet(method, ...args) { this.do(method, ...args).catch(() => { }); }
|
|
266
|
+
playPcm() { throw new Error("playPcm uzak plugin'lerde yok (playUrl / playMedia kullan)"); }
|
|
267
|
+
playUrl(url, opts) { return this.do("playUrl", url, opts ?? {}); }
|
|
268
|
+
playMedia(query, opts) { return this.do("playMedia", query, opts ?? {}); }
|
|
269
|
+
prefetch(query) { this.quiet("prefetch", query); }
|
|
270
|
+
stop() { this.quiet("stop"); }
|
|
271
|
+
pause() { this.st = { ...this.st, paused: true }; this.quiet("pause"); }
|
|
272
|
+
resume() { this.st = { ...this.st, paused: false }; this.quiet("resume"); }
|
|
273
|
+
setVolume(v) { this.st = { ...this.st, volume: v }; this.quiet("setVolume", v); }
|
|
274
|
+
leave() { return this.do("leave"); }
|
|
275
|
+
on(ev, fn) {
|
|
276
|
+
let set = this.fns.get(ev);
|
|
277
|
+
if (!set) {
|
|
278
|
+
set = new Set();
|
|
279
|
+
this.fns.set(ev, set);
|
|
280
|
+
}
|
|
281
|
+
set.add(fn);
|
|
282
|
+
return () => void set.delete(fn);
|
|
283
|
+
}
|
|
284
|
+
emit(ev, args, safe) {
|
|
285
|
+
for (const fn of [...(this.fns.get(ev) ?? [])])
|
|
286
|
+
safe("voice." + ev, () => fn(...args));
|
|
287
|
+
}
|
|
288
|
+
}
|