【发布时间】:2022-02-24 23:44:26
【问题描述】:
我做了一个不和谐的机器人命令来播放音乐。而且它做的一切都很好,它也在播放音乐。问题是每当我命令它在我的系统上下载音乐。我没有太多空间来保留那些 mp3在我的系统上。那么,我能用这个做什么。这是下面给出的代码。希望你能提供帮助。(我还下载了所有模块,包括 ffmpeg)。
@client.command(aliases=["p"])
async def play(ctx, *, query):
try:
voiceChannel = discord.utils.get(ctx.guild.voice_channels, name=str(ctx.message.author.voice.channel))
await voiceChannel.connect()
await ctx.send("Joined " + str(ctx.message.author.voice.channel) + " voice channel!:white_check_mark:")
except AttributeError:
await ctx.send(ctx.message.author.mention + " is not in any voice channel :negative_squared_cross_mark:")
return
except Exception as e:
print(e)
url = None
if len(query) == 0:
await ctx.send(
ctx.message.author.mention + "you need to provide a youtube video link or any query with the play command :negative_squared_cross_mark:")
return
elif query.startswith("https://www.youtube.com/watch?v="):
url = query
else:
s = gs.search("https://www.youtube.com/results?search_query=" + query.replace(" ", "+"), "com", "en", num=10,
stop=10, pause=2.0)
for i in s:
if i.startswith("https://www.youtube.com/watch?v="):
url = i
break
if url == None:
await ctx.send(ctx.message.author.mention + " some error is caused :negative_squared_cross_mark:")
return
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
yt = YouTube(str(url))
yt_embed = discord.Embed(title=yt.title + ":musical_note:", description=yt.description, color=discord.Colour.red())
yt_embed.set_thumbnail(url=yt.thumbnail_url)
yt_embed.add_field(name="Author: ", value=yt.author + ":musical_score: ", inline=False)
yt_embed.add_field(name="Duration: ", value=str(yt.length) + " seconds :clock3: ", inline=False)
yt_embed.add_field(name="Publish date: ", value=str(yt.publish_date) + ":calendar_spiral:", inline=False)
yt_embed.add_field(name="Rating: ", value=str(yt.rating) + ":star2:", inline=False)
yt_embed.add_field(name="Views: ", value=str(yt.views) + ":eyes:", inline=False)
t = yt.streams.filter(only_audio=True)
t[0].download(".\songs")
try:
print(".\songs\\" + yt.title + ".mp4")
voice.play(discord.FFmpegPCMAudio(".\songs\\" + yt.title + ".mp4"))
await ctx.send("Playing " + yt.title + " :loud_sound:")
await ctx.send(embed=yt_embed)
except Exception as e:
print(e)
await ctx.send(ctx.message.author.mention + " Alena already playing audio :negative_squared_cross_mark:")
await ctx.send(
"Use stop command to stop the currently playing song and leave command to make Alena exit the current voice channel")
return
@client.command(aliases=["disconnect", "exit"])
async def leave(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice.is_connected():
await voice.disconnect()
await ctx.send("Disconnected :wave:")
else:
await ctx.send("The bot is not connected to a voice channel. :negative_squared_cross_mark:")
@client.command()
async def pause(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
await ctx.send("Paused :pause_button:")
else:
await ctx.send("Currently no audio is playing. :negative_squared_cross_mark:")
@client.command()
async def resume(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice.is_paused():
voice.resume()
await ctx.send("Resumed :play_pause: ")
else:
await ctx.send("The audio is not paused. :negative_squared_cross_mark:")
@client.command()
async def stop(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
voice.stop()
await ctx.send("Stopped playing :octagonal_sign: ")
【问题讨论】:
-
看看this例子
-
@ŁukaszKwieciński 甚至不播放音乐
标签: python discord.py