我确信通过重写有更好的方法来做到这一点,但我和你在同一条船上。很长时间都想不通。
在浏览了 youtube-dl 文档和重写文档之后,这是我能想到的最好的。请记住,我不知道这是否适用于队列系统(可能不会)。另外我不知道这是一个错误还是当机器人加入时我做错了什么,然后你使用播放命令它不会输出音乐,但是如果机器人离开然后再次加入,音乐就会播放。为了解决这个问题,我让我的加入命令加入、离开和加入。
加入命令:
@bot.command(pass_context=True, brief="Makes the bot join your channel", aliases=['j', 'jo'])
async def join(ctx):
channel = ctx.message.author.voice.channel
if not channel:
await ctx.send("You are not connected to a voice channel")
return
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await ctx.send(f"Joined {channel}")
播放命令:
@bot.command(pass_context=True, brief="This will play a song 'play [url]'", aliases=['pl'])
async def play(ctx, url: str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current playing music end or use the 'stop' command")
return
await ctx.send("Getting everything ready, playing audio soon")
print("Someone wants to play music let me get that ready for them...")
voice = get(bot.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio("song.mp3"))
voice.volume = 100
voice.is_playing()
离开命令:
@bot.command(pass_context=True, brief="Makes the bot leave your channel", aliases=['l', 'le', 'lea'])
async def leave(ctx):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
await ctx.send(f"Left {channel}")
else:
await ctx.send("Don't think I am in a voice channel")
所有需要导入的(我认为):
import discord
import youtube_dl
import os
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from os import system
您可能还需要从他们的网站下载 ffmpeg(有 youtube 教程介绍如何下载和安装)
使用带有 youtube url ('/play www.youtube.com') 的播放命令帖子,它会首先查找“song.mp3”,如果有则将其删除,下载新歌曲将其重命名为“ song.mp3' 然后播放 mp3 文件。 mp3 文件将放在与您的 bot.py 相同的目录中
就像我之前说的那样,可能有一种允许队列命令的方式来做到这一点,但我现在还不知道这种方式。
希望这会有所帮助!