如何检查不和谐机器人是否在频道中?

时间:2021-05-03 23:35:52

标签: python pip discord

我有点制作了这个工作音乐机器人代码,它加入 vc 并播放歌曲。但是当它在声音中时它不会播放另一个,我需要断开他的连接。即使他加入,我也希望他播放下一首歌。怎么做?

代码(我想要的部分):

@bot.command()
async def hraj(ctx, url):
    if not ctx.message.author.voice:
        await ctx.send("Musíš být ve voicu ty pepego!")
        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=bot.loop)
        voice_channel.play(player, after=lambda e: print("Error. %s" %e if e else None))

    e = discord.Embed(title="Právě hraje:", description=f"[{player.title}]({url})", color=0x0091ff)
    e.set_footer(text="Lets go jdem vibovat <333")
    await ctx.send(embed=e)

1 个答案:

答案 0 :(得分:0)

你可以使用

voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice == None:
   channel = ctx.message.author.voice.channel

这是有效的,因为如果您的机器人不在语音频道中,discord.utils.get(client.voice_clients, guild=ctx.guild) 会返回 None

所以你的代码是:

@bot.command()
async def hraj(ctx, url):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice == None:
       channel = ctx.message.author.voice.channel
    else:
        await ctx.send('Musíš být ve voicu' ty pepego)

    await channel.connect()

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

    async with ctx.typing():
        player = await YTDLSource.from_url(url, loop=bot.loop)
        voice_channel.play(player, after=lambda e: print("Error. %s" %e if e else None))

    e = discord.Embed(title="Právě hraje:", description=f"[{player.title}]({url})", color=0x0091ff)
    e.set_footer(text="Lets go jdem vibovat <333")
    await ctx.send(embed=e)

顺便说一句,你的问题不是很清楚。