到目前为止,我已经试着让代码的这一部分工作了大约两天。我曾试图寻找类似的问题,但没有一个答案奏效。
@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)发布于 2022-01-07 14:15:29
YouTube DL并不是最好的音乐机器人。我建议使用小波。您可以创建自己的命令(在Cog中或不在Cog中),也可以使用消音
下面的示例if用于与名为dismusic的预先制作的音乐齿轮一起使用小波
# 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服务器
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步骤执行。
java -jar Lavalink.jar可用命令-
--play -播放一首歌或播放列表
--pause -暂停播放器
--connect -连接到vc
--seek -寻找玩家
--nowplaying -现在播放
--queue -查看队列
--equalizer集均衡器
--volume集卷
--resume -恢复播放器
--loop -循环歌曲/播放列表
发布于 2022-01-07 09:18:43
嘿,我看到你只是想播放这首歌,这是一些我发现的代码,可能有用。试试看!
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)你可能得修几件东西,但这应该管用。
https://stackoverflow.com/questions/70616009
复制相似问题