discord.py - 在音乐机器人上添加 queue 以解决不和谐和搜索问题

discord.py - Adding a queue on a music bot for discord and search problems

有 2 个问题。

首先,我尝试为 discord 制作一个音乐机器人,它运行良好,直到我尝试从中播放另一首歌曲,播放另一首歌曲(queue 也许?)我不得不离开机器人语音通道并再次编写播放命令(这不是我想要的)并且还尝试从音乐标题播放而不是仅从 url 播放(因为转到 youtube 并复制 link 对于我想播放的每首歌)但只有当我把标题放在下划线之间时它才有效(比如“!播放 name_of_the_song”)我尝试了很多东西但是 none 似乎有效, 所以在这里问是我最后的火腿。

所以我想要的是通过播放命令一首又一首地播放机器人,并且我可以从它的标题中搜索一首歌,而不给我另一首与我正在寻找的东西无关的歌曲

我的代码现在是什么样的:

import discord
from discord.ext import commands, tasks
import youtube_dl
import asyncio
from keep_alive import keep_alive
from random import choice
import os, shutil


folder = 'music_files'

youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
    'format': 'bestaudio/best',
    'outtmpl': 'music_files/%(id)s.mp3',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}

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):
        loop = loop or asyncio.get_event_loop()
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))

        if 'entries' in data:
            # take first item from a playlist
            data = data['entries'][0]

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


client = commands.Bot(command_prefix='!')

status = ['Jamming out to music!', 'Eating!', 'Sleeping!']

@client.event
async def on_ready():
    change_status.start()
    print('Bot is online!')

@client.event
async def on_member_join(member):
    channel = discord.utils.get(member.guild.channels, name='general')
    await channel.send(f'Welcome {member.mention}!  Ready to jam out? See `?help` command for details!')

@client.command(name='ping', help='This command returns the latency')
async def ping(ctx):
    await ctx.send(f'**Pong!** Latency: {round(client.latency * 1000)}ms')

@client.command(name='hello', help='This command returns a random welcome message')
async def hello(ctx):
    responses = ['***grumble*** Why did you wake me up?', 'Top of the morning to you lad!', 'Hello, how are you?', 'Hi', '**Wasssuup!**']
    await ctx.send(choice(responses))

@client.command(name='die', help='This command returns a random last words')
async def die(ctx):
    responses = ['why have you brought my short life to an end', 'i could have done so much more', 'i have a family, kill them instead']
    await ctx.send(choice(responses))

@client.command(name='credits', help='This command returns the credits')
async def credits(ctx):
    await ctx.send('Made by Danxx')
    await ctx.send('Thanks to `RK Coding` for the base code')

@client.command(name='play', help='This command plays music')
async def play(ctx, url):
    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 channel.connect()

    server = ctx.message.guild
    voice_channel = server.voice_client

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

    await ctx.send('**Now playing:** {}'.format(player.title))

@client.command()
async def pause(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice.is_playing():
        voice.pause()
    else:
        await ctx.send("Currently no audio is playing.")

@client.command()
async def resume(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice.is_paused():
        voice.resume()
    else:
        await ctx.send("The audio is not paused.")

@client.command(name='stop', help='This command stops the music and makes the bot leave the voice channel')
async def stop(ctx):
    voice_client = ctx.message.guild.voice_client
    await voice_client.stop()

@client.command()
async def leave(ctx):
    for song in os.listdir(folder):
                file_path = os.path.join(folder, song)
                try:
                    if os.path.isfile(file_path) or os.path.islink(file_path):
                        os.unlink(file_path)
                    elif os.path.isdir(file_path):
                        shutil.rmtree(file_path)
                except Exception as e:
                    print('Failed to delete %s. Reason %s' % (file_path, e))
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice.is_connected():
        await voice.disconnect()
    else:
        await ctx.send("The bot is not connected to a voice channel.")

@tasks.loop(seconds=20)
async def change_status():
    await client.change_presence(activity=discord.Game(choice(status)))


keep_alive()
client.run('TOKEN')

感谢您的帮助 注意:keep alive 是我制作的一个 header keep alive 方法,所以机器人 24/7 在线所以如果你想忽略它,也很抱歉我的英语不好

可以为公会排队播放音乐。使用内置模块队列,或仅使用列表。像这样的东西会起作用:

queue = {}
def addToQueue(guild, song):
    if guild.id in queue:
        queue[guild.id] = []
    queue[guild.id].append(song)

async def playSong(ctx, channel):
    async with ctx.typing():
        song = queue[channel.guild.id].pop(0, None)
        if song == None:
            return
        player = await YTDLSource.from_url(song, loop=client.loop, stream=True)
        channel.play(
            player,
            after=lambda e:
            print('Player error: %s' % e) if e else await playSong(ctx, channel)
        )

    await ctx.send('**Now playing:** {}'.format(player.title))

@client.command(name='play', help='This command plays music')
async def play(ctx, url):
    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
    
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if channel != voice and voice is None:
        await channel.connect()
    elif channel != voice and voice is not None:
        await ctx.send("Already connected to a channel")
        return

    server = ctx.message.guild
    voice_channel = server.voice_client
    addToQueue(ctx.message.guild, url)
    await playSong(ctx, voice_channel)

对于您的搜索问题,您提供的代码似乎无法解决该问题。您将不得不使用 Youtube API。