【问题标题】:No audio from discord bot. discord.py/ffmpeg没有来自不和谐机器人的音频。不和谐.py/ffmpeg
【发布时间】:2021-09-30 00:58:50
【问题描述】:

我正在创建一个简单的音乐机器人,但我无法弄清楚为什么机器人没有音频。我试过踢和重新加入,更改代码等等。我就是想不通。命令行没有错误,机器人周围有一个绿色的小圆圈,就好像它有音频一样。但是,如果您重新加入 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 教程中获得了这段代码,所以我不完全理解音频部分。

【问题讨论】:

  • await message.content.startswith('play') 这一行是什么意思?首先你没有定义message,其次你为什么要等它?
  • 您使用的是过时的方法。如果你看教程,请看最近的,因为discord.py 经常更新。例如,您可以查看this answer

标签: python ffmpeg discord.py


【解决方案1】:

试试这个。运行命令时,消息永远不会以play 开头,机器人前缀是","

另外,您将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)

【讨论】:

  • 它只是给了我这个大错误:文件“C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py”,第 542 行,在转换 raise MissingRequiredArgument(param) discord.ext.commands.errors.MissingRequiredArgument: url 是缺少的必需参数。 @FoxGames01
  • 真的不知道。如果您从 YouTube 视频中复制了该代码并且它不起作用,那么您可能已经看过过时版本的 discord.py 的视频。每当你去观看编程 youtube 教程时,请记住查看编写代码的版本。
猜你喜欢
  • 2020-06-20
  • 2020-10-08
  • 2021-07-11
  • 2019-04-04
  • 2018-03-20
  • 2021-07-07
  • 2020-12-18
  • 2021-11-18
  • 2021-05-12
相关资源
最近更新 更多