@@ -112,9 +112,7 @@ async def leave_inactive_voice_channel_task(self) -> None:
112112
113113 async def check_user_bot_same_channel (self , ia : discord .Interaction ) -> bool :
114114 """Return True if the user is in the same channel as the bot."""
115- vc : discord .VoiceProtocol = discord .utils .get (
116- self .bot .voice_clients , guild = ia .guild
117- )
115+ vc = discord .utils .get (self .bot .voice_clients , guild = ia .guild )
118116 if vc :
119117 channel : discord .VoiceChannel | discord .StageChannel = vc .channel
120118 try :
@@ -124,6 +122,20 @@ async def check_user_bot_same_channel(self, ia: discord.Interaction) -> bool:
124122 else :
125123 return True
126124
125+ async def play_track (
126+ self , player : wavelink .Player , track : wavelink .Playable
127+ ) -> None :
128+ """Play a track. Also handles seeking to a specific start time."""
129+ await player .play (track , end = dict (track .extras )["end" ])
130+ if dict (track .extras )["start" ] != 0 :
131+ # https://github.com/lavalink-devs/youtube-source/issues/97
132+ await player .pause (True )
133+ await asyncio .sleep (1 )
134+ await player .pause (False )
135+ await player .seek (dict (track .extras )["start" ])
136+
137+ logger .info (f"Playing track: { track .title } " )
138+
127139 # UTILITIES END
128140 ######################################
129141
@@ -146,7 +158,7 @@ async def on_wavelink_track_start(
146158 for member in payload .player .channel .members :
147159 if not member .bot :
148160 await payload .player .channel .send (
149- "Now playing * {title}* {looping}!" .format (
161+ "Now playing ` {title}` {looping}!" .format (
150162 title = payload .track .title ,
151163 looping = " (looping)"
152164 if payload .player .queue .mode != wavelink .QueueMode .normal
@@ -179,7 +191,7 @@ async def on_wavelink_track_end(
179191 return
180192
181193 await asyncio .sleep (1 )
182- await payload .player . play ( track )
194+ await self . play_track ( payload .player , track )
183195 else :
184196 logger .error (f"Music playing stopped. Reason: { payload .reason .upper ()} " )
185197 await payload .player .channel .send (
@@ -198,9 +210,17 @@ async def on_wavelink_track_end(
198210 @discord .app_commands .guild_only ()
199211 @discord .app_commands .describe (
200212 youtube_url = "URL of the Youtube video you want to play." ,
213+ start = "Start time of the song in seconds. Default: 0" ,
214+ end = "End time of the song in seconds. Default: None" ,
201215 )
202216 @check_node_exist
203- async def play (self , ia : discord .Interaction , youtube_url : str ) -> None :
217+ async def play (
218+ self ,
219+ ia : discord .Interaction ,
220+ youtube_url : str ,
221+ start : int = 0 ,
222+ end : None | int = None ,
223+ ) -> None :
204224 """Play a song with the given search query."""
205225
206226 # User must be in a voice channel
@@ -218,6 +238,10 @@ async def play(self, ia: discord.Interaction, youtube_url: str) -> None:
218238 )
219239 return
220240
241+ if end and start >= end :
242+ await ia .response .send_message ("Start time must be earlier than end time!" )
243+ return
244+
221245 # Delay response, maximum 15 mins
222246 await ia .response .defer ()
223247
@@ -245,8 +269,14 @@ async def play(self, ia: discord.Interaction, youtube_url: str) -> None:
245269 )
246270
247271 track = tracks [0 ]
248- # Add requester name to track, so that the `queue` command can show it
249- track .extras = {"requester" : ia .user .name }
272+ # Extra information of the track
273+ track .extras = {
274+ # Add requester name to track, so that the `queue` command can show it
275+ "requester" : ia .user .name ,
276+ # Add start and end time to track (used by the voice player)
277+ "start" : start * 1000 ,
278+ "end" : end * 1000 if end is not None else None ,
279+ }
250280
251281 await vc .queue .put_wait (track )
252282 await ia .followup .send (
@@ -258,7 +288,8 @@ async def play(self, ia: discord.Interaction, youtube_url: str) -> None:
258288 )
259289 )
260290 if not vc .playing :
261- await vc .play (vc .queue .get ())
291+ track = vc .queue .get ()
292+ await self .play_track (vc , track )
262293
263294 @discord .app_commands .command ()
264295 @discord .app_commands .guild_only ()
0 commit comments