@littlespace/fluxkit 0.1.0 → 0.1.1

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/README.md CHANGED
@@ -41,7 +41,8 @@ Bots are never "passive" in a guild, so they get every message they can see with
41
41
  - `client.rest`: `sendMessage`, `editMessage`, `deleteMessage(ch, id, reason)`, `react`, `createDM`, `me`, plus `get/post/patch/put/delete(path, ...)` for anything else (paths work with or without `/v1`).
42
42
  - `client.reply(message, content)`: replies without pinging the author.
43
43
  - Events: `ready`, `message`, `commandError`, `debug`, `error`, `raw (t, d)`, and every dispatch by name (`MESSAGE_DELETE`, `GUILD_MEMBER_ADD`, ...).
44
- - `client.guilds`, `client.user`, `client.gateway.ping`.
44
+ - `client.permissionsFor(message)`: the author's permissions in that channel (a BigInt), or `null` in DMs. Check with `hasPermission(perms, Permissions.MANAGE_MESSAGES)`.
45
+ - `client.guilds` (kept current as roles and channels change), `client.channel(guildId, channelId)`, `client.user`, `client.gateway.ping`.
45
46
 
46
47
  ## Testing
47
48
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@littlespace/fluxkit",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Shared Fluxer adapter for the CiGaming bot fleet",
5
5
  "main": "index.js",
6
6
  "directories": {
package/src/client.js CHANGED
@@ -6,6 +6,7 @@ import { discover } from './discovery.js';
6
6
  import { REST } from './rest.js';
7
7
  import { Gateway } from './gateway.js';
8
8
  import { Commands } from './commands.js';
9
+ import { computePermissions } from './permissions.js';
9
10
 
10
11
  const DEFAULT_IGNORED = ['TYPING_START', 'PRESENCE_UPDATE'];
11
12
 
@@ -66,12 +67,38 @@ export class FluxerClient extends EventEmitter {
66
67
  for (const g of d.guilds ?? []) this.guilds.set(g.id, g);
67
68
  this.emit('ready', d);
68
69
  break;
69
- case 'GUILD_CREATE':
70
+ case 'GUILD_CREATE': {
71
+ // Fluxer nests the guild record (owner_id, name...) under "properties".
72
+ const { properties, ...rest } = d;
73
+ this.guilds.set(d.id, { ...this.guilds.get(d.id), ...(properties ?? {}), ...rest });
74
+ break;
75
+ }
70
76
  case 'GUILD_UPDATE':
71
77
  this.guilds.set(d.id, { ...this.guilds.get(d.id), ...d });
72
78
  break;
73
79
  case 'GUILD_DELETE':
74
80
  if (!d.unavailable) this.guilds.delete(d.id);
81
+ else if (this.guilds.has(d.id)) this.guilds.get(d.id).unavailable = true;
82
+ break;
83
+ case 'GUILD_ROLE_CREATE':
84
+ case 'GUILD_ROLE_UPDATE':
85
+ this.#upsert(d.guild_id, 'roles', [d.role]);
86
+ break;
87
+ case 'GUILD_ROLE_UPDATE_BULK':
88
+ this.#upsert(d.guild_id, 'roles', d.roles);
89
+ break;
90
+ case 'GUILD_ROLE_DELETE':
91
+ this.#remove(d.guild_id, 'roles', d.role_id);
92
+ break;
93
+ case 'CHANNEL_CREATE':
94
+ case 'CHANNEL_UPDATE':
95
+ if (d.guild_id) this.#upsert(d.guild_id, 'channels', [d]);
96
+ break;
97
+ case 'CHANNEL_UPDATE_BULK':
98
+ this.#upsert(d.guild_id, 'channels', d.channels);
99
+ break;
100
+ case 'CHANNEL_DELETE':
101
+ if (d.guild_id) this.#remove(d.guild_id, 'channels', d.id);
75
102
  break;
76
103
  case 'MESSAGE_CREATE':
77
104
  if (d.author?.id === this.user?.id) break;
@@ -89,6 +116,34 @@ export class FluxerClient extends EventEmitter {
89
116
  this.emit(t, d);
90
117
  }
91
118
 
119
+ channel(guildId, channelId) {
120
+ return this.guilds.get(guildId)?.channels?.find((c) => c.id === channelId) ?? null;
121
+ }
122
+
123
+ // Permissions the author of a message has in the channel it was sent in.
124
+ // Uses the member object Fluxer attaches to guild messages; DMs return null.
125
+ permissionsFor(message) {
126
+ if (!message.guild_id || !message.member) return null;
127
+ const guild = this.guilds.get(message.guild_id);
128
+ if (!guild?.roles) return null;
129
+ return computePermissions(guild, message.author.id, message.member.roles ?? [], this.channel(message.guild_id, message.channel_id));
130
+ }
131
+
132
+ #upsert(guildId, key, items) {
133
+ const g = this.guilds.get(guildId);
134
+ if (!g) return;
135
+ const list = g[key] ?? (g[key] = []);
136
+ for (const item of items ?? []) {
137
+ const i = list.findIndex((x) => x.id === item.id);
138
+ if (i === -1) list.push(item); else list[i] = { ...list[i], ...item };
139
+ }
140
+ }
141
+
142
+ #remove(guildId, key, id) {
143
+ const g = this.guilds.get(guildId);
144
+ if (g?.[key]) g[key] = g[key].filter((x) => x.id !== id);
145
+ }
146
+
92
147
  reply(message, content) {
93
148
  const body = typeof content === 'string' ? { content } : { ...content };
94
149
  body.message_reference = { message_id: message.id, channel_id: message.channel_id };
package/src/rest.js CHANGED
@@ -15,7 +15,7 @@ export class FluxerAPIError extends Error {
15
15
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
16
16
 
17
17
  export class REST {
18
- constructor({ api, token, fetchImpl = fetch, maxRetries = 3, userAgent = '@littlespace/fluxkit (0.1.0)' }) {
18
+ constructor({ api, token, fetchImpl = fetch, maxRetries = 3, userAgent = '@littlespace/fluxkit (0.1.1)' }) {
19
19
  if (!api) throw new Error('REST needs the api base URL from discovery');
20
20
  this.api = api;
21
21
  this.token = token;