【问题标题】:Discord.py command to play audio in a VC and command to leave VC using interactions/slash commands. NOT ctx or 'discord.ext commands'Discord.py 命令在 VC 中播放音频,并使用交互/斜杠命令离开 VC。不是 ctx 或 \'discord.ext 命令\'
【发布时间】:2023-02-05 01:50:58
【问题描述】:

我想让我自己的个人/私人机器人加入我所在的语音频道并播放音频文件。我可以加入 VC,但我不知道如何使用斜线命令/交互让机器人离开或播放音乐/音频。我到处都看到它只是陈旧和过时的例子。即使是 discord.py github 示例也无济于事,它依赖于使用 ctx 和 discord.ext 命令。 (与我在 stackoverflow 上可以找到的内容相同)。

它本应如此简单,但却被垃圾示例和过时材料所混淆。与我想做的事无关。不是 cogs/classes,不是 ctx.,与“self”无关。只是交互/斜杠命令。 “应用程序命令”

我也不需要知道如何使用斜杠命令或如何使用它们。我想我已经记下了。

我正在尝试使用 interaction.voice_client.play() 播放音频,但我只是收到以下错误。 AttributeError: 'Interaction' object has no attribute 'voice_client' 我花了一整天的时间试图理解 discord.py 文档,却无法找到使用斜杠命令/交互的最新示例。我什至不确定要寻找什么,甚至不知道该去哪里寻找那堆乱七八糟的文档。搜索错误对搜索结果完全不同的错误等没有任何帮助。

这是我用于 play 命令的一些代码..我没有用于 leave 命令的代码: (来自 discord.py github 示例目录中的示例,但稍作编辑以尝试允许我使用斜杠命令,而不是仅在聊天中发送“!播放”。)

@muise.tree.command()
@app_commands.describe(url='Youtube URL')
async def play(interaction: discord.Interaction, url: str):
    """Streams audio from a url"""

    player = await YTDLSource.from_url(url, loop=muise.loop, stream=True) 
    #no idea if muise.loop will even work. used to be "self.bot.loop" But I am not in a cog or class.
    interaction.voice_client.play(player, after=lambda e: print(f'Player error: {e}') if e else None)

    embed = discord.Embed(title='Muise', colour=main_embed_color, timestamp=datetime.datetime.now(datetime.timezone.utc))
    embed.add_field(name='Now Playing', value=f'{player.title}')
    embed.set_footer(text=Config["author"], icon_url='https://cdn.discordapp.com/attachments/1019374213037035530/1040294855315836998/Ori_the_cutie-1.png')
    await interaction.response.send_message(embed=embed)

预期的结果应该是在 VC 中播放音频。

我的主要问题是没有正确的知识如何使这项工作..idk做什么,在哪里看。我太累了,想结束这个简单的任务。任何帮助将不胜感激。

欢迎提问,我会尽我所能回答。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    尝试做

    guild = interaction.guild
    guild.voice_client.play
    

    代替

    interaction.voice_client.play
    

    【讨论】:

      【解决方案2】:

      你应该使用

      voices = interaction.client.voice_clients
      for i in voices:
          if i.channel == interaction.user.voice.channel:
              voice = i
              break
      else:
          if interaction.user.voice is not None:
              vc = interaction.user.voice.channel
              voice = await vc.connect()
          else:
              await interaction.response.defer(ephemeral=True)
              await interaction.followup.send("You are not in a voice channel.")
              return
      

      找到语音客户端

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-31
        • 2022-11-30
        • 2021-10-19
        • 1970-01-01
        • 2021-07-02
        • 2021-06-06
        • 2023-02-26
        • 1970-01-01
        相关资源
        最近更新 更多