Lavalink and voice 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()
Extension file: exts/music.py
from interactions import Extension, extension_command, extension_listener, option, CommandContext, VoiceState
from interactions.ext.lavalink import Lavalink
class Music(Extension):
def __init__(self, client):
self.client = client
self.lavalink: Lavalink = None
@extension_listener()
async def on_start(self):
# Initialize lavalink instance
self.lavalink: Lavalink = Lavalink(self.client)
# Connect to lavalink server
self.lavalink.add_node("127.0.0.1", 43421, "your_password", "eu")
@extension_command()
@option()
async def play(self, ctx: CommandContext, query: str):
await ctx.defer()
# Getting user's voice state
voice_state: VoiceState = ctx.author.voice_state
if not voice_state or not voice_state.joined:
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)
# Selecting first founded track
track = tracks[0]
# Adding track to the queue
player.add(requester=int(ctx.author.id), track=track)
# Check if 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}`")
@extension_command()
async def leave(self, ctx: CommandContext):
# Disconnect from voice channel and remove player
await self.lavalink.disconnect(ctx.guild_id)
await ctx.send("Disconnected", ephemeral=True)
def setup(client):
Music(client)
Events
To listen lavalink event you have to use either @bot.event
or @extension_listener
decorator.
from interactions import Extension, extension_listener
import lavalink
class MusicExt(Extension):
... # Some your cool music commands
# There are many useful events for you. You can use other events if you want it.
@extension_listener()
async def on_track_start(self, event: lavalink.TrackStartEvent):
"""Fires when track starts"""
@extension_listener()
async def on_track_end(self, event: lavalink.TrackEndEvent):
"""Fires when track ends"""
@extension_listener()
async def on_queue_end(self, event: lavalink.QueueEndEvent):
"""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-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | dae4aad2d3a528a99a7e179f3add7ccb18d2974bde5c8da890d597aef5d23f4d |
|
MD5 | b76cf4eb27c88242f72ddf88ceb3bda0 |
|
BLAKE2b-256 | 5ec832a1979da0ce331d6b621862cfa990599140abcab2f3e35babcbb98aceab |
Close
Hashes for interactions_lavalink-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fda9090ca6692eaf361965646e14658ff7d3faa3c11807165b4e4ea91b0159cf |
|
MD5 | 965f2bea09d153242792381697dc0445 |
|
BLAKE2b-256 | 505f5f43bbce50909e14bec846f8761c8f98e91029c53f2d4687ce10db05a81c |