smusic-discord 1.0.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.
Files changed (3) hide show
  1. package/README.md +107 -0
  2. package/dist/index.js +33614 -0
  3. package/package.json +45 -0
package/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # smusic-discord
2
+
3
+ A fast, reliable, and extensible Node.js package built with Bun to resolve music sources (YouTube, SoundCloud, direct URLs) and return a stream compatible with Discord voice bots using `@discordjs/voice`.
4
+
5
+ ## Installation
6
+
7
+ You must use Bun.
8
+
9
+ ```bash
10
+ bun add smusic-discord
11
+ ```
12
+
13
+ ## Features
14
+
15
+ - **Fast Metadata Resolution:** Uses `play-dl` for quick resolution.
16
+ - **Multiple Source Support:** Works out-of-the-box with YouTube, SoundCloud, and Direct Audio URLs.
17
+ - **Search Support:** Natively search YouTube if the input is a query instead of a URL.
18
+ - **Discord Compatibility:** Provides a Node.js Readable stream ready to be used with `@discordjs/voice`.
19
+ - **Fallback Strategy for YouTube:** Employs multiple fallback layers (`play-dl` -> `ytdl-core` -> `yt-dlp`) to ensure playback consistency.
20
+ - **Built-in Caching:** Prevents redundant metadata API calls by caching resolved tracks.
21
+
22
+ ## Usage
23
+
24
+ ### Example Discord Bot Setup
25
+
26
+ ```typescript
27
+ import { resolve, search } from 'smusic-discord';
28
+ import { createAudioResource, createAudioPlayer, joinVoiceChannel } from '@discordjs/voice';
29
+ import { Client, GatewayIntentBits, Message } from 'discord.js';
30
+
31
+ const client = new Client({
32
+ intents: [
33
+ GatewayIntentBits.Guilds,
34
+ GatewayIntentBits.GuildMessages,
35
+ GatewayIntentBits.MessageContent,
36
+ GatewayIntentBits.GuildVoiceStates,
37
+ ],
38
+ });
39
+
40
+ client.on('messageCreate', async (message: Message) => {
41
+ if (!message.content.startsWith('!play') || !message.member?.voice.channel) return;
42
+
43
+ const query = message.content.replace('!play ', '');
44
+
45
+ // Join VC
46
+ const connection = joinVoiceChannel({
47
+ channelId: message.member.voice.channel.id,
48
+ guildId: message.guildId!,
49
+ adapterCreator: message.guild!.voiceAdapterCreator,
50
+ });
51
+
52
+ try {
53
+ // Resolve the query or URL
54
+ const track = await resolve(query);
55
+ console.log(`Now playing: ${track.title}`);
56
+
57
+ // Get the audio stream
58
+ const stream = await track.stream();
59
+
60
+ // Play it using discordjs/voice
61
+ const resource = createAudioResource(stream);
62
+ const player = createAudioPlayer();
63
+
64
+ player.play(resource);
65
+ connection.subscribe(player);
66
+ } catch (error) {
67
+ console.error('Failed to play track:', error);
68
+ }
69
+ });
70
+
71
+ client.login('YOUR_BOT_TOKEN');
72
+ ```
73
+
74
+ ## API Documentation
75
+
76
+ ### `resolve(input: string): Promise<Track>`
77
+
78
+ Resolves a given URL or attempts a Youtube search if it's plain text. Returns a `Track` object.
79
+
80
+ ### `search(query: string): Promise<Track>`
81
+
82
+ Forces a search on YouTube and returns the first result as a `Track`.
83
+
84
+ ### `Track` Object
85
+
86
+ ```typescript
87
+ interface Track {
88
+ title: string;
89
+ url: string;
90
+ duration: number; // in seconds
91
+ thumbnail?: string;
92
+ source: string; // 'youtube', 'soundcloud', 'direct', etc.
93
+
94
+ stream(): Promise<Readable>;
95
+ }
96
+ ```
97
+
98
+ ## Build and Publish
99
+
100
+ ```bash
101
+ bun build src/index.ts --outdir dist --target node
102
+ bun publish
103
+ ```
104
+
105
+ ## License
106
+
107
+ MIT