在Rhythm和Groovy不和谐机器人被删除后,我决定做一些研究,为我们的不和谐服务器制作我自己的音乐机器人。我从Youtuber教程中找到了一些代码(我真的忘记了名字,我感觉很糟糕),它能够加入、播放、暂停、恢复和断开连接。但是,我想给它添加一个队列,这样我和我的朋友就可以在玩一些游戏的时候排队一些歌曲。这就是我到目前为止所拥有的
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')
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))
我希望得到一个工作队列,如果可能的话,带有跳过和清除命令。谢谢你:)
这是我的队列命令,只是摆脱黑名单,它应该可以工作
async def check_queue(self, ctx):
if len(self.song_queue[ctx.guild.id]) > 0:
ctx.voice_client.stop()
await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
self.song_queue[ctx.guild.id].pop(0)
@commands.command()
@commands.cooldown(1, 5, commands.BucketType.user)
async def queue(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else: # display the current guilds queue
commandd = "queue"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if len(self.song_queue[ctx.guild.id]) == 0:
return await ctx.send("There are currently no songs in the queue.")
embed = discord.Embed(title="Song Queue", description="", colour=discord.Color.green().dark_gold())
i = 1
for url in self.song_queue[ctx.guild.id]:
embed.description += f"{i}) {url}\n"
i += 1
embed.set_footer(text="Thanks for using me!")
await ctx.send(embed=embed)
来自愤怒机器人的首席开发人员