Skip to main content

Lavalink and voice support for interactions.py

Project description

interactions-lavalink

Installation

  1. Download Java if you don't have it
  2. Download lavalink from this repo
  3. Configure application.yml file like here
  4. Download ext via pip install interactions-lavalink

Usage

Run lavalink via java -jar Lavalink.jar in same folder with application.yml file. Create bot like example and run it.

Main file:

import interactions
from interactions.ext.lavalink import VoiceState, VoiceClient

client = VoiceClient(...)

client.load("exts.music")

client.start()

Extension file: exts/music.py

import interactions
from interactions.ext.lavalink import VoiceClient, VoiceState, listener, Player
import lavalink


class Music(interactions.Extension):
    def __init__(self, client):
        self.client: VoiceClient = client

    @listener()
    async def on_track_start(self, event: lavalink.TrackStartEvent):
        """
        Fires when track starts
        """
        print("STARTED", event.track)

    @interactions.extension_listener()
    async def on_start(self):
        self.client.lavalink_client.add_node("127.0.0.1", 43421, "your_password", "eu")

    @interactions.extension_listener()
    async def on_voice_state_update(self, before: VoiceState, after: VoiceState):
        """
        Disconnect if bot is alone
        """
        if before and not after.joined:
            voice_states = self.client.get_channel_voice_states(before.channel_id)
            if len(voice_states) == 1 and voice_states[0].user_id == self.client.me.id:
                await self.client.disconnect(before.guild_id)

    @interactions.extension_command()
    @interactions.option()
    async def play(self, ctx: interactions.CommandContext, query: str):
        await ctx.defer()

        # NOTE: ctx.author.voice can be None if you ran a bot after joining the voice channel
        voice: VoiceState = ctx.author.voice
        if not voice or not voice.joined:
            return await ctx.send("You're not connected to the voice channel!")

        player: Player  # Typehint player variable to see their methods
        if (player := ctx.guild.player) is None:
            player = await voice.connect()

        tracks = await player.search_youtube(query)
        track = tracks[0]
        player.add(requester=int(ctx.author.id), track=track)

        if player.is_playing:
            return await ctx.send(f"Added to queue: `{track.title}`")
        await player.play()
        await ctx.send(f"Now playing: `{track.title}`")

    @interactions.extension_command()
    async def leave(self, ctx: interactions.CommandContext):
        await self.client.disconnect(ctx.guild_id)

Events

To listen lavalink event you have to use @listener decorator.

import lavalink
from interactions.ext.lavalink import listener


# NOTE: Works only in extensions.
class MusicExt(Extension):
    ...

    # There are most useful events for you. You can use other events if you want it.
    @listener()
    async def on_track_start(self, event: lavalink.TrackStartEvent):
        """Fires when track starts"""

    @listener()
    async def on_track_end(self, event: lavalink.TrackEndEvent):
        """Fires when track ends"""

    @listener()
    async def on_queue_end(self, event: lavalink.QueueEndEvent):
        """Fires when queue ends"""

New methods/properties for interactions.py library

Member.voice - returns current member's VoiceState. It can be None if not cached.
Channel.voice_states - returns a list of voice states of the voice channel. Can be empty if not cached.
Guild.voice_states - returns a list of guild voice states. Can be empty if not cached.

Documentation

lavalink.py documentation
lavalink.py repository

Credits

Thanks EdVraz for VoiceState from voice ext

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

interactions-lavalink-0.1.0.tar.gz (21.0 kB view hashes)

Uploaded Source

Built Distribution

interactions_lavalink-0.1.0-py3-none-any.whl (21.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page