【问题标题】:How to make discord bot play audio without using ctx?如何在不使用 ctx 的情况下让 discord bot 播放音频?
【发布时间】:2021-11-22 03:36:24
【问题描述】:
  async def playnext(self, message):
   if self.songQ != []:
      self.nuses = self.nuses + 1
      message.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(pafy.new(self.songQ[0]).getbestaudio().url)), after=lambda error: self.removeplay(message))
   else:
      await message.channel.send("Queue is empty")

我正在尝试使用消息创建 .playnext() 方法,但我不知道如何形成第 4 行的语法,因为根据我得到的错误,message.voice_client 没有方法 .play()

我见过人们使用discord.ctx 这样做: ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(pafy.new(self.songQ[0]).getbestaudio().url)), after=lambda error: self.removeplay(message))

但是我的整个机器人是建立在on_message() 函数上的,我不想在这个项目中使用不和谐的命令。

【问题讨论】:

  • 你能发布确切的错误信息吗?因为Discord.py的官方文档说VoiceClient有play方法。

标签: python python-3.x discord discord.py bots


【解决方案1】:

根据文档,ctx.voice_clientguild.voice_client 的快捷方式,因此您可以使用它。

你需要先定义你的公会:

guild = bot.get_guild(1234567890)
guild.voice_client.play(...)

https://discordpy.readthedocs.io/en/master/ext/commands/api.html?highlight=voice%20client#discord.ext.commands.Context.voice_client

https://discordpy.readthedocs.io/en/master/api.html?highlight=voice%20client#discord.Guild.voice_client

【讨论】:

  • 在 OPs 示例中,您可以通过guild = message.guild获得它
猜你喜欢
  • 2021-05-18
  • 2021-11-18
  • 2021-10-29
  • 2018-05-17
  • 1970-01-01
  • 2017-03-05
  • 2021-11-07
  • 2020-11-26
  • 2018-10-25
相关资源
最近更新 更多