【问题标题】:How do I make my discord.py bot play mp3 in voice channel?如何让我的 discord.py 机器人在语音频道中播放 mp3?
【发布时间】:2019-05-05 08:55:37
【问题描述】:

我是 Python 的初学者,最近开始为我和一些朋友制作一个不和谐的机器人。我的想法是输入 !startq 并让机器人加入频道,播放本地存储在bot.py 也在同一个文件夹中。

import discord, chalk
from discord.ext import commands
import time
import asyncio

bot = commands.Bot(command_prefix = "!")

@bot.event
async def on_ready():
    print("Bot is ready!")

@bot.command()
async def q5(ctx):
    await ctx.send("@here QUEUE STARTING IN 5 MINUTES")

@bot.command()
async def q3(ctx):
    await ctx.send("@here QUEUE STARTING IN 3 MINUTES")

@bot.command()
async def q1(ctx):
    await ctx.send("@here QUEUE STARTING IN 1 MINUTES")

@bot.command()
async def ping(ctx):
    ping_ = bot.latency
    ping =  round(ping_ * 1000)
    await ctx.send(f"my ping is {ping}ms")

@bot.command()
async def startq(ctx):
    voicechannel = discord.utils.get(ctx.guild.channels, name='queue')
    vc = await voicechannel.connect()
    vc.play(discord.FFmpegPCMAudio("countdown.mp3"), after=lambda e: print('done', e))
    bot.run('TOKEN')

到目前为止,我的机器人可以正常加入频道,但它实际上并没有播放 mp3。我在“Unofficial Discord API Discord”和其他一些编程 Discords 中问过无数人,但我还没有得到答案。

【问题讨论】:

  • bot.run 应该在您的功能之外。请参阅the documentation中的示例
  • 是的,我只是将它添加到此线程错误。对不起!
  • 你安装了FFmpeg吗?
  • 是的,我确实 pip install ffmpeg 并且它安装正确。虽然仍然无法让它工作
  • 需要单独下载FFmpeg 可以在这里找到:ffmpeg.org。下载后,请确保将exe添加到您的路径环境变量中,否则将无法正常工作。见这里:discordpy.readthedocs.io/en/rewrite/…

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


【解决方案1】:

我对我的不和谐机器人做了类似的事情,这里有一些示例代码你可以参考。如果您正在播放 mp3 文件,请确保安装 ffmpeg,我在设置机器人时按照此处的说明进行操作 https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg

@client.command(
    name='vuvuzela',
    description='Plays an awful vuvuzela in the voice channel',
    pass_context=True,
)
async def vuvuzela(context):
    # grab the user who sent the command
    user=context.message.author
    voice_channel=user.voice.voice_channel
    channel=None
    # only play music if user is in a voice channel
    if voice_channel!= None:
        # grab user's voice channel
        channel=voice_channel.name
        await client.say('User is in channel: '+ channel)
        # create StreamPlayer
        vc= await client.join_voice_channel(voice_channel)
        player = vc.create_ffmpeg_player('vuvuzela.mp3', after=lambda: print('done'))
        player.start()
        while not player.is_done():
            await asyncio.sleep(1)
        # disconnect after the player has finished
        player.stop()
        await vc.disconnect()
    else:
        await client.say('User is not in a channel.')

【讨论】:

    【解决方案2】:

    我会这样做:

    voice.play(discord.FFmpegPCMAudio(executable="C:/path/ffmpeg.exe", source="C:/songpath"))
    
    

    【讨论】:

      【解决方案3】:

      这就是我用于我的bot 播放 mp3 文件的重写版本的方法。您还需要加载作品,这很容易并且还具有 FFMPEG。

      OPUS_LIBS = ['libopus-0.x86.dll', 'libopus-0.x64.dll', 'libopus-0.dll', 'libopus.so.0', 'libopus.0.dylib']
      
      
      def load_opus_lib(opus_libs=OPUS_LIBS):
          if opus.is_loaded():
              return True
      
          for opus_lib in opus_libs:
              try:
                  opus.load_opus(opus_lib)
                  return
              except OSError:
                  pass
      
              raise RuntimeError('Could not load an opus lib. Tried %s' % (', '.join(opus_libs)))
      
      @bot.command(aliases=['paly', 'queue', 'que'])
      async def play(ctx):
          guild = ctx.guild
          voice_client: discord.VoiceClient = discord.utils.get(bot.voice_clients, guild=guild)
          audio_source = discord.FFmpegPCMAudio('vuvuzela.mp3')
          if not voice_client.is_playing():
              voice_client.play(audio_source, after=None)
      

      【讨论】:

        猜你喜欢
        • 2022-01-24
        • 2020-08-30
        • 2021-08-16
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 2020-10-04
        • 2018-06-16
        • 2021-09-03
        相关资源
        最近更新 更多