Discord.py Bot无法播放音乐

时间:2020-10-23 18:11:16

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

大家好,出于某种原因,我的机器人可以加入,但无法播放音频。 实际上,我对此完全陌生,我试图使它像两个小时一样工作,并设法设置了加入但不播放(或离开)命令。

import os
import random
from dotenv import load_dotenv
import youtube_dl
import discord
from discord.ext import commands

from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
players={}


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

@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')


@bot.command(name="Ja_som_to_nebol.")
async def sranda(ctx):
    sranda_quotes = ["Not gonna lie, kinda sus",
        ("Not gonna lie, kinda sus"), ]
    response = random.choice(sranda_quotes)
    await ctx.send(response)
    
#@bot.event
#async def on_command_error(ctx, error):
   # if isinstance(error, commands.errors.CheckFailure):
   #     await ctx.send('You do not have the correct role for this command.')

@bot.command(name="join")
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()

@bot.command(name="leave")
async def leave(ctx):
    channel = ctx.author.voice.channel
    await channel.disconnect()
   
   

@bot.command(name="play")
async def play(ctx, url):
    guild = ctx.author.voice.channel
    voice_client = discord.utils.find(lambda c: c.guild.id == server.id, client.voice_client)
    player = await voice_client.create_ytdl_player(url)
    players=[server.id] = player
    player.start()


bot.run(TOKEN)

1 个答案:

答案 0 :(得分:0)

您必须安装FFmpeg,而不是像这样使用我使用的代码。

@commands.command(name='play', aliases=['p'])
async def _play(self, ctx: commands.Context, *, search: str):
  if not ctx.voice_state.voice:
    await ctx.invoke(self._join)

  async with ctx.typing():
    try:
      source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop)
    except YTDLError as e:
      await ctx.send('An error occurred while processing this request: {}'.format(str(e)))
  else:
    song = Song(source)

    await ctx.voice_state.songs.put(song)
    await ctx.send('Enqueued {}'.format(str(source)))

我在这里使用一些齿轮,您可以自己编辑为普通代码。