【问题标题】:Trying to add a queue to a Discord Music Bot尝试将队列添加到 Discord 音乐机器人
【发布时间】:2021-11-11 23:07:47
【问题描述】:

Rhythm 和 Groovy discord 机器人被取消后,我决定做一些研究,为我们的 discord 服务器制作我自己的音乐机器人。我从 Youtuber 教程中找到了一些代码(老实说,我忘记了名字,我感觉很糟糕),它能够加入、播放、暂停、恢复和断开连接。但是,我想给它添加一个队列,这样我和我的朋友就可以在玩一些游戏的同时排队一些歌曲。这就是我目前所拥有的

  • main.py
    import discord
from discord.ext import commands
import music

cogs = [music]

client = commands.Bot(command_prefix='!', intents = discord.Intents.all())

for i in range(len(cogs)):
    cogs[i].setup(client)


client.run('TOKEN')
  • 音乐.py
import discord
from discord.ext import commands
import youtube_dl

class music(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def join(self,ctx):
        if ctx.author.voice is None:
          await ctx.send("Get in a voice channel idiot!")
        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)

    @commands.command()
    async def disconnect(self,ctx):
        await ctx.voice_client.disconnect()

    @commands.command()
    async def play(self,ctx,url):
        ctx.voice_client.stop()
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
        YDL_OPTIONS = {'format':"bestaudio"}
        vc = ctx.voice_client
        self.music_queue = []

        with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(url,download=False)
            url2 = info['formats'][0]['url']
            source = await discord.FFmpegOpusAudio.from_probe(url2,**FFMPEG_OPTIONS)
            vc.play(source)

    @commands.command()
    async def pause(self, ctx):
        await ctx.voice_client.pause()
        await ctx.send("Paused ⏸️")

    @commands.command()
    async def resume(self, ctx):
        await ctx.voice_client.resume()
        await ctx.send("Resumed ⏯️ ")

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

我希望得到一个工作队列,如果可能的话,使用跳过和清除命令。谢谢:)

【问题讨论】:

  • 您查看过queue 模块吗?
  • 你不应该真的在异步上下文中使用队列模块,考虑使用asyncio.queue
  • @WasiMaster 你知道我将如何实现它吗?就像我说的,我对代码还不是很感兴趣。而且我不确定我应该在该示例中更改什么以使其与我的链接和工作良好
  • @JordanAmo 这是 Mysty 在 discord.py 官方服务器上写的 example。不要只是明目张胆地复制粘贴,看看并尝试理解。然后自己做

标签: python discord.py


【解决方案1】:

这是我的队列命令,只是摆脱了黑名单,它应该可以工作

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()
    @commands.cooldown(1, 5, commands.BucketType.user)
    async def queue(self, ctx):
        if await get_mute(ctx.author) != 0:
            await ctx.send("You are blacklisted from the bot")
        else: # display the current guilds queue
            commandd = "queue"
            print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
            print(x)
            print(" ")
            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.Color.green().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)

来自愤怒机器人的首席开发人员

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 2021-02-21
    • 2021-12-19
    • 1970-01-01
    • 2021-05-27
    • 2019-04-30
    • 2018-07-24
    • 2022-12-20
    • 2021-02-07
    相关资源
    最近更新 更多