DisTube class

Hierarchy

Constructors

  • Parameters

    • client: Client<boolean>
    • opts: DisTubeOptions & {
          youtubeCookie: string;
      }

    Returns DisTube

    Deprecated

    Use youtubeCookie: Cookie[] instead. Guide: YouTube Cookies

  • Create a new DisTube class.

    Parameters

    • client: Client<boolean>

      Discord.JS client

    • Optional opts: DisTubeOptions

      Custom DisTube options

    Returns DisTube

    Example

    const Discord = require('discord.js'),
    DisTube = require('distube'),
    client = new Discord.Client();
    // Create a new DisTube
    const distube = new DisTube.default(client, { searchSongs: 10 });
    // client.DisTube = distube // make it access easily
    client.login("Your Discord Bot Token")
    ```ts

    Throws

    DisTubeError

Properties

client: Client<boolean>
customPlugins: CustomPlugin[]
extractorPlugins: ExtractorPlugin[]
filters: Filters
options: Options
queues: QueueManager
defaultMaxListeners: number

Accessors

Methods

  • Type Parameters

    Parameters

    Returns this

  • Create a custom playlist

    Parameters

    Returns Promise<Playlist<unknown>>

    Example

    const songs = ["https://www.youtube.com/watch?v=xxx", "https://www.youtube.com/watch?v=yyy"];
    const playlist = await distube.createCustomPlaylist(songs, {
    member: message.member,
    properties: { name: "My playlist name", source: "custom" },
    parallel: true
    });
    distube.play(voiceChannel, playlist, { ... });
    ```ts
  • Type Parameters

    Parameters

    Returns boolean

  • Emit error event

    Parameters

    • error: Error

      error

    • Optional channel: GuildTextBasedChannel

      Text channel where the error is encountered.

    Returns void

  • Type Parameters

    Returns U[]

  • Returns number

  • Get the guild queue

    Parameters

    Returns undefined | Queue

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "queue") {
    const queue = distube.getQueue(message);
    message.channel.send('Current queue:\n' + queue.songs.map((song, id) =>
    `**${id+1}**. [${song.name}](${song.url}) - \`${song.formattedDuration}\``
    ).join("\n"));
    }
    });
    ```ts
  • Jump to the song number in the queue. The next one is 1, 2,... The previous one is -1, -2,...

    Parameters

    Returns Promise<Song<unknown>>

    The new Song will be played

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "jump")
    distube.jump(message, parseInt(args[0]))
    .catch(err => message.channel.send("Invalid song number."));
    });
    ```ts
  • Parameters

    Returns number

  • Type Parameters

    Parameters

    • type: U

    Returns TypedDisTubeEvents[U][]

  • Type Parameters

    Parameters

    Returns this

  • Type Parameters

    Parameters

    Returns this

  • Type Parameters

    Parameters

    Returns this

  • Play / add a song or playlist from url. Search and play a song if it is not a valid url.

    Parameters

    Returns Promise<void>

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "play")
    distube.play(message.member.voice.channel, args.join(" "), {
    member: message.member,
    textChannel: message.channel,
    message
    });
    });
    ```ts

    Throws

    DisTubeError

  • Type Parameters

    Parameters

    Returns this

  • Type Parameters

    Parameters

    Returns this

  • Play the previous song

    Parameters

    Returns Promise<Song<unknown>>

    The new Song will be played

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "previous")
    distube.previous(message);
    });
    ```ts
  • Type Parameters

    Parameters

    • type: U

    Returns TypedDisTubeEvents[U][]

  • Parameters

    • Optional event: keyof DisTubeEvents

    Returns this

  • Type Parameters

    Parameters

    Returns this

  • Search for a song. You can customize how user answers instead of send a number. Then use DisTube#play to play it.

    Parameters

    • string: string

      The string search for

    • Optional options: {
          limit?: number;
          retried?: boolean;
          safeSearch?: boolean;
          type?: VIDEO;
      }

      Search options

      • Optional limit?: number

        Limit the results

      • Optional retried?: boolean
      • Optional safeSearch?: boolean

        Whether or not use safe search (YouTube restricted mode)

      • Optional type?: VIDEO

        Type of results (video or playlist).

    Returns Promise<SearchResultVideo[]>

    Array of results

  • Search for a song. You can customize how user answers instead of send a number. Then use DisTube#play to play it.

    Parameters

    • string: string

      The string search for

    • options: {
          limit?: number;
          retried?: boolean;
          safeSearch?: boolean;
          type: PLAYLIST;
      }

      Search options

      • Optional limit?: number

        Limit the results

      • Optional retried?: boolean
      • Optional safeSearch?: boolean

        Whether or not use safe search (YouTube restricted mode)

      • type: PLAYLIST

        Type of results (video or playlist).

    Returns Promise<SearchResultPlaylist[]>

    Array of results

  • Search for a song. You can customize how user answers instead of send a number. Then use DisTube#play to play it.

    Parameters

    • string: string

      The string search for

    • Optional options: {
          limit?: number;
          retried?: boolean;
          safeSearch?: boolean;
          type?: SearchResultType;
      }

      Search options

      • Optional limit?: number

        Limit the results

      • Optional retried?: boolean
      • Optional safeSearch?: boolean

        Whether or not use safe search (YouTube restricted mode)

      • Optional type?: SearchResultType

        Type of results (video or playlist).

    Returns Promise<SearchResult[]>

    Array of results

  • Set the playing time to another position

    Parameters

    Returns Queue

    Seeked queue

    Example

    client.on('message', message => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command = 'seek')
    distube.seek(message, Number(args[0]));
    });
    ```ts
  • Parameters

    • n: number

    Returns this

  • Set the repeat mode of the guild queue. Toggle mode (Disabled -> Song -> Queue -> Disabled ->...) if mode is undefined

    Parameters

    • guild: GuildIdResolvable

      The type can be resolved to give a Queue

    • Optional mode: number

      The repeat modes (toggle if undefined)

    Returns number

    The new repeat mode

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "repeat") {
    let mode = distube.setRepeatMode(message, parseInt(args[0]));
    mode = mode ? mode == 2 ? "Repeat queue" : "Repeat song" : "Off";
    message.channel.send("Set repeat mode to `" + mode + "`");
    }
    });
    ```ts

    Example

    const { RepeatMode } = require("distube");
    let mode;
    switch(distube.setRepeatMode(message, parseInt(args[0]))) {
    case RepeatMode.DISABLED:
    mode = "Off";
    break;
    case RepeatMode.SONG:
    mode = "Repeat a song";
    break;
    case RepeatMode.QUEUE:
    mode = "Repeat all queue";
    break;
    }
    message.channel.send("Set repeat mode to `" + mode + "`");
    ```ts
  • Set the guild stream's volume

    Parameters

    • guild: GuildIdResolvable

      The type can be resolved to give a Queue

    • percent: number

      The percentage of volume you want to set

    Returns Queue

    The guild queue

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "volume")
    distube.setVolume(message, Number(args[0]));
    });
    ```ts
  • Shuffle the guild queue songs

    Parameters

    Returns Promise<Queue>

    The guild queue

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "shuffle")
    distube.shuffle(message);
    });
    ```ts
  • Skip the playing song if there is a next song in the queue. If Queue#autoplay is true and there is no up next song, DisTube will add and play a related song.

    Parameters

    Returns Promise<Song<unknown>>

    The new Song will be played

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "skip")
    distube.skip(message);
    });
    ```ts
  • Stop the guild stream

    Parameters

    Returns Promise<void>

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "stop") {
    distube.stop(message);
    message.channel.send("Stopped the queue!");
    }
    });
    ```ts
  • Toggle autoplay mode

    Parameters

    Returns boolean

    Autoplay mode state

    Example

    client.on('message', (message) => {
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();
    if (command == "autoplay") {
    const mode = distube.toggleAutoplay(message);
    message.channel.send("Set autoplay mode to `" + (mode ? "On" : "Off") + "`");
    }
    });
    ```ts

