Lavalink and voice support for interactions.py
Project description
interactions-lavalink
Installation
- Download Java if you don't have it
- Download lavalink from this repo
- Configure
application.yml
file like here - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for interactions-lavalink-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d032a233ca5ae31c6f41ec6907c5072d469caaf62a606b5157496bf4367e0ccb |
|
MD5 | 5dfd997731687139a6561c39cff2a6af |
|
BLAKE2b-256 | bfa0f75ea0e6dafe49951d20f15242293d8575b5a010c9080ed39928c147b504 |
Close
Hashes for interactions_lavalink-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c502b51ea8429cebd48fb3d64e06b06b497635a3d06022e979a53f709d9bbcf5 |
|
MD5 | 3a5fdb1ac291d0c5e60bd7017966f62e |
|
BLAKE2b-256 | d2ff1bfa41f2b5090f4fa8e9d902f2cd42f55566baa720201f0c530b4f6becf5 |