【发布时间】:2021-12-19 03:24:11
【问题描述】:
我正在寻找一种将以下内容添加到我的音乐机器人的方法:
- 队列功能,允许在播放另一首歌曲时输入歌曲。这将在第一首歌曲结束后通过播放功能
- 能够显示此队列
- 从队列中移除歌曲/更改歌曲位置
我该怎么做呢?下面是我的代码。
from discord.ext import commands
from dotenv import load_dotenv
from keep_alive import keep_alive
from youtube_dl import YoutubeDL
from discord.utils import get
load_dotenv()
TOKEN = os.getenv('TOKEN')
bot = commands.Bot(command_prefix='!', case_insensitive=True)
@bot.event
async def on_ready():
print(f'{bot.user} has successfully connected to Discord!')
@bot.command(aliases=['p'])
async def play(ctx, url: str = None):
YDL_OPTIONS = {'format': 'bestaudio/best', 'noplaylist':'True'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
voice = get(bot.voice_clients, guild=ctx.guild)
try:
channel = ctx.message.author.voice.channel
except AttributeError:
await ctx.send('Bruh, join a voice channel')
return None
if not voice:
await channel.connect()
await ctx.send("Music/Audio will begin shortly, unless no URL provided.")
voice = get(bot.voice_clients, guild=ctx.guild)
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
I_URL = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(I_URL, **FFMPEG_OPTIONS)
voice.play(source)
voice.is_playing()
await ctx.send("Playing audio.")
@bot.command()
async def pause(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
await ctx.send("Audio is paused.")
if voice.is_not_playing():
await ctx.send("Currently no audio is playing.")
@bot.command()
async def resume(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_paused():
voice.resume()
await ctx.send("Resumed audio.")
if voice.is_not_paused():
await ctx.send("The audio is not paused.")
@bot.command()
async def stop(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
voice.stop()
await ctx.send("Stopped playback.")
@bot.command(aliases=['l'])
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
await voice.disconnect()
await ctx.send("Left voice channel.")
keep_alive()
bot.run(TOKEN
【问题讨论】:
-
欢迎来到 SO!该站点通常用于您可能遇到或需要解决的特定代码错误或错误,因此如果此问题已关闭,请不要担心。要添加队列,您可能想要存储歌曲列表,并遍历它们(for loop 或 iter.next())。队列对于永久存储来说并不是很重要,但是如果你想保证它的安全,请查看 json 模块,它与 open() 函数结合使用时,可以让你将字典写入硬盘上的文件驱动器,用于永久存储。
-
大声笑,你的机器人会因为使用 YTDL 而被删除。万事如意
-
该机器人正在我和我的朋友的一台个人服务器中使用。
-
queue必须是global列表 - 这样所有函数都可以访问队列
标签: python discord discord.py