discwoofer 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +215 -2
- package/dist/index.cjs +1884 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +368 -0
- package/dist/index.d.mts +368 -0
- package/dist/index.mjs +1883 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +94 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 stepskop
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,216 @@
|
|
|
1
|
-
#
|
|
1
|
+
# discwoofer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Drop-in music player for [discord.js](https://discord.js.org) bots. One class adds slash commands, a queue and an optional interactive "now playing" panel. It plays YouTube, SoundCloud and Spotify links, and plain text searches.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { MusicPlayer } from 'discwoofer';
|
|
7
|
+
|
|
8
|
+
const music = new MusicPlayer(client, {
|
|
9
|
+
sources: ['youtube', 'soundcloud'],
|
|
10
|
+
commands: { register: 'global' },
|
|
11
|
+
});
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
- Node.js **22.12+** (or Bun)
|
|
17
|
+
- `discord.js` 14 and `@discordjs/voice` 0.19 (peer dependencies)
|
|
18
|
+
- An Opus encoder: `@discordjs/opus` (native, faster) **or** `opusscript` (pure JS)
|
|
19
|
+
- FFmpeg: `ffmpeg-static` **or** `ffmpeg` on your `PATH`
|
|
20
|
+
- [yt-dlp](https://github.com/yt-dlp/yt-dlp#installation) on your `PATH`. It streams the audio. Keep it updated (`yt-dlp -U`), since YouTube changes often.
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
pnpm add discwoofer discord.js @discordjs/voice @discordjs/opus ffmpeg-static
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
With pnpm 10+, allow the build scripts of `@discordjs/opus` and `ffmpeg-static` (`pnpm approve-builds`), or they won't work.
|
|
27
|
+
|
|
28
|
+
Your client needs the `Guilds` and `GuildVoiceStates` intents.
|
|
29
|
+
|
|
30
|
+
## What it plays
|
|
31
|
+
|
|
32
|
+
| Input | How |
|
|
33
|
+
|---|---|
|
|
34
|
+
| Text | Searched on your `sources` in order; the first source with results wins |
|
|
35
|
+
| YouTube link (video, playlist, YouTube Music) | Played from YouTube |
|
|
36
|
+
| SoundCloud link (track, set) | Played from SoundCloud |
|
|
37
|
+
| Spotify link (track, album, playlist) | Spotify doesn't allow streaming, so each song is searched on your `sources` in order |
|
|
38
|
+
|
|
39
|
+
**`sources`** sets the fallback order (default `['youtube', 'soundcloud']`). When a YouTube or SoundCloud track can't be played (blocked, removed, region-locked), the same song is searched on the next source. Use `['soundcloud', 'youtube']` to prefer SoundCloud, or a single entry to use only that one.
|
|
40
|
+
|
|
41
|
+
**Spotify credentials are optional.** Links work without them, but playlists and albums are limited to their first 100 tracks. With [app credentials](https://developer.spotify.com/dashboard) (`spotify: { clientId, clientSecret }`) there's no limit.
|
|
42
|
+
|
|
43
|
+
## Commands
|
|
44
|
+
|
|
45
|
+
| Command | |
|
|
46
|
+
|---|---|
|
|
47
|
+
| `/play query [next]` | Link or search terms. Autocomplete shows results as you type. `next` puts it at the front of the queue. |
|
|
48
|
+
| `/skip [to]` | Skip, or jump to a queue position |
|
|
49
|
+
| `/stop` | Stop and clear the queue |
|
|
50
|
+
| `/pause`, `/resume` | |
|
|
51
|
+
| `/queue [page]` | Show the queue |
|
|
52
|
+
| `/nowplaying` | Current track with progress |
|
|
53
|
+
| `/volume [level]` | 0-200% |
|
|
54
|
+
| `/loop [mode]` | `off` / `track` / `queue` (cycles when omitted) |
|
|
55
|
+
| `/shuffle`, `/clear`, `/remove position`, `/leave` | |
|
|
56
|
+
|
|
57
|
+
Commands only work in servers. Control commands require the user to be in the bot's voice channel.
|
|
58
|
+
|
|
59
|
+
### UI modes
|
|
60
|
+
|
|
61
|
+
- **`commands`** (default): plain slash commands. Replies are public and each new track is announced in the channel.
|
|
62
|
+
- **`widgets`**: a now-playing panel with buttons (play/pause, skip, stop, shuffle, loop, volume, queue) that updates as the state changes. Typing a search into `/play` (without picking an autocomplete result) shows a menu to choose from. Command replies are ephemeral, since the panel shows the state.
|
|
63
|
+
|
|
64
|
+
## Options
|
|
65
|
+
|
|
66
|
+
All options are optional.
|
|
67
|
+
|
|
68
|
+
| Option | Default | |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| `sources` | `['youtube', 'soundcloud']` | Where audio comes from, in fallback order |
|
|
71
|
+
| `ui` | `'commands'` | `'commands'` or `'widgets'` |
|
|
72
|
+
| `channels.text` | any | Channel ids, or `(channel) => boolean`, where commands are allowed |
|
|
73
|
+
| `channels.voice` | any | Channel ids, or `(channel) => boolean`, that the bot may join |
|
|
74
|
+
| `commands.group` | none | Register everything under one command, e.g. `'music'` gives `/music play` |
|
|
75
|
+
| `commands.names` | none | Rename commands: `{ nowplaying: 'np' }` |
|
|
76
|
+
| `commands.disabled` | none | Leave commands out: `['shuffle']` |
|
|
77
|
+
| `commands.register` | `false` | `'global'` or `{ guildIds }` to register on ready |
|
|
78
|
+
| `spotify` | none | `{ clientId, clientSecret }` for Spotify playlists over 100 tracks |
|
|
79
|
+
| `ytdlp.path` | `$YTDLP_PATH` or `yt-dlp` | Path to the yt-dlp binary |
|
|
80
|
+
| `ytdlp.args` | none | Extra yt-dlp arguments, e.g. `['--cookies', 'cookies.txt']` |
|
|
81
|
+
| `listen` | `true` | Attach an `interactionCreate` listener. Set to `false` and call `handleInteraction()` yourself. |
|
|
82
|
+
| `canUse` | none | `(interaction) => boolean \| string`. Return `false` or a message to deny. |
|
|
83
|
+
| `defaultVolume` | `80` | Percent |
|
|
84
|
+
| `maxQueueSize` | `500` | |
|
|
85
|
+
| `maxPlaylistSize` | `200` | |
|
|
86
|
+
| `searchLimit` | `5` | Results shown in the picker and autocomplete |
|
|
87
|
+
| `leaveOnEmpty` | `60` | Seconds to stay in an empty channel, or `false` to stay |
|
|
88
|
+
| `leaveOnFinish` | `300` | Seconds to stay after the queue ends, or `false` to stay |
|
|
89
|
+
| `announceTracks` | `true` | Post "Now playing" messages in commands mode |
|
|
90
|
+
| `embedColor` | `0x5865f2` | |
|
|
91
|
+
| `customIdPrefix` | `'dw'` | Prefix for button and menu ids |
|
|
92
|
+
|
|
93
|
+
## Example bot
|
|
94
|
+
|
|
95
|
+
[`examples/bot.ts`](./examples/bot.ts) is a complete bot that only needs a token. It plays from YouTube with SoundCloud as fallback, uses the widgets panel, and registers global commands. To change any of that, edit the `MusicPlayer` options in the file.
|
|
96
|
+
|
|
97
|
+
It runs on [Bun](https://bun.sh) straight from the TypeScript sources, with no build step. Bun is a dev dependency, so you don't need it installed globally.
|
|
98
|
+
|
|
99
|
+
1. In the [Discord developer portal](https://discord.com/developers/applications), create an application, open **Bot**, and copy the token.
|
|
100
|
+
2. Install [yt-dlp](https://github.com/yt-dlp/yt-dlp#installation) (`brew install yt-dlp`, `pipx install yt-dlp`, …).
|
|
101
|
+
3. Create a `.env` file in the repository root with your token. Bun loads it automatically.
|
|
102
|
+
|
|
103
|
+
```sh
|
|
104
|
+
DISCORD_TOKEN=your-bot-token
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
4. Run it:
|
|
108
|
+
|
|
109
|
+
```sh
|
|
110
|
+
pnpm install
|
|
111
|
+
pnpm bot # or `pnpm bot:dev` to restart on file changes
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
On startup the bot prints an invite link with the permissions it needs. Open it to add the bot to your server, join a voice channel, and run `/play`.
|
|
115
|
+
|
|
116
|
+
## Using it in an existing bot
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
const music = new MusicPlayer(client, {
|
|
120
|
+
commands: { group: 'music' }, // `/music play` etc., so nothing clashes with your commands
|
|
121
|
+
listen: false,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Registration: merge the music commands into your own bulk registration...
|
|
125
|
+
await rest.put(Routes.applicationCommands(appId), { body: [...myCommands, ...music.commandData] });
|
|
126
|
+
// ...or let the player create/update only its own commands:
|
|
127
|
+
await music.registerCommands(); // or registerCommands([guildId])
|
|
128
|
+
|
|
129
|
+
// Routing:
|
|
130
|
+
client.on('interactionCreate', async (interaction) => {
|
|
131
|
+
if (await music.handleInteraction(interaction)) return;
|
|
132
|
+
// your handlers
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`registerCommands()` creates or updates the music commands one by one and leaves your other commands alone. Don't call it on every start in production, because command creation is rate limited. See [`examples/existing-bot.ts`](./examples/existing-bot.ts).
|
|
137
|
+
|
|
138
|
+
## Programmatic API
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const { player, tracks } = await music.play(voiceChannel, 'daft punk around the world', {
|
|
142
|
+
textChannel,
|
|
143
|
+
requestedBy: { id: user.id, name: user.username },
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const player = music.get(guildId);
|
|
147
|
+
player?.pause();
|
|
148
|
+
player?.skip();
|
|
149
|
+
player?.setVolume(50);
|
|
150
|
+
player?.setLoop('queue');
|
|
151
|
+
player?.queue.upcoming; // Track[]
|
|
152
|
+
player?.current; // Track | null
|
|
153
|
+
player?.destroy(); // leave
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Events
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
music.on('trackStart', (player, track) => {});
|
|
160
|
+
music.on('trackEnd', (player, track) => {});
|
|
161
|
+
music.on('trackAdd', (player, tracks, playlist) => {});
|
|
162
|
+
music.on('queueEnd', (player) => {});
|
|
163
|
+
music.on('disconnect', (player) => {});
|
|
164
|
+
music.on('error', (error, player, track) => {}); // without a listener, errors are logged to the console
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Troubleshooting
|
|
168
|
+
|
|
169
|
+
- **No sound, or `Cannot find module '@discordjs/opus'`**: install an Opus encoder and FFmpeg. Run `console.log(generateDependencyReport())` (from `@discordjs/voice`) to see what is missing.
|
|
170
|
+
- **`yt-dlp not found`**: install yt-dlp, or set `ytdlp.path` / `YTDLP_PATH`.
|
|
171
|
+
- **YouTube errors such as "Sign in to confirm you're not a bot"**: update yt-dlp first. On server IPs you may need cookies: `ytdlp: { args: ['--cookies', 'cookies.txt'] }`. Add `'soundcloud'` to `sources` so songs fall back to SoundCloud meanwhile.
|
|
172
|
+
- **A Spotify playlist stops at 100 tracks**: add Spotify credentials.
|
|
173
|
+
|
|
174
|
+
## Development
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
pnpm install
|
|
178
|
+
pnpm test # vitest
|
|
179
|
+
pnpm typecheck
|
|
180
|
+
pnpm build # tsdown -> dist (ESM + CJS + .d.ts)
|
|
181
|
+
pnpm bot # example bot on Bun, see "Example bot"
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Source layout:
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
src/
|
|
188
|
+
MusicPlayer.ts public class: options, events, command registration
|
|
189
|
+
options.ts option types and defaults
|
|
190
|
+
types.ts Track, Platform, ...
|
|
191
|
+
player/ per-server playback (GuildPlayer) and the queue
|
|
192
|
+
sources/ link detection, YouTube, SoundCloud, Spotify, yt-dlp, fallback order
|
|
193
|
+
commands/ slash command definitions and handlers, buttons, autocomplete
|
|
194
|
+
ui/ embeds, panel and message formatting
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Releasing
|
|
198
|
+
|
|
199
|
+
CI (`.github/workflows/ci.yml`) runs typecheck, tests and build on every pull request and push. On `main`, it also publishes to npm when the `version` in `package.json` isn't on npm yet, then tags the commit and creates a GitHub release. Versions with a pre-release suffix (`1.0.0-beta.1`) are published under the `next` tag.
|
|
200
|
+
|
|
201
|
+
To release, bump the version and merge to `main`:
|
|
202
|
+
|
|
203
|
+
```sh
|
|
204
|
+
npm version patch # or minor / major / prerelease --preid beta
|
|
205
|
+
git push --follow-tags
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Publishing uses [npm trusted publishing](https://docs.npmjs.com/trusted-publishers), so no npm token is stored in GitHub. One-time setup:
|
|
209
|
+
|
|
210
|
+
1. npm can't trust-publish a package that doesn't exist yet, so publish the first version by hand: `npm login`, then `npm publish --access public` (runs typecheck, tests and build first).
|
|
211
|
+
2. On npmjs.com, open the package → **Settings** → **Trusted publishing** → **GitHub Actions**, and enter owner `stepskop`, repository `discwoofer`, workflow `ci.yml`. A new trusted publisher must publish within 2 days, or it expires.
|
|
212
|
+
3. Optional: under **Publishing access**, require two-factor authentication and disallow tokens, so only CI can publish.
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
MIT
|