Events

addList: ((queue, playlist) => any)

Emitted after DisTube add a new playlist to the playing Queue.

Type declaration

    • (queue, playlist): any
    • Parameters

      • queue: Queue

        The guild queue

      • playlist: Playlist<unknown>

        Playlist info

      Returns any

Example

distube.on("addList", (queue, playlist) => queue.textChannel.send(
`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to the queue!`
));
addSong: ((queue, song) => any)

Emitted after DisTube add a new song to the playing Queue.

Type declaration

    • (queue, song): any
    • Parameters

      • queue: Queue

        The guild queue

      • song: Song<unknown>

        Added song

      Returns any

Example

distube.on("addSong", (queue, song) => queue.textChannel.send(
`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}.`
));
deleteQueue: ((queue) => any)

Emitted when a Queue is deleted with any reasons.

Type declaration

    • (queue): any
    • Parameters

      • queue: Queue

        The guild queue

      Returns any

disconnect: ((queue) => any)

Emitted when the bot is disconnected to a voice channel.

Type declaration

    • (queue): any
    • Parameters

      • queue: Queue

        The guild queue

      Returns any

empty: ((queue) => any)

Emitted when there is no user in the voice channel, DisTubeOptions.leaveOnEmpty is true and there is a playing queue.

If there is no playing queue (stopped and DisTubeOptions.leaveOnStop is false), it will leave the channel without emitting this event.

Type declaration

    • (queue): any
    • Parameters

      • queue: Queue

        The guild queue

      Returns any

Example

distube.on("empty", queue => queue.textChannel.send("Channel is empty. Leaving the channel"))
error: ((channel, error) => any)

Emitted when DisTube encounters an error while playing songs.

