没有来自不和谐机器人的音频。不和谐的.py/ffmpeg

时间:2021-07-22 18:42:02

标签: python ffmpeg discord.py

我正在创建一个简单的音乐机器人,但我不知道为什么机器人没有音频。我试过踢和重新加入,更改代码等等。我就是想不通。命令行没有错误,机器人周围有一个绿色的小圆圈,好像有音频一样。但是,如果您重新加入 VC,它就会消失。

代码如下:

import discord
from discord.utils import get
from discord.ext import commands
from discord import FFmpegPCMAudio
from discord.voice_client import VoiceClient

import youtube_dl
import os

TOKEN = "Token Here"
bot = commands.Bot(command_prefix='s')
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=',', intents = intents)

@bot.event
async def on_ready():
    channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
    await channel.connect()
    
    
@bot.command(name='play')
async def play(ctx, url):
    await message.content.startswith('play')
    player = await voice_client.create_ytdl_player(url)
    player = await voice.create_ytdl_player("https://youtu.be/K4DyBUG242c")
    player = await vc.create_ytdl_player()
    player.start()

bot.run(TOKEN) 

如果您愿意,请随时改进它。我从 YouTube 教程中得到了这段代码,所以我没有完全理解音频部分。

1 个答案:

答案 0 :(得分:0)

试试这个。当您运行该命令时,消息永远不会以 play 开头,bot 前缀为 ","

此外,您将 intents 添加到 client。您应该将其设置为 bot 并删除 client,您永远不会在代码中使用它。

import discord
from discord.utils import get
from discord.ext import commands
from discord import FFmpegPCMAudio
from discord.voice_client import VoiceClient

import youtube_dl
import os

TOKEN = "Token Here"

intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=',', intents = intents)

@bot.event
async def on_ready():
    channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
    await channel.connect()
    
    
@bot.command(name='play')
async def play(ctx, url):
    # await message.content.startswith('play')
    player = await voice_client.create_ytdl_player(url)
    player = await voice.create_ytdl_player("https://youtu.be/K4DyBUG242c")
    player = await vc.create_ytdl_player()
    player.start()

bot.run(TOKEN)