Skip to content

Commit

Permalink
merge: v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Damego authored May 18, 2023
2 parents c286af0 + 7d7ea3b commit 577c086
Show file tree
Hide file tree
Showing 15 changed files with 1,154 additions and 296 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,3 @@ dmypy.json

logs/*
Lavalink.jar

2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include requirements.txt
recursive-include interactions_lavalink *
73 changes: 36 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,97 +21,96 @@ from interactions import Client


# Creating bot variable
client = Client(...)
client = Client()

# Loading your extension
client.load("exts.music")

# Starting bot
client.start()
client.start("TOKEN")
```

Extension file: `exts/music.py`
```python
from interactions import Extension, extension_command, extension_listener, option, CommandContext, VoiceState
from interactions.ext.lavalink import Lavalink
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
self.lavalink: Lavalink | None = None

@extension_listener()
async def on_start(self):
# Initialize lavalink instance
@listen()
async def on_startup(self):
# Initializing lavalink instance on bot startup
self.lavalink: Lavalink = Lavalink(self.client)

# Connect to lavalink server
# Connecting to local 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):
@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: VoiceState = ctx.author.voice_state
if not voice_state or not voice_state.joined:
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)

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
# 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}`")

@extension_command()
async def leave(self, ctx: CommandContext):
# Disconnect from voice channel and remove player
await self.lavalink.disconnect(ctx.guild_id)
@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)


def setup(client):
Music(client)

```

## Events
To listen lavalink event you have to use either `@bot.event` or `@extension_listener` decorator.
To listen lavalink event you have to use `@listen` decorator.

```python
from interactions import Extension, extension_listener

import lavalink
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.
@extension_listener()
async def on_track_start(self, event: lavalink.TrackStartEvent):
@listen()
async def on_track_start(self, event: TrackStart):
"""Fires when track starts"""
print(f"Track {event.track.title} started")

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

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

```
Expand Down
10 changes: 4 additions & 6 deletions examples/bot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from interactions import Intents
from interactions.ext.lavalink import VoiceClient
from interactions import Client

client = VoiceClient("TOKEN", intents=Intents.DEFAULT)
client = Client()

client.load("exts.music")

client.start()
client.load_extension("exts.music")
client.start("TOKEN")
57 changes: 25 additions & 32 deletions examples/exts/music.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,55 @@
from interactions import (
CommandContext,
Extension,
VoiceState,
extension_command,
extension_listener,
option,
)
from interactions.ext.lavalink import Lavalink
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
self.lavalink: Lavalink | None = None

@extension_listener()
async def on_start(self):
# Initialize lavalink instance
@listen()
async def on_startup(self):
# Initializing lavalink instance on bot startup
self.lavalink: Lavalink = Lavalink(self.client)

# Connect to lavalink server
# Connecting to local 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):
@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: VoiceState = ctx.author.voice_state
if not voice_state or not voice_state.joined:
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)

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
# 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}`")

@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)

@slash_command()
async def leave(self, ctx: SlashContext):
# Disconnecting from voice channel
await self.lavalink.disconnect(ctx.guild.id)

def setup(client):
Music(client)
await ctx.send("Disconnected", ephemeral=True)
17 changes: 0 additions & 17 deletions interactions/ext/lavalink/base.py

This file was deleted.

Loading

0 comments on commit 577c086

Please sign in to comment.