【问题标题】:Discord py - music bot queue commandDiscord py - 音乐机器人队列命令
【发布时间】:2021-02-21 04:42:54
【问题描述】:

大家好,

我有两个小问题不知道怎么解决。

我想做什么?

我想创建一个具有一些功能的非常简单的音乐机器人。

  1. 从 URL 播放音乐
  2. 播放标题中的音乐
  3. 播放队列中的音乐

现在我的问题:

每次使用

?播放标题或网址

它会跳过当前播放的歌曲 - 我希望机器人等到歌曲播放完毕

我的第二个问题几乎相同 - 我希望机器人播放队列并等到每首歌曲完成 - 现在他循环播放歌曲列表并只播放第一首:(

我的代码是什么样的:

import discord
import asyncio
import os
import youtube_dl

import urllib.parse, urllib.request, re
import requests

from discord.ext import commands
from discord import Embed, FFmpegPCMAudio
from discord.utils import get

'''

INSTALLING YOUTUBE-DL

pip install -U git+https://github.com/l1ving/youtube-dl

'''

queue = []

youtube_dl.utils.bug_reports_message = lambda: ''

ytdl_format_options = {
    'format': 'bestaudio/best',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0'
}

ffmpeg_options = {
    'options': '-vn'
}

ytdl = youtube_dl.YoutubeDL(ytdl_format_options)

class YTDLSource(discord.PCMVolumeTransformer):
    def __init__(self, source, *, data, volume=0.5):
        super().__init__(source, volume)

        self.data = data

        self.title = data.get('title')
        self.url = data.get('url')

    @classmethod
    async def from_url(cls, url, *, loop=None, stream=False, play=False):
        loop = loop or asyncio.get_event_loop()
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream or play))

        if 'entries' in data:
            data = data['entries'][0]

        filename = data['url'] if stream else ytdl.prepare_filename(data)
        return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)


