Introduction to Moonlink
Moonlink is a powerful JavaScript library designed to streamline various functionalities related to player and node management. This guide provides an overview of its structure and usage to help you integrate Moonlink seamlessly into your application.
Understanding the Structure
At the core of Moonlink is the MoonlinkManager
class, which serves as the primary interface for interacting with its features. This class encapsulates essential functionalities, including player and node management, ensuring a cohesive experience for developers.
Player and Node Management
One of the key aspects of Moonlink is its modular approach to player and node management. By dividing responsibilities into distinct classes, such as PlayerManager
and NodeManager
, Moonlink enhances clarity and organization within your application.
Players
Players represent entities responsible for audio playback within a specific context, such as a Discord server. The PlayerManager
class handles player-related operations, offering functions for retrieving existing players, creating new ones, and accessing player-specific functionalities.
Retrieving Players
To retrieve a player associated with a particular Discord server, you can use the get
method:
<moonlink>.players.get("myGuildIdDiscord")
Creating Players
Creating a new player involves specifying relevant parameters, such as the Discord guild ID and associated voice/text channels:
<moonlink>.players.create({
guildId: "id",
voiceChannel: "voiceId",
textChannel: "textId"
})
Accessing All Players
You can also retrieve all existing players within the application:
<moonlink>.players.all
Node Management
Nodes are essential components in Moonlink responsible for handling audio streams and distributing workload efficiently. The NodeManager
class facilitates node management operations, offering functionalities for adding, retrieving, and removing nodes.
Adding Nodes
To add a new node, you can use the add
method, specifying parameters such as host, password, and port:
<moonlink>.nodes.add({
host: "example.com",
password: "securePassword",
port: 2333,
// Other parameters...
})
Retrieving Nodes
You can retrieve a node using either its host or identifier:
<moonlink>.nodes.get("example.com")
Removing Nodes
Removing a node from the system is straightforward using the remove
method:
<moonlink>.nodes.remove("example.com")
Initialization and Packet Updates
Before utilizing Moonlink, it's essential to initialize the package and handle packet updates. Initialization ensures proper synchronization with the client, while packet updates enable seamless integration with audio streaming servers.
<moonlink>.init(clientId)
To handle packet updates, listen for raw data events from the client and pass them to Moonlink:
<client>.on("raw", (data) => <moonlink>.packetUpdate(data))
Understanding Lavalink
Lavalink serves as a simplified audio playback server, functioning as both an API and a WebSocket server for manipulating audio streams. It interfaces with LavaPlayer, which manages audio sources and related functionalities. Additionally, it includes Koe, similar to @discordjs/Voice
, facilitating connections with Discord for audio transmission.