首页 > 解决方案 > 如何解决和实现当前不工作的不和谐音乐机器人的正确队列功能?

问题描述

我不确定为什么在音乐机器人上输入时队列功能不起作用/不显示。其余的功能,如暂停、恢复、播放、加入等,但队列不起作用,我想知道它是否可以链接到用于存储音乐 url 的字典“检查队列”是问题?这个问题有解决方法吗?谢谢!

Main.py 此函数的用途:与不和谐令牌文件链接并执行 python 音乐机器人

import os
import discord
import logging
from dotenv import load_dotenv
from discord.ext import commands

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

    # Greetings
    @commands.Cog.listener()
    async def on_ready(self):
        print(f'Logged in as {self.bot.user} ({self.bot.user.id})')

    # Reconnect
    @commands.Cog.listener()
    async def on_resumed(self):
        print('Bot has reconnected!')

    # Error Handlers
    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        # Uncomment line 26 for printing debug
        # await ctx.send(error)

        # Unknown command
        if isinstance(error, commands.CommandNotFound):
            await ctx.send('Invalid Command!')

        # Bot does not have permission
        elif isinstance(error, commands.MissingPermissions):
            await ctx.send('Bot Permission Missing!')


# Gateway intents
intents = discord.Intents.default()
intents.members = True
intents.presences = True

# Bot prefix
bot = commands.Bot(command_prefix=commands.when_mentioned_or('/'),
                   description='', intents=intents)



# Logging
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

# Loading data from .env file
load_dotenv()
token = os.getenv('TOKEN')

if __name__ == '__main__':
    # Load extension
    for filename in os.listdir('./commands'):
        if filename.endswith('.py'):
            bot.load_extension(f'commands.{filename[: -3]}')

        bot.add_cog(TestBot(bot))
        bot.run(token, reconnect=True)

Music py 主要目的:包含运行音乐机器人的所有功能主要发现的问题:可能在“check_queue”和“queue”功能下重现“错误”的步骤,使用“/play”命令将 2 首歌曲添加到机器人中,但是当谈到下一个 2nd song 时,什么都没有播放,并且 /queue 不起作用/在 discord 上显示任何输出。

import asyncio
import youtube_dl
import pafy
import discord
from discord.ext import commands

class PlayMusic(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.song_queue = {}

        self.setup()

    def setup(self):
        for guild in self.bot.guilds:
            self.song_queue[guild.id] = []

    async def check_queue(self, ctx):
        if len(self.song_queue[ctx.guild.id]) > 0:
            ctx.voice_client.stop()
            await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
            self.song_queue[ctx.guild.id].pop(0)

    @commands.command()
    async def queue(self, ctx):  # display the current guilds queue
        if len(self.song_queue[ctx.guild.id]) == 0:
            return await ctx.send("There are currently no songs in the queue.")

        embed = discord.Embed(title="Song Queue", description="", colour=discord.Colour.dark_gold())
        i = 1
        for url in self.song_queue[ctx.guild.id]:
            embed.description += f"{i}) {url}\n"

            i += 1

        embed.set_footer(text="Thanks for using me!")
        await ctx.send(embed=embed)

   
def setup(bot):
    bot.add_cog(PlayMusic(bot))

标签: pythonpandasdiscord

解决方案


推荐阅读