Lavalink support for interactions.py
Project description
interactions-lavalink
Installation
Download ext via pip install --upgrade interactions-lavalink
Configuring own lavalink server
- Download Java SE if you don't have it
- Download lavalink from this repo
- Configure
application.yml
file like here - Run lavalink server via
java -jar Lavalink.jar
in same folder withapplication.yml
file.
Usage
Create bot like example and run it.
Main file:
from interactions import Client
# Creating bot variable
client = Client()
# Loading your extension
client.load("exts.music")
# Starting bot
client.start("TOKEN")
Extension file: exts/music.py
from interactions import Extension, SlashContext, listen, slash_command, slash_option
from interactions_lavalink import Lavalink
from interactions_lavalink.events import TrackStart
class Music(Extension):
def __init__(self, client):
self.client = client
self.lavalink: Lavalink | None = None
@listen()
async def on_startup(self):
# Initializing lavalink instance on bot startup
self.lavalink: Lavalink = Lavalink(self.client)
# Connecting to local lavalink server
self.lavalink.add_node("127.0.0.1", 43421, "your_password", "eu")
@listen()
async def on_track_start(self, event: TrackStart):
print("Track started", event.track.title)
@slash_command()
@slash_option("query", "The search query or url", opt_type=3, required=True)
async def play(self, ctx: SlashContext, query: str):
await ctx.defer()
# Getting user's voice state
voice_state = ctx.author.voice
if not voice_state or not voice_state.channel:
return await ctx.send("You're not connected to the voice channel!")
# Connecting to voice channel and getting player instance
player = await self.lavalink.connect(voice_state.guild.id, voice_state.channel.id)
# Getting tracks from youtube
tracks = await player.search_youtube(query)
track = tracks[0]
# Adding track to the queue
player.add(requester=int(ctx.author.id), track=track)
# Check if player is already playing
if player.is_playing:
return await ctx.send(f"Added to queue: `{track.title}`")
# Starting playing track
await player.play()
await ctx.send(f"Now playing: `{track.title}`")
@slash_command()
async def leave(self, ctx: SlashContext):
# Disconnecting from voice channel
await self.lavalink.disconnect(ctx.guild.id)
await ctx.send("Disconnected", ephemeral=True)
Events
To listen lavalink event you have to use @listen
decorator.
from interactions import Extension, listen
from interactions_lavalink import TrackStart, TrackEnd, QueueEnd
class MusicExt(Extension):
... # Some your cool music commands
# There are many useful events for you. You can use other events if you want it.
@listen()
async def on_track_start(self, event: TrackStart):
"""Fires when track starts"""
print(f"Track {event.track.title} started")
@listen()
async def on_track_end(self, event: TrackEnd):
"""Fires when track ends"""
@listen()
async def on_queue_end(self, event: QueueEnd):
"""Fires when queue ends"""
More events you could find in the lavalink.py
documentation
Documentation
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-3.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0de870d9c076c6370d53184c792fb52d4b86fd615a4cbafdc0d1f379370c5547 |
|
MD5 | 34ccdb4b9e053a0e649f547c4f0b72cc |
|
BLAKE2b-256 | a8082ae672de8ef1329c02611b8c53c10ad07cecae080f428ffb6b681292b90a |
Close
Hashes for interactions_lavalink-3.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 901f3f9321d9b54e2cdcdc66e5ca715ebf804df8bca110a1eed71be6cbd8efdf |
|
MD5 | 328fd8e737b021e92b0ef54bfb494b01 |
|
BLAKE2b-256 | e57f99f3e4925e9390a725d4cbe5e49c1d318e44910c54049cbcd0165ca5bd0e |