首页 > 解决方案 > discord.py - 在音乐机器人上添加队列以解决不和谐和搜索问题

问题描述

有2个问题。

首先,我尝试为不和谐制作一个音乐机器人,它一直很好,直到我尝试从中播放另一首歌曲,播放另一首歌曲(可能是队列?)我不得不从语音通道中离开机器人并再次编写播放命令(这不是我想要的)并且还尝试从音乐标题播放,而不仅仅是从 url 播放(因为它会进入 youtube 并复制我想要播放的每首歌曲的链接)但它只有在我放undescore 之间的标题(例如“!play name_of_the_song”)我尝试了很多东西,但似乎没有一个有效,所以在这里问是我最后的火腿。

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

我的代码现在看起来像什么:

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')

感谢您的帮助注意:保持活动是我为保持活动方法制作的标题,因此机器人 24/7 在线,因此如果您愿意,请尝试忽略它,也对不起我的英语不好

标签: pythondiscorddiscord.pyqueuebots

解决方案


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

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。


推荐阅读