首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >坚持下载,discord.py

坚持下载,discord.py
EN

Stack Overflow用户
提问于 2022-01-07 02:43:56
回答 2查看 272关注 0票数 1

到目前为止,我已经试着让代码的这一部分工作了大约两天。我曾试图寻找类似的问题,但没有一个答案奏效。

代码语言:javascript
复制
@commands.command()
async def play(self, ctx, url):
    ctx.voice_client.stop()
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': 'vn'}
    YDL_OPTIONS = {'format': 'bestaudio', 'extractaudio': True, 'audioformat' : 'mp3'}
    vc = ctx.voice_client

    with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
        info = ydl.extract_info(url, download = False)
        url2 = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
        vc.play(source)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-07 14:15:29

YouTube DL并不是最好的音乐机器人。我建议使用小波。您可以创建自己的命令(在Cog中或不在Cog中),也可以使用消音

下面的示例if用于与名为dismusic的预先制作的音乐齿轮一起使用小波

代码语言:javascript
复制
# 1 - Install ffmpeg (and add to path)
#        Debian/Ubuntu -
#           apt install ffmpeg -y   - On Ubuntu
#        Windows -
#           Download ffmpeg, add to PATH or copy the ffmpeg.exe to System32 Folder

# 2 - Install discord, wavelink, lavalink, PyNacl and dismusic with the command below
#        Debian/Ubuntu -
#           pip3 install lavalink wavelink discord PyNacl dismusic
#        Windows -
#           pip install lavalink wavelink discord PyNacl dismusic

# Code for a basic discord bot
from discord.ext import commands
client = commands.Bot(command_prefix='--')

@client.event
async def on_ready():
    print('+ Logged in as {0.user}'.format(bot))

# Lavalink node to connect to
client.lava_nodes = [
    {
        # a free public node
        # ----------------------
        # 'host': "lava.link", 
        # 'port': 80,
        # 'rest_uri': f'http://lava.link:80',
        # 'password': 'anyPassword',

        # for hosting your own Lavalink locally (More stable)
        # Follow the steps below this code snippet only if you are doing this
        # 
        # ----------------------
        'host': "127.0.0.1",
        'port': 2333,
        'rest_uri': f'http://127.0.0.1:2333',
        'password': 'youshallnotpass',

        # Other Settings
        # ----------------------
        'identifier': 'MAIN',
        'region': 'singapore'
    }
]

# loading the installed, pre-made music cog to the bot
client.load_extension('dismusic')

# Running the bot
bot.run("DoNotShareYourTOKEN")

如果选择免费公共lava.link服务器(http://lava.link:80),则可以跳过以下步骤

如果使用Debain/Ubuntu,您可以逐行运行下面的命令来启动自己的Lavalink服务器

代码语言:javascript
复制
sudo apt install wget curl default-jdk -y
mkdir Lavalink && cd Lavalink # Optional
wget "https://github.com/freyacodes/Lavalink/releases/download/3.4/Lavalink.jar"
curl "https://raw.githubusercontent.com/hirusha-adi/Near/main/others/application.yml" >> "application.yml"
java -jar ./Lavalink.jar

如果您正在使用windows来承载不和谐(这不是最好的主意,但如果您想要的话),请按照写好的bel步骤执行。

  1. 安装java并添加到路径
  2. 下载Lavalink.jarapplication.yml
  3. 将两个下载的文件放在同一个文件夹中。
  4. 在该文件夹中打开CMD或Powershell并运行java -jar Lavalink.jar

可用命令-

--play -播放一首歌或播放列表

--pause -暂停播放器

--connect -连接到vc

--seek -寻找玩家

--nowplaying -现在播放

--queue -查看队列

--equalizer集均衡器

--volume集卷

--resume -恢复播放器

--loop -循环歌曲/播放列表

票数 0
EN

Stack Overflow用户

发布于 2022-01-07 09:18:43

嘿,我看到你只是想播放这首歌,这是一些我发现的代码,可能有用。试试看!

代码语言:javascript
复制
async def play(self, ctx, url):
  ffmpeg_options = {
      'options': '-vn',
      "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"
  }
  ytdlopts = {
      'format': 'bestaudio/best',
      'outtmpl': 'downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s',
      'restrictfilenames': True,
      'noplaylist': True,
      'nocheckcertificate': True,
      'ignoreerrors': False,
      'logtostderr': False,
      'quiet': True,
      'no_warnings': True,
      'default_search': 'auto',
      'source_address': '0.0.0.0'
  }
  vc = ctx.voice_client
  with youtube_dl.YoutubeDL(ytdlopts) as ydl:
    info = ydl.extract_info(url, download = False)
    url2 = info['formats'][0]['url']
    source = discord.FFmpegPCMAudio(url2, **ffmpeg_options)
    vc.play(source)

你可能得修几件东西,但这应该管用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70616009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档