@@ -21,97 +21,96 @@ from interactions import Client
2121
2222
2323# Creating bot variable
24- client = Client(... )
24+ client = Client()
2525
2626# Loading your extension
2727client.load(" exts.music" )
2828
2929# Starting bot
30- client.start()
30+ client.start(" TOKEN " )
3131```
3232
3333Extension file: ` exts/music.py `
3434``` python
35- from interactions import Extension, extension_command, extension_listener, option, CommandContext, VoiceState
36- from interactions.ext.lavalink import Lavalink
35+ from interactions import Extension, SlashContext, listen, slash_command, slash_option
36+
37+ from interactions_lavalink import Lavalink
38+ from interactions_lavalink.events import TrackStart
3739
3840
3941class Music (Extension ):
4042 def __init__ (self , client ):
4143 self .client = client
42- self .lavalink: Lavalink = None
44+ self .lavalink: Lavalink | None = None
4345
44- @extension_listener ()
45- async def on_start (self ):
46- # Initialize lavalink instance
46+ @listen ()
47+ async def on_startup (self ):
48+ # Initializing lavalink instance on bot startup
4749 self .lavalink: Lavalink = Lavalink(self .client)
4850
49- # Connect to lavalink server
51+ # Connecting to local lavalink server
5052 self .lavalink.add_node(" 127.0.0.1" , 43421 , " your_password" , " eu" )
5153
52- @extension_command ()
53- @option ()
54- async def play (self , ctx : CommandContext, query : str ):
54+ @listen ()
55+ async def on_track_start (self , event : TrackStart):
56+ print (" Track started" , event.track.title)
57+
58+ @slash_command ()
59+ @slash_option (" query" , " The search query or url" , opt_type = 3 , required = True )
60+ async def play (self , ctx : SlashContext, query : str ):
5561 await ctx.defer()
5662
5763 # Getting user's voice state
58- voice_state: VoiceState = ctx.author.voice_state
59- if not voice_state or not voice_state.joined :
64+ voice_state = ctx.author.voice
65+ if not voice_state or not voice_state.channel :
6066 return await ctx.send(" You're not connected to the voice channel!" )
6167
6268 # Connecting to voice channel and getting player instance
63- player = await self .lavalink.connect(voice_state.guild_id, voice_state.channel_id)
64-
69+ player = await self .lavalink.connect(voice_state.guild.id, voice_state.channel.id)
6570 # Getting tracks from youtube
6671 tracks = await player.search_youtube(query)
67- # Selecting first founded track
6872 track = tracks[0 ]
6973 # Adding track to the queue
7074 player.add(requester = int (ctx.author.id), track = track)
7175
72- # Check if already playing
76+ # Check if player is already playing
7377 if player.is_playing:
7478 return await ctx.send(f " Added to queue: ` { track.title} ` " )
7579
7680 # Starting playing track
7781 await player.play()
7882 await ctx.send(f " Now playing: ` { track.title} ` " )
7983
80- @extension_command ()
81- async def leave (self , ctx : CommandContext ):
82- # Disconnect from voice channel and remove player
83- await self .lavalink.disconnect(ctx.guild_id )
84+ @slash_command ()
85+ async def leave (self , ctx : SlashContext ):
86+ # Disconnecting from voice channel
87+ await self .lavalink.disconnect(ctx.guild.id )
8488
8589 await ctx.send(" Disconnected" , ephemeral = True )
86-
87-
88- def setup (client ):
89- Music(client)
90-
9190```
9291
9392## Events
94- To listen lavalink event you have to use either ` @bot.event ` or ` @extension_listener ` decorator.
93+ To listen lavalink event you have to use ` @listen ` decorator.
9594
9695``` python
97- from interactions import Extension, extension_listener
98-
99- import lavalink
96+ from interactions import Extension, listen
97+ from interactions_lavalink import TrackStart, TrackEnd, QueueEnd
10098
10199class MusicExt (Extension ):
102100 ... # Some your cool music commands
103101
104102 # There are many useful events for you. You can use other events if you want it.
105- @extension_listener ()
106- async def on_track_start (self , event : lavalink.TrackStartEvent ):
103+ @listen ()
104+ async def on_track_start (self , event : TrackStart ):
107105 """ Fires when track starts"""
106+ print (f " Track { event.track.title} started " )
108107
109- @extension_listener ()
110- async def on_track_end (self , event : lavalink.TrackEndEvent ):
108+ @listen ()
109+ async def on_track_end (self , event : TrackEnd ):
111110 """ Fires when track ends"""
112111
113- @extension_listener ()
114- async def on_queue_end (self , event : lavalink.QueueEndEvent ):
112+ @listen ()
113+ async def on_queue_end (self , event : QueueEnd ):
115114 """ Fires when queue ends"""
116115
117116```
0 commit comments