У меня есть простенький музыкальный дискорд бот для канала моих друзей.
Я хочу добавить функцию skip, но не понимаю как это сделать.
import discord
from discord.ext import commands
from discord.utils import get
import youtube_dl
import os
import config
BOT_PREFIX = '/'
bot = commands.Bot(command_prefix=BOT_PREFIX)
@bot.event
async def on_ready():
print("Logged in as: " + bot.user.name + "\n")
@bot.command(pass_context=True, aliases=['j', 'joi'])
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()
print(f"The bot has connected to {channel}\n")
await ctx.send(f"Подключился к {channel}")
@bot.command(pass_context=True, aliases=['l', 'lea'])
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()
print(f"The bot has left {channel}")
await ctx.send(f"Отключился от {channel}")
else:
print("Bot was told to leave voice channel, but was not in one")
await ctx.send("Don't think I am in a voice channel")
@bot.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, url: str):
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: Музыка уже играет!")
return
await ctx.send("Ожидайте 5-10 секунд")
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.07
nname = name.rsplit("-", 2)
await ctx.send(f"Сейчас играет: {nname[0]}")
print("playing\n")
bot.run(config.TOKEN_music_bot)