python-3.x - 如何让我的 discord.py 机器人在语音 channel 中播放 mp3?

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

我是 Python 的初学者,我最近开始为一些 friend 和我制作一个不和谐的机器人。我的想法是输入 !startq 并让机器人加入 channel ,播放本地存储在同一文件夹中的 mp3 文件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')

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

最佳答案

我对我的不和谐机器人做了类似的事情,这里有一些你可以引用的示例代码。如果您正在播放 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.')

关于python-3.x - 如何让我的 discord.py 机器人在语音 channel 中播放 mp3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53604339/

相关文章:

python-3.x - Python 3 - 将字典动态嵌套到给定未知数量类别的列表中

python-2.7 - 在安装了 Anaconda 的 Mac OSX 10.8 上让 Python 2 和 3 共存的最简单方法

php - 为 HLS 创建主播放列表

php - 不一致的视频转换

python - 如何向 discord.py 事件循环添加函数?

python - 平面嵌套字典,其中新键将是所有嵌套键的元组

Python C++ 扩展单例

ffmpeg - 如何使用具有特定搜索时间的 ffmpeg 从在线视频流中捕获多个屏幕截图

python - 如何加入服务器?

javascript - 记录来自 channel 的所有消息