如何使不和谐的音乐机器人识别出一首歌曲,以便它可以播放下一首歌曲?

时间:2020-07-28 03:34:57

标签: python discord bots discord.py-rewrite

尝试播放队列中的下一首歌曲时,我的Discord机器人遇到了麻烦。这个问题很难描述,因为在我的代码中进行很小的调整就可以得到很多不同的错误。

首先,当我自己不知道如何解决该问题时,我检查了StackOverflow中是否存在类似的问题,并且低估了问题,我的问题还有另一个用户。但是,解决方案中显示的针对该用户的修复不适用于我。

我已经尝试过对我可以想到的所有可能的排列进行许多调整,但是,a,我似乎无法弄清楚如何使其工作。在我的“播放”命令中一切都出错了。这是我最后留下的代码。

@commands.command(aliases=["p"])
async def play(self, ctx):
    await self.client.get_command("join").callback(self, ctx) # Joins the channel the user is in
    voice = get(self.client.voice_clients, guild=ctx.guild)

    async def play_next(self, ctx):
        global CURRENT_SONG
        for file in os.listdir("./songs"):
            print(file)
            print("CURRENT SONG = {0}".format(CURRENT_SONG))
            if file.endswith(".mp3"):
                file_prefix = int(file[:3])

                if CURRENT_SONG < file_prefix:
                    print("Current song has been changed")
                    CURRENT_SONG = file_prefix
                
                if file_prefix == CURRENT_SONG and not voice.is_playing():
                    await voice.play(discord.FFmpegPCMAudio(SONGS_FOLDER + file), after= await play_next(self, ctx))
                    voice.source = discord.PCMVolumeTransformer(voice.source)
                    voice.source.volume = 0.25
    
    await play_next(self, ctx)

我遇到三个稍有不同的问题,具体取决于我对代码进行的调整。

#1-完全没有歌曲播放,处于无限循环状态 enter image description here

这似乎最常发生。根据我的判断,after=的{​​{1}}部分似乎会不断重复播放,即使这是在歌曲播放完之后进行的。

#2-歌曲#1播放,但之后没有播放

当我将voice.play更改为after= await play_next(self, ctx)并删除play_next函数,只是将for循环保留在play函数中时,就会发生这种情况。这告诉我必须添加此部分,但是我不确定如何添加。有趣的是,控制台会说下一首歌会播放,但永远不会播放。

我很早以前就遇到过这个问题,但是我似乎无法以某种方式重现此问题,因此无法提供终端的屏幕截图。

#3-控制台显示所有歌曲将播放,全部跳过,播放最后一首歌曲

这种情况很少发生,但令人讨厌。我在代码触发器中有打印件,但是歌曲不播放。他们只是跳过,直到到达最后一个,这时的确确实起作用了。不幸的是,由于这种情况很少发生,因此我也没有此特定问题的屏幕截图。本质上,它会打印:

001-{songname}已结束播放

002-{songname}已经播放完毕

003-{songname}的比赛结束了

......

n-{songname}完成播放

但是,似乎只播放了最后一个。

很长时间以来,我一直不遗余力,非常感谢您的帮助。这个问题一直困扰着我近一个星期。

预先感谢

这是我上次离开时​​的完整代码\ /

bot.py

after=print("{0} has finished playing".format(file)))

basic_functions.py

import discord
from discord.ext import commands
import os
from settings import BOT_TOKEN

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

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.idle)
    print("This bot is ready!")

@client.command()
async def load(ctx, extension):
    client.load_extension("cogs.{0}".format(extension))

@client.command()
async def unload(ctx, extension):
    client.unload_extension("cogs.{0}".format(extension))

@client.command()
async def reload(ctx, extension):
    await client.get_command("unload").callback(ctx, extension.lower())
    await client.get_command("load").callback(ctx, extension.lower())

@client.command()
async def reload_all_cogs(ctx):
    for filename in os.listdir("./cogs"):
        if filename.endswith(".py"):
            await client.get_command("reload").callback(ctx, filename[:-3])

# Load Cogs on Boot
for filename in os.listdir("./cogs"):
    if filename.endswith(".py"):
        client.load_extension("cogs.{0}".format(filename[:-3]))


client.run(BOT_TOKEN)

music_bot.py

import discord
from discord.ext import commands

class BasicFunctions(commands.Cog):
    def __init__(self, client):
        self.client = client
    
    @commands.command()
    async def die(self, ctx):
        await ctx.bot.logout()
    
    @commands.command()
    async def clear(self, ctx, amount=10):
        await ctx.channel.purge(limit=amount)

    @clear.error
    async def clear_error(self, ctx, error):
        if isinstance(error, commands.BadArgument):
            await ctx.send("Sorry, please enter an integer amount of lines to clear between 1 and 100")
        else:
            await ctx.send("There was an error, please try again!")

    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        if isinstance(error, commands.CommandNotFound):
            await ctx.send("Sorry, there are no commands by that name")


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

此外,这是我的文件夹层次结构的屏幕截图,以防万一您需要它: folder hierarchy

如果您希望自己重新创建此漫游器,则以下是点列表: pip list

0 个答案:

没有答案
相关问题