class Music(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def join(self, ctx):

        if not ctx.message.author.voice:
            await ctx.send("You are not connected to a voice channel!")
            return
        else:
            channel = ctx.message.author.voice.channel
            await ctx.send(f'Connected to ``{channel}``')

        await channel.connect()

    @commands.command()
    async def play(self, ctx, *, url):

        try:

            async with ctx.typing():
                player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
                ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

            await ctx.send(f':mag_right: **Searching for** ``' + url + '``\n<:youtube:763374159567781890> **Now Playing:** ``{}'.format(player.title) + "``")

        except:

            await ctx.send("Somenthing went wrong - please try again later!")

    @commands.command()
    async def play_queue(self, ctx):

        for url in queue:

            try:

                async with ctx.typing():
                    player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
                    ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

                await ctx.send(f'<:youtube:763374159567781890> **Now Playing:** ``{url}``')

            except:

                await ctx.send("Somenthing went wrong - please try again later!")
            
        else:

            await ctx.send("Queue is now done!")

    @commands.command()
    async def pause(self, ctx):
        voice = get(self.bot.voice_clients, guild=ctx.guild)

        voice.pause()

        user = ctx.message.author.mention
        await ctx.send(f"Bot was paused by {user}")

    @commands.command()
    async def resume(self, ctx):
        voice = get(self.bot.voice_clients, guild=ctx.guild)

        voice.resume()

        user = ctx.message.author.mention
        await ctx.send(f"Bot was resumed by {user}")

    @commands.command()
    async def add_queue(self, ctx, url):

        global queue

        try:
            queue.append(url)
            user = ctx.message.author.mention
            await ctx.send(f'``{url}`` was added to the queue by {user}!')
        except:
            await ctx.send(f"Couldnt add {url} to the queue!")

    @commands.command()
    async def remove_queue(self, ctx, number):

        global queue

        try:
            del(queue[int(number)])
            if len(queue) < 1:
                await ctx.send("Your queue is empty now!")
            else:
                await ctx.send(f'Your queue is now {queue}')
        except:
            await ctx.send("List index out of range - the queue starts at 0")

    @commands.command()
    async def clear_queue(self, ctx):

        global queue

        queue.clear()
        user = ctx.message.author.mention
        await ctx.send(f"The queue was cleared by {user}")

    @commands.command()
    async def view_queue(self, ctx):

        if len(queue) < 1:
            await ctx.send("The queue is empty - nothing to see here!")
        else:
            await ctx.send(f'Your queue is now {queue}')

    @commands.command()
    async def leave(self, ctx):
        voice_client = ctx.message.guild.voice_client
        user = ctx.message.author.mention
        await voice_client.disconnect()
        await ctx.send(f'Disconnected from {user}')

    @play_queue.before_invoke
    @play.before_invoke
    async def ensure_voice(self, ctx):
        if ctx.voice_client is None:
            if ctx.author.voice:
                await ctx.author.voice.channel.connect()
            else:
                await ctx.send("You are not connected to a voice channel.")
                raise commands.CommandError("Author not connected to a voice channel.")
        elif ctx.voice_client.is_playing():
            ctx.voice_client.stop()

def setup(client):
    client.add_cog(Music(client))

感谢您的帮助

【问题讨论】:

  • 我只想说你真的很好地描述了你的问题,一切都井井有条,干得好!

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

所以我认为您需要做的是将playplay_queue 函数完全拆分为playstart_playing 函数。

我的意思是当前你的播放命令找到一首歌曲并播放它,而它应该做的是找到一首歌曲并将其附加到队列中,所以这个过程应该是这样的:

  1. 有人输入play 'song'
  2. 机器人检查是否没有歌曲正在播放if len(self.queue) == 0: start_playing(song),如果是,它会调用一个开始播放歌曲的函数,如果有歌曲正在播放,它会将其添加到队列中,我建议这样做的方式是给每首歌一个id,self.queue[len(self.queue)] = song
  3. 然后,如果有人再次使用 play 'song' 命令,则歌曲将添加到队列中,并在第一首歌曲播放完毕后播放

基本上我建议删除play_queue 命令,如果没有歌曲正在播放,则将play 转换为播放,否则添加到队列命令,以及在没有歌曲播放时调用的函数

self.queue 的实现可能如下所示:

@commands.command()
async def join(self, ctx):

    if not ctx.message.author.voice:
        await ctx.send("You are not connected to a voice channel!")
        return
    else:
        channel = ctx.message.author.voice.channel
        self.queue = {}
        await ctx.send(f'Connected to ``{channel}``')

    await channel.connect()

@commands.command()
async def play(self, ctx, *, url):

    try:

        async with ctx.typing():
            player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)

            if len(self.queue) == 0:

                self.start_playing(ctx.voice_client, player)
                await ctx.send(f':mag_right: **Searching for** ``' + url + '``\n<:youtube:763374159567781890> **Now Playing:** ``{}'.format(player.title) + "``")

            else:
                
                self.queue[len(self.queue)] = player
                await ctx.send(f':mag_right: **Searching for** ``' + url + '``\n<:youtube:763374159567781890> **Added to queue:** ``{}'.format(player.title) + "``")

    except:

        await ctx.send("Somenthing went wrong - please try again later!")

def start_playing(self, voice_client, player):

    self.queue[0] = player

    i = 0
    while i <  len(self.queue):
        try:
            voice_client.play(self.queue[i], after=lambda e: print('Player error: %s' % e) if e else None)

        except:
            pass
        i += 1

这应该可以解决播放和播放队列问题

【讨论】:

  • 感谢代码,但现在机器人不能再播放歌曲了。他立即进入“除了:await ctx.send("出了点问题-请稍后再试!")”:/-但我没有收到任何错误?
  • 去掉try和except语句看看错误
  • 我没有收到任何错误 - 它只是不播放歌曲?
  • 我将self.queue = queue 添加到班级顶部。还是我忘记了什么?
  • 我不能肯定。你改变了什么?
猜你喜欢
  • 1970-01-01
  • 2021-05-24
  • 2021-02-07
  • 1970-01-01
  • 2022-12-03
  • 2022-01-21
  • 2021-12-19
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多