@bunker-community/plugin-sdk 0.0.0-stage → 0.1.7
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 +589 -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 +85 -0
- package/dist/remote.d.ts +50 -0
- package/dist/remote.js +291 -0
- package/dist/types.d.ts +323 -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
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type PluginManifest } from "./types.ts";
|
|
2
|
+
export declare const MANIFEST_FILE = "bunker-plugin.json";
|
|
3
|
+
/** 2-28 characters: the bot identity "BunkerPluginBot.<id>" stays at most 44 characters */
|
|
4
|
+
export declare const ID_RE: RegExp;
|
|
5
|
+
export type ManifestResult = {
|
|
6
|
+
ok: true;
|
|
7
|
+
manifest: PluginManifest;
|
|
8
|
+
} | {
|
|
9
|
+
ok: false;
|
|
10
|
+
errors: string[];
|
|
11
|
+
};
|
|
12
|
+
export declare function validateManifest(raw: unknown): ManifestResult;
|
|
13
|
+
/** Is the SDK version the manifest asks for compatible with this SDK ("0.1" -> 0.1.x; "1" -> 1.x). In 0.x a minor version counts as breaking too. */
|
|
14
|
+
export declare function sdkCompatible(want: string, have: string): boolean;
|
package/dist/manifest.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/* bunker-plugin.json validation (dependency-free; the runtime and the developer tool use the same code) */
|
|
2
|
+
import { PERMISSIONS } from "./types.js";
|
|
3
|
+
export const MANIFEST_FILE = "bunker-plugin.json";
|
|
4
|
+
/** 2-28 characters: the bot identity "BunkerPluginBot.<id>" stays at most 44 characters */
|
|
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 must be a JSON object"] };
|
|
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}: must be text`);
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
if (v.length > max)
|
|
22
|
+
errors.push(`${k}: at most ${max} characters`);
|
|
23
|
+
return v.trim();
|
|
24
|
+
};
|
|
25
|
+
const id = str("id", 28);
|
|
26
|
+
if (id && !ID_RE.test(id))
|
|
27
|
+
errors.push("id: lowercase letters, digits and dashes (2-28 characters, not starting or ending with a dash)");
|
|
28
|
+
const name = str("name", 40);
|
|
29
|
+
const version = str("version", 32);
|
|
30
|
+
if (version && !VERSION_RE.test(version))
|
|
31
|
+
errors.push("version: must be x.y.z");
|
|
32
|
+
const description = str("description", 300, false);
|
|
33
|
+
const details = str("details", 4000, false);
|
|
34
|
+
const author = str("author", 80, false);
|
|
35
|
+
const main = str("main", 120, false);
|
|
36
|
+
if (main && (main.startsWith("/") || main.includes("..") || main.includes("\\")))
|
|
37
|
+
errors.push("main: must be a relative path inside the plugin folder");
|
|
38
|
+
const sdk = str("sdk", 10);
|
|
39
|
+
if (sdk && !SDK_RANGE_RE.test(sdk))
|
|
40
|
+
errors.push('sdk: a major version like "0" or "0.1"');
|
|
41
|
+
const perms = o.permissions;
|
|
42
|
+
let permissions = [];
|
|
43
|
+
if (!Array.isArray(perms))
|
|
44
|
+
errors.push("permissions: must be an array (can be empty)");
|
|
45
|
+
else {
|
|
46
|
+
for (const p of perms)
|
|
47
|
+
if (!PERMISSIONS.includes(p))
|
|
48
|
+
errors.push(`permissions: unknown permission "${String(p)}"`);
|
|
49
|
+
permissions = [...new Set(perms)];
|
|
50
|
+
}
|
|
51
|
+
let ui;
|
|
52
|
+
if (o.ui !== undefined) {
|
|
53
|
+
const u = o.ui;
|
|
54
|
+
if (!u || typeof u !== "object" || Array.isArray(u))
|
|
55
|
+
errors.push("ui: must be an object");
|
|
56
|
+
else {
|
|
57
|
+
const title = typeof u.title === "string" && u.title.trim() ? u.title.trim().slice(0, 40) : "";
|
|
58
|
+
const page = typeof u.page === "string" ? u.page.trim() : "";
|
|
59
|
+
const icon = u.icon === undefined ? undefined : typeof u.icon === "string" && /^[a-zA-Z]{1,24}$/.test(u.icon) ? u.icon : null;
|
|
60
|
+
if (!title)
|
|
61
|
+
errors.push("ui.title: must be text");
|
|
62
|
+
if (!page || page.startsWith("/") || page.includes("..") || page.includes("\\") || !/\.html?$/i.test(page))
|
|
63
|
+
errors.push("ui.page: must be an .html file in the plugin folder");
|
|
64
|
+
if (u.scope !== "voice" && u.scope !== "guild")
|
|
65
|
+
errors.push('ui.scope: "voice" or "guild"');
|
|
66
|
+
if (icon === null)
|
|
67
|
+
errors.push("ui.icon: an icon name made of letters");
|
|
68
|
+
if (title && page && icon !== null)
|
|
69
|
+
ui = { title, page, scope: u.scope, ...(icon ? { icon } : {}) };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (errors.length)
|
|
73
|
+
return { ok: false, errors };
|
|
74
|
+
return { ok: true, manifest: { id: id, name: name, version: version, description, ...(details ? { details } : {}), author, main: main ?? "src/index.ts", sdk: sdk, permissions, ...(ui ? { ui } : {}) } };
|
|
75
|
+
}
|
|
76
|
+
/** Is the SDK version the manifest asks for compatible with this SDK ("0.1" -> 0.1.x; "1" -> 1.x). In 0.x a minor version counts as breaking too. */
|
|
77
|
+
export function sdkCompatible(want, have) {
|
|
78
|
+
const [wMaj, wMin] = want.split(".").map(Number);
|
|
79
|
+
const [hMaj, hMin] = have.split(".").map(Number);
|
|
80
|
+
if (wMaj !== hMaj)
|
|
81
|
+
return false;
|
|
82
|
+
if (wMaj === 0)
|
|
83
|
+
return wMin === undefined ? true : wMin === hMin;
|
|
84
|
+
return wMin === undefined || hMin >= wMin;
|
|
85
|
+
}
|
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
|
+
/** The token from Settings -> Developer */
|
|
5
|
+
token: string;
|
|
6
|
+
/** Gateway address (default bunker.community) */
|
|
7
|
+
url?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Manifest (the content of bunker-plugin.json). If not given, bunker-plugin.json in the `dir` folder is read; if that is missing too
|
|
10
|
+
* the manifest stored on Bunker is used. The id field is ignored (it is the key's plugin).
|
|
11
|
+
*/
|
|
12
|
+
manifest?: Partial<PluginManifest>;
|
|
13
|
+
/** Folder of bunker-plugin.json and the window page (default: the working directory) */
|
|
14
|
+
dir?: string;
|
|
15
|
+
/** HTML of the window page (if not given, the manifest's ui.page file is read from `dir`) */
|
|
16
|
+
uiPage?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Connection (default: WebSocket(url)). For plugins hosted on Bunker the runtime provides its own channel;
|
|
19
|
+
* don't pass it normally.
|
|
20
|
+
*/
|
|
21
|
+
transport?: (url: string) => TransportLike;
|
|
22
|
+
/** Logger (default console) */
|
|
23
|
+
logger?: {
|
|
24
|
+
info(...a: unknown[]): void;
|
|
25
|
+
warn(...a: unknown[]): void;
|
|
26
|
+
error(...a: unknown[]): void;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** The part of WebSocket that is used */
|
|
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
|
+
/** The first connection was made and setup ran */
|
|
45
|
+
readonly ready: Promise<void>;
|
|
46
|
+
readonly connected: boolean;
|
|
47
|
+
/** Close the connection, run the onUnload handlers */
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
export declare function connect(def: PluginDefinition, opts: ConnectOptions): Connection;
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { SDK_VERSION as SDK } from "./version.js";
|
|
2
|
+
export const DEFAULT_GATEWAY = "wss://bunker.community/app/api/plugin/gateway";
|
|
3
|
+
/** No retry if it closes with these codes */
|
|
4
|
+
const FATAL = {
|
|
5
|
+
4001: "invalid key",
|
|
6
|
+
4003: "plugin is off on Bunker",
|
|
7
|
+
4004: "another connection was opened with the same key",
|
|
8
|
+
4005: "invalid manifest",
|
|
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("not connected to Bunker"));
|
|
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
|
+
/** A call whose result wasn't awaited (error to the log) */
|
|
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] could not read manifest/window:", 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} is already registered`);
|
|
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
|
+
serverLang: (gid) => call("serverLang", gid).then((v) => (v === "tr" ? "tr" : "en")),
|
|
121
|
+
voice: {
|
|
122
|
+
async join(cid) { return sessionFor(await call("voice.join", cid)); },
|
|
123
|
+
session(cid) { return sessions.get(cid) ?? null; },
|
|
124
|
+
channelOf: (uid) => call("voice.channelOf", uid),
|
|
125
|
+
},
|
|
126
|
+
ui: {
|
|
127
|
+
setState: (where, state, o) => fire("ui.setState", where, state, o ?? {}),
|
|
128
|
+
onAction(fn) {
|
|
129
|
+
return ctx.on("ui.action", fn);
|
|
130
|
+
},
|
|
131
|
+
send: (uid, where, data) => fire("ui.send", uid, where, data),
|
|
132
|
+
},
|
|
133
|
+
onUnload(fn) { unloaders.push(fn); },
|
|
134
|
+
};
|
|
135
|
+
function sessionFor(st) {
|
|
136
|
+
let s = sessions.get(st.cid);
|
|
137
|
+
if (!s) {
|
|
138
|
+
s = new RemoteSession(st, call);
|
|
139
|
+
sessions.set(st.cid, s);
|
|
140
|
+
}
|
|
141
|
+
s.st = st;
|
|
142
|
+
return s;
|
|
143
|
+
}
|
|
144
|
+
function onMessage(data) {
|
|
145
|
+
let m;
|
|
146
|
+
try {
|
|
147
|
+
m = JSON.parse(data);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
switch (m.op) {
|
|
153
|
+
case "ready":
|
|
154
|
+
manifest = m.manifest;
|
|
155
|
+
backoff = 1000;
|
|
156
|
+
log.info(`[bunker] connected: ${manifest.name} (${manifest.id}), ${m.guilds?.length ?? 0} servers`);
|
|
157
|
+
if (!started) {
|
|
158
|
+
started = true;
|
|
159
|
+
Promise.resolve().then(() => def.setup(ctx)).then(() => readyResolve(), (e) => { log.error("[bunker] setup:", e); readyReject(e); });
|
|
160
|
+
}
|
|
161
|
+
else
|
|
162
|
+
resubscribe();
|
|
163
|
+
return;
|
|
164
|
+
case "result": {
|
|
165
|
+
const p = pending.get(m.id);
|
|
166
|
+
if (!p)
|
|
167
|
+
return;
|
|
168
|
+
pending.delete(m.id);
|
|
169
|
+
if (m.ok)
|
|
170
|
+
p.resolve(m.value ?? undefined);
|
|
171
|
+
else
|
|
172
|
+
p.reject(new Error(String(m.error)));
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
case "event":
|
|
176
|
+
for (const fn of [...(events.get(m.name) ?? [])])
|
|
177
|
+
safe(m.name, () => fn(m.data));
|
|
178
|
+
return;
|
|
179
|
+
case "command": {
|
|
180
|
+
const fn = commands.get(m.name);
|
|
181
|
+
const c = m.call;
|
|
182
|
+
if (fn)
|
|
183
|
+
safe("!" + m.name, () => fn({ ...c, lang: c.lang === "tr" ? "tr" : "en", reply: async (text) => { await call("chat.send", c.chan, text); } }));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
case "voice": {
|
|
187
|
+
const s = sessions.get(m.cid);
|
|
188
|
+
if (!s)
|
|
189
|
+
return;
|
|
190
|
+
s.st = m.state;
|
|
191
|
+
if (m.ev === "leave")
|
|
192
|
+
sessions.delete(m.cid);
|
|
193
|
+
s.emit(m.ev, m.args ?? [], safe);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async function dial() {
|
|
199
|
+
if (closed)
|
|
200
|
+
return;
|
|
201
|
+
const { m, page } = await loadLocal();
|
|
202
|
+
const sock = opts.transport ? opts.transport(url) : new WebSocket(url);
|
|
203
|
+
ws = sock;
|
|
204
|
+
sock.onopen = () => {
|
|
205
|
+
open = true;
|
|
206
|
+
sock.send(JSON.stringify({ op: "hello", token: opts.token, sdk: SDK, ...(m ? { manifest: m } : {}), ...(page !== undefined ? { uiPage: page } : {}) }));
|
|
207
|
+
};
|
|
208
|
+
sock.onmessage = (e) => onMessage(String(e.data));
|
|
209
|
+
sock.onclose = (e) => {
|
|
210
|
+
open = false;
|
|
211
|
+
ws = null;
|
|
212
|
+
for (const p of pending.values())
|
|
213
|
+
p.reject(new Error("connection lost"));
|
|
214
|
+
pending.clear();
|
|
215
|
+
for (const s of sessions.values()) {
|
|
216
|
+
s.st = { ...s.st, connected: false, playing: false };
|
|
217
|
+
s.emit("leave", ["connection lost"], safe);
|
|
218
|
+
}
|
|
219
|
+
sessions.clear();
|
|
220
|
+
if (closed)
|
|
221
|
+
return;
|
|
222
|
+
const fatal = FATAL[e.code];
|
|
223
|
+
if (fatal) {
|
|
224
|
+
log.error(`[bunker] connection closed: ${fatal}${e.reason ? ` (${e.reason})` : ""}`);
|
|
225
|
+
closed = true;
|
|
226
|
+
readyReject(new Error(e.reason || fatal));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
log.warn(`[bunker] connection lost (${e.code}${e.reason ? " " + e.reason : ""}), retrying in ${Math.round(backoff / 1000)} s`);
|
|
230
|
+
setTimeout(() => void dial(), backoff);
|
|
231
|
+
backoff = Math.min(30_000, backoff * 2);
|
|
232
|
+
};
|
|
233
|
+
sock.onerror = () => { };
|
|
234
|
+
}
|
|
235
|
+
void dial();
|
|
236
|
+
return {
|
|
237
|
+
ready,
|
|
238
|
+
get connected() { return open; },
|
|
239
|
+
async close() {
|
|
240
|
+
if (closed && !ws)
|
|
241
|
+
return;
|
|
242
|
+
closed = true;
|
|
243
|
+
for (const u of unloaders.splice(0)) {
|
|
244
|
+
try {
|
|
245
|
+
await u();
|
|
246
|
+
}
|
|
247
|
+
catch (e) {
|
|
248
|
+
log.error("[bunker] onUnload:", e);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
ws?.close(1000, "turned off");
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
class RemoteSession {
|
|
256
|
+
st;
|
|
257
|
+
call;
|
|
258
|
+
fns = new Map();
|
|
259
|
+
constructor(st, call) { this.st = st; this.call = call; }
|
|
260
|
+
get gid() { return this.st.gid; }
|
|
261
|
+
get cid() { return this.st.cid; }
|
|
262
|
+
get playing() { return this.st.playing; }
|
|
263
|
+
get paused() { return this.st.paused; }
|
|
264
|
+
get volume() { return this.st.volume; }
|
|
265
|
+
get connected() { return this.st.connected; }
|
|
266
|
+
do(method, ...args) { return this.call("voice.call", this.st.cid, method, args).then((st) => { if (st)
|
|
267
|
+
this.st = st; }); }
|
|
268
|
+
quiet(method, ...args) { this.do(method, ...args).catch(() => { }); }
|
|
269
|
+
playPcm() { throw new Error("playPcm is not available for remote plugins (use playUrl / playMedia)"); }
|
|
270
|
+
playUrl(url, opts) { return this.do("playUrl", url, opts ?? {}); }
|
|
271
|
+
playMedia(query, opts) { return this.do("playMedia", query, opts ?? {}); }
|
|
272
|
+
prefetch(query) { this.quiet("prefetch", query); }
|
|
273
|
+
stop() { this.quiet("stop"); }
|
|
274
|
+
pause() { this.st = { ...this.st, paused: true }; this.quiet("pause"); }
|
|
275
|
+
resume() { this.st = { ...this.st, paused: false }; this.quiet("resume"); }
|
|
276
|
+
setVolume(v) { this.st = { ...this.st, volume: v }; this.quiet("setVolume", v); }
|
|
277
|
+
leave() { return this.do("leave"); }
|
|
278
|
+
on(ev, fn) {
|
|
279
|
+
let set = this.fns.get(ev);
|
|
280
|
+
if (!set) {
|
|
281
|
+
set = new Set();
|
|
282
|
+
this.fns.set(ev, set);
|
|
283
|
+
}
|
|
284
|
+
set.add(fn);
|
|
285
|
+
return () => void set.delete(fn);
|
|
286
|
+
}
|
|
287
|
+
emit(ev, args, safe) {
|
|
288
|
+
for (const fn of [...(this.fns.get(ev) ?? [])])
|
|
289
|
+
safe("voice." + ev, () => fn(...args));
|
|
290
|
+
}
|
|
291
|
+
}
|