【发布时间】:2021-08-16 18:25:01
【问题描述】:
我的音乐大约需要 30 秒或更长时间才能开始播放音频,我想知道是否有任何方法可以让它像 Groovy 和许多其他机器人一样更快地播放音频。对此的所有帮助表示赞赏。我知道直接从 yt 流式传输是一回事,我的机器人目前下载并提取文件,然后播放它。这是我的代码:
@bot.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, url: str):
global voice
channel = ctx.message.author.voice.channel
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()
print(f"The bot has connected to {channel}\n")
def check_queue():
Queue_infile = os.path.isdir("./Queue")
if Queue_infile is True:
DIR = os.path.abspath(os.path.realpath("Queue"))
length = len(os.listdir(DIR))
still_q = length - 1
try:
first_file = os.listdir(DIR)[0]
except:
print("No more queued song(s)\n")
queues.clear()
return
main_location = os.path.dirname(os.path.realpath(__file__))
song_path = os.path.abspath(os.path.realpath("Queue") + "\\" + first_file)
if length != 0:
print("Song done, playing next queued\n")
print(f"Songs still in queue: {still_q}")
song_there = os.path.isfile("song.mp3")
if song_there:
os.remove("song.mp3")
shutil.move(song_path, main_location)
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: check_queue())
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.07
else:
queues.clear()
return
else:
queues.clear()
print("No songs were queued before the ending of the last song\n")
【问题讨论】:
-
也许Code Review Stack Exchange 是一个更好的地方。
-
在Code Review 发帖之前,请务必阅读A guide to Code Review for Stack Overflow users,因为那里有些事情的处理方式不同 - 例如。问题标题应该简单地说明代码做了什么,因为问题总是“我该如何改进?”。确保代码正常工作;如果可能的话,包括你的单元测试。您可能会收到一些建议,使其更高效、更易于阅读和更好地测试。
标签: python discord.py