Я нахожусь в процессе создания музыкального бота discord.py (мой первый), и мне интересно, как создать очередь. Я предполагаю, что это как-то связано с командой asyncio
(import asyncio
), но я действительно не знаю.
from discord.ext import commands
from discord.utils import get
import asyncio
import youtube_dl
import os
bot = commands.Bot(command_prefix='>')
bot.remove_command('help')
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name = ">help"))
print("Bot is online! Logged in as: " + bot.user.name + "\n")
@bot.command(pass_context=True)
async def ping(ctx):
await ctx.send(f'**Pong!** Latency: {round(bot.latency * 1000)}ms')
@bot.command(pass_context=True, aliases=['j'])
async def join(ctx):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
@bot.command(pass_context=True, aliases=['l'])
async def leave(ctx):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
else:
print("Bot was told to leave voice channel, but was not in one.")
await ctx.send("OneBeat is not connected to a voice channel. Connect OneBeat to a voice channel before using this command.")
@bot.command(pass_context=True, aliases=['p'])
async def play(ctx, url: str):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
print("Removed old song file.")
except PermissionError:
print("Trying to delete song file, but it's being played")
await ctx.send("Error: Music is already playing (Queue feature coming soon).")
return
await ctx.send("One second...")
voice = get(bot.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print("Downloading audio now\n")
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
print(f"Renamed File: {file}\n")
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print("Song done!"))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.5
nname = name.rsplit("-", 2)
await ctx.send(f"Now playing: {nname[0]}")
print("Playing\n")
bot.run('token')
Не совсем! Я понятия не имею, как использовать asyncio, и мне все же удалось создать очереди. Мой способ сделать это - иметь файл json, содержащий словари и списки. Что выглядит примерно так:
{
"queueskey": [
{
"channelid": [],
"queue": [],
"status": []
},
{
"channelid": [],
"queue": [],
"status": []
},
{
"channelid": [],
"queue": [],
"status": []
}
]
}
Что-то подобное повторяется снова и снова. Вы также можете хранить дополнительную информацию. Хотя мне потребовалось довольно много времени, чтобы отладить и заставить все это работать, я бы сказал, что это один из самых простых способов сделать это.
Взгляните на asyncio.Queue