【发布时间】:2021-11-11 23:07:47
【问题描述】:
Rhythm 和 Groovy discord 机器人被取消后,我决定做一些研究,为我们的 discord 服务器制作我自己的音乐机器人。我从 Youtuber 教程中找到了一些代码(老实说,我忘记了名字,我感觉很糟糕),它能够加入、播放、暂停、恢复和断开连接。但是,我想给它添加一个队列,这样我和我的朋友就可以在玩一些游戏的同时排队一些歌曲。这就是我目前所拥有的
- main.py
import discord
from discord.ext import commands
import music
cogs = [music]
client = commands.Bot(command_prefix='!', intents = discord.Intents.all())
for i in range(len(cogs)):
cogs[i].setup(client)
client.run('TOKEN')
- 音乐.py
import discord
from discord.ext import commands
import youtube_dl
class music(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("Get in a voice channel idiot!")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
@commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
@commands.command()
async def play(self,ctx,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
self.music_queue = []
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url,download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2,**FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self, ctx):
await ctx.voice_client.pause()
await ctx.send("Paused ⏸️")
@commands.command()
async def resume(self, ctx):
await ctx.voice_client.resume()
await ctx.send("Resumed ⏯️ ")
def setup(client):
client.add_cog(music(client))
我希望得到一个工作队列,如果可能的话,使用跳过和清除命令。谢谢:)
【问题讨论】:
-
您查看过queue 模块吗?
-
你不应该真的在异步上下文中使用队列模块,考虑使用
asyncio.queue -
@WasiMaster 你知道我将如何实现它吗?就像我说的,我对代码还不是很感兴趣。而且我不确定我应该在该示例中更改什么以使其与我的链接和工作良好
-
@JordanAmo 这是 Mysty 在 discord.py 官方服务器上写的 example。不要只是明目张胆地复制粘贴,看看并尝试理解。然后自己做
标签: python discord.py