【问题标题】:How to check if discord bot is in channel?如何检查不和谐机器人是否在频道中?
【发布时间】:2021-05-04 07:53:41
【问题描述】:

我制作了这个可以工作的音乐机器人代码,它加入了 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)

【问题讨论】:

    标签: python pip discord


    【解决方案1】:

    你可以使用

    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)
    

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

    【讨论】:

    • 就像我只想制作一些可以工作的音乐机器人:D。现在它可以工作了
    猜你喜欢
    • 2019-11-05
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2020-12-28
    • 2019-12-08
    • 2020-02-19
    • 1970-01-01
    相关资源
    最近更新 更多