Ситуация и проблема:
Я проигрываю песню и создаю список песен, которые будут воспроизводиться. После этого я использую >skip, попадаю в play_music, попадаю в self.vc.play и там запускаю после =, когда предыдущая песня закончилась из-за того, что я > пропустил.
(Надо отметить, что до этого я пришёл на self.vc.play Трек загрузил еще раз, следует минус одна песня)
Продолжая after=, я попадаю в play_next, где снова загружу песню и в результате потеряю 1 песню.
Отсюда и вопрос. , как написать код, чтобы песни могли нормально > пропускать?
И вот простой пример, который я получаю
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.client = Client(token_yandex_music).init()
# all the music related stuff
self.is_playing = False
self.is_paused = False
# 2d array containing [song, channel]
self.music_queue = []
self.vc = None
def play_next(self):
if len(self.music_queue) > 0:
self.vc.stop()
self.music_queue[0][0]['source'].download('song.mp3')
self.music_queue.pop(0)
self.vc.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: self.play_next())
self.is_playing = True
else:
self.is_playing = False
async def play_music(self, ctx):
if len(self.music_queue) > 0:
self.is_playing = True
self.music_queue[0][0]['source'].download('song.mp3')
# try to connect to voice channel if you are not already connected
if self.vc is None or not self.vc.is_connected():
self.vc = await self.music_queue[0][1].connect()
# in case we fail to connect
if self.vc is None:
await ctx.send("Вы не подключены к гс каналу")
return
else:
await self.vc.move_to(self.music_queue[0][1])
# remove the first element as you are currently playing it
self.music_queue.pop(0)
self.vc.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: self.play_next())
else:
self.is_playing = False
@commands.command(name="play", aliases=["p", "playing"], help="Plays a selected song from ym")
async def play(self, ctx, *args):
query = " ".join(args)
voice_channel = ctx.author.voice.channel
if voice_channel is None:
# you need to be connected so that the bot knows where to go
await ctx.send('Вы не подключены к гс каналу')
elif self.is_paused:
self.vc.resume()
else:
song = await self.search_yt(ctx, query)
if type(True) == type(song):
await ctx.send(
"Не удалось загрузить песню")
else:
await ctx.send("Песня добавлена в плейлист")
self.music_queue.append([song, voice_channel])
if not self.is_playing:
await self.play_music(ctx)
@commands.command(name="skip", aliases=["s"], help="Skips the current song being played")
async def skip(self, ctx):
if self.vc is not None and self.vc:
self.vc.stop()
# try to play next in the queue if it exists
await self.play_music(ctx)
Подробнее здесь: https://stackoverflow.com/questions/734 ... ks-at-once
Музыкальный бот Discord, когда я пропускаю трек, бот пропускает сразу 2 трека ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение