lolka.js 0.3.0 → 0.4.0

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
@@ -34,3 +34,56 @@ client.on('messageCreate', (message) => {
34
34
 
35
35
  client.login('token');
36
36
  ```
37
+
38
+ ## Voice
39
+
40
+ Voice works over WebRTC (lolka's voice stack, not Discord's UDP transport) and ships with the base
41
+ install — there's nothing extra to add. The API is in the spirit of `@discordjs/voice`: get a
42
+ connection with `joinVoiceChannel`, play audio, and (optionally) receive other participants' audio.
43
+
44
+ ```js
45
+ const { Client, GatewayIntentBits, joinVoiceChannel } = require('lolka.js');
46
+
47
+ const client = new Client({
48
+ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages],
49
+ });
50
+
51
+ client.on('messageCreate', async (message) => {
52
+ if (message.content !== '!play') return;
53
+
54
+ const channel = message.member?.voice?.channel;
55
+ if (!channel) return message.reply('Join a voice channel first.');
56
+
57
+ const connection = joinVoiceChannel({
58
+ channelId: channel.id,
59
+ guildId: channel.guild.id,
60
+ adapterCreator: channel.guild.voiceAdapterCreator,
61
+ });
62
+
63
+ // receive other participants' audio (optional)
64
+ connection.on('track', (track, userId) => {
65
+ // `track` is a MediaStreamTrack from @roamhq/wrtc
66
+ console.log(`receiving audio from ${userId}`);
67
+ });
68
+
69
+ await connection.awaitReady();
70
+ connection.play('song.mp3'); // a file path or any Readable stream (decoded via ffmpeg)
71
+
72
+ connection.on('idle', () => console.log('playback finished'));
73
+ });
74
+
75
+ client.login('token');
76
+ ```
77
+
78
+ Leave the channel with `connection.destroy()`, and stop playback (without leaving) with
79
+ `connection.stop()`. Playing files requires `ffmpeg` to be available in the bot's environment.
80
+
81
+ `joinVoiceChannel(options)` accepts:
82
+
83
+ | option | type | description |
84
+ | --- | --- | --- |
85
+ | `channelId` | `string` | Voice channel id |
86
+ | `guildId` | `string` | Guild id |
87
+ | `adapterCreator` | `Function` | `guild.voiceAdapterCreator` |
88
+ | `selfMute` | `boolean` | default `false` |
89
+ | `selfDeaf` | `boolean` | default `false` |
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "lolka.js",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "description": "A powerful library for interacting with the lolka bot API (discord.js-compatible fork)",
6
6
  "scripts": {
7
7
  "build": "node ./scripts/esmDts.mjs",
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const { userMention } = require('@discordjs/formatters');
4
- const { calculateUserDefaultAvatarIndex } = require('@discordjs/rest');
4
+ const { lolkaDefaultAvatarIndex } = require('../util/LolkaDefaultAvatar');
5
5
  const { DiscordSnowflake } = require('../util/LolkaSnowflake');
6
6
  const Base = require('./Base');
7
7
  const TextBasedChannel = require('./interfaces/TextBasedChannel');
@@ -270,12 +270,10 @@ class User extends Base {
270
270
  * @readonly
271
271
  */
272
272
  get defaultAvatarURL() {
273
- const index =
274
- this.discriminator === '0' || this.discriminator === '0000'
275
- ? calculateUserDefaultAvatarIndex(this.id)
276
- : this.discriminator % 5;
277
-
278
- return this.client.rest.cdn.defaultAvatar(index);
273
+ // Lolka: свои дефолтные аватарки system/avatars/{0..5}.jpg вместо Discord embed/avatars.
274
+ // Индекс по FNV-1a от id (как в веб/мобильном клиенте), чтобы дефолт совпадал во всех клиентах.
275
+ const base = process.env.LOLKA_CDN ?? 'https://cdn.lolka.app';
276
+ return `${base}/system/avatars/${lolkaDefaultAvatarIndex(this.id)}.jpg`;
279
277
  }
280
278
 
281
279
  /**
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ // FNV-1a хеш от десятичной строки id % 6 — та же формула, что в веб/мобильном клиенте Lolka
4
+ // (client/common/ui/Avatar/utils/color.ts), чтобы дефолтная аватарка совпадала во всех клиентах.
5
+ function lolkaDefaultAvatarIndex(id) {
6
+ let h = 2166136261;
7
+ const s = String(id);
8
+ for (let i = 0; i < s.length; i++) {
9
+ h = Math.imul(h ^ s.charCodeAt(i), 16777619);
10
+ }
11
+ return (h >>> 0) % 6;
12
+ }
13
+
14
+ module.exports = { lolkaDefaultAvatarIndex };