我制作了一个discord bot命令来播放音乐。它做得很好,也在播放音乐。问题是,每当我命令它时,它都会在我的系统上下载音乐。我的系统上没有太多的空间来保留这些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: ")
在项目中创建名为
cogs
的文件夹,然后将名为music.py
的文件添加到cogs文件夹中。将下面的所有代码放在music.py
文件中:然后,在主
.py
文件的on_ready()
方法中,添加以下内容:将代码修改为希望命令打印的内容。请验证此答案是否有效
相关问题 更多 >
编程相关推荐