#python-3.x #ffmpeg #discord #discord.py #repl.it
#python-3.x #ffmpeg #Discord #discord.py #repl.it
Вопрос:
Я новичок в python, поэтому мне интересно, есть ли способ сделать это.
Вот моя команда воспроизведения mp3:
@bot.command()
async def play_song1(ctx):
global voice
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()
voice.play(discord.FFmpegPCMAudio('./mp3/song1.mp3'))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.1
await ctx.send ('playing')
while voice.is_playing():
await asyncio.sleep(.1)
await voice.disconnect()
Я сделал еще 2 такие же команды, но для song2
and song3
, и теперь я хочу ставить mp3 в очередь, когда кто-то их вызывает.
Комментарии:
1. @Lemon.py
from discord.utils import get
Ответ №1:
Похоже, что вы не используете cogs, вы могли бы попробовать что-то вроде этого:
guild_queues = {} # Multi-server support as a dict, just use a list for one server
# EDIT: Map song names in a dict
play_messages = {
"song1": "Now playing something cool!",
"song2": "And something even cooler just started playing!",
"song3": "THE COOLEST!"
}
async def handle_queue(ctx, song):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
channel = ctx.author.voice.channel
if voice and channel and voice.channel != channel:
await voice.move_to(channel)
elif not voice and channel:
voice = await channel.connect()
if not voice.is_playing():
audio = discord.FFmpegPCMAudio(f"./{song}.mp3")
source = discord.PCMVolumeTransformer(audio)
source.volume = 0.1
voice.play(audio)
await ctx.send(play_messages[song])
while voice.is_playing():
await asyncio.sleep(.1)
if len(guild_queues[ctx.guild.id]) > 0:
next_song = guild_queues[ctx.guild.id].pop(0)
await handle_queue(ctx, next_song)
else:
await voice.disconnect()
@bot.command()
async def play(ctx, *, song): # I'd recommend adding the filenames as an arg, but it's up to you
# Feel free to add a check if the filename exists
try:
# Get the current guild's queue
queue = guild_queues[ctx.guild.id]
except KeyError:
# Create a queue if it doesn't already exist
guild_queues[ctx.guild.id] = []
queue = guild_queues[ctx.guild.id]
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
queue.append(song)
# The one song would be the one currently playing
if voice and len(queue) > 0:
await ctx.send("Added to queue!")
else:
current_song = queue.pop(0)
await handle_queue(ctx, current_song)
Ссылки:
Комментарии:
1. @jaos1 Извините, я только что увидел вашу правку! Я только что отредактировал свой ответ — вы можете сопоставить некоторые значения с названиями песен в dict
2. Теперь он добавляет песню в очередь, но продолжает зацикливать первую песню, почему?
3. @jaos1 Извините за это, исправил мой ответ!
4. @jaos1 Строка отключения была с недостаточным отступом, попробуйте еще раз