Type declaration

    • (channel, error): any
    • Parameters

      • channel: undefined | GuildTextBasedChannel

        Text channel where the error is encountered.

      • error: Error

        The error encountered

      Returns any

Example

distube.on('error', (channel, e) => {
if (channel) channel.send(`An error encountered: ${e}`)
else console.error(e)
})
ffmpegDebug: ((debug) => any)

Emitted for FFmpeg debugging information.

Type declaration

    • (debug): any
    • Parameters

      • debug: string

        The debug information

      Returns any

finish: ((queue) => any)

Emitted when there is no more song in the queue and Queue#autoplay is false. DisTube will leave voice channel if DisTubeOptions.leaveOnFinish is true.

Type declaration

    • (queue): any
    • Parameters

      • queue: Queue

        The guild queue

      Returns any

Example

distube.on("finish", queue => queue.textChannel.send("No more song in queue"));
finishSong: ((queue, song) => any)

Emitted when DisTube finished a song.

Type declaration

    • (queue, song): any
    • Parameters

      • queue: Queue

        The guild queue

      • song: Song<unknown>

        Finished song

      Returns any

Example

distube.on("finishSong", (queue, song) => queue.textChannel.send(`${song.name} has finished!`));
initQueue: ((queue) => any)

Emitted when DisTube initialize a queue to change queue default properties.

Type declaration

    • (queue): any
    • Parameters

      • queue: Queue

        The guild queue

      Returns any

Example

distube.on("initQueue", queue => {
queue.autoplay = false;
queue.volume = 100;
});
```ts
noRelated: ((queue) => any)

Emitted when Queue#autoplay is true, Queue#songs is empty, and DisTube cannot find related songs to play.

Type declaration

    • (queue): any
    • Parameters

      • queue: Queue

        The guild queue

      Returns any

Example

distube.on("noRelated", queue => queue.textChannel.send("Can't find related video to play."));
```ts
playSong: ((queue, song) => any)

Emitted when DisTube play a song.

If DisTubeOptions.emitNewSongOnly is true, this event is not emitted when looping a song or next song is the previous one.

Type declaration

    • (queue, song): any
    • Parameters

      • queue: Queue

        The guild queue

      • song: Song<unknown>

        Playing song

      Returns any

Example

distube.on("playSong", (queue, song) => queue.textChannel.send(
`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}`
));
```ts
searchCancel: ((message, query) => any)

Emitted when DisTubeOptions.searchSongs bigger than 0, and the search canceled due to DisTubeOptions | DisTubeOptions.searchTimeout.

Type declaration

    • (message, query): any
    • Parameters

      • message: Message<boolean>

        The user message called play method

      • query: string

        The search query

      Returns any

Example

// DisTubeOptions.searchSongs > 0
distube.on("searchCancel", (message) => message.channel.send(`Searching canceled`));
```ts
searchDone: ((message, answer, query) => any)

Emitted when DisTubeOptions.searchSongs bigger than 0, and after the user chose a search result to play.

Type declaration

    • (message, answer, query): any
    • Parameters

      • message: Message<boolean>

        The user message called play method

      • answer: string

        The answered message of user

      • query: string

        The search query

      Returns any

searchInvalidAnswer: ((message, answer, query) => any)

Emitted when DisTubeOptions.searchSongs bigger than 0, and the search canceled due to user's next message is not a number or out of results range.

Type declaration

    • (message, answer, query): any
    • Parameters

      • message: Message<boolean>

        The user message called play method

      • answer: string

        The answered message of user

      • query: string

        The search query

      Returns any

Example

// DisTubeOptions.searchSongs > 0
distube.on("searchInvalidAnswer", (message) => message.channel.send(`You answered an invalid number!`));
```ts
searchNoResult: ((message, query) => any)

Emitted when DisTube cannot find any results for the query.

Type declaration

    • (message, query): any
    • Parameters

      • message: Message<boolean>

        The user message called play method

      • query: string

        The search query

      Returns any

Example

distube.on("searchNoResult", (message, query) => message.channel.send(`No result found for ${query}!`));
```ts
searchResult: ((message, results, query) => any)

Emitted when DisTubeOptions.searchSongs bigger than 0, and song param of DisTube#play is invalid url. DisTube will wait for user's next message to choose a song manually. Safe search is enabled if DisTubeOptions.nsfw is disabled and the message's channel is not a nsfw channel.

Type declaration

    • (message, results, query): any
    • Parameters

      • message: Message<boolean>

        The user message called play method

      • results: SearchResult[]

        Searched results

      • query: string

        The search query

      Returns any

Example

// DisTubeOptions.searchSongs > 0
distube.on("searchResult", (message, results) => {
message.channel.send(`**Choose an option from below**\n${
results.map((song, i) => `**${i + 1}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")
}\n*Enter anything else or wait 60 seconds to cancel*`);
});
```ts