devops_97

Python - Music bot v2 01.04.2022

Apr 1st, 2022
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. import os
  4. import youtube_dl
  5.  
  6. client = commands.Bot(command_prefix="!")
  7.  
  8. ## komendy ktore potrzebujemy
  9. # pause
  10. # resume
  11. # skip
  12. # loop?
  13. # add w wersji 3
  14.  
  15. @client.event
  16. async def on_ready():
  17.     print("Bot jest gotowy.")
  18.  
  19. @client.command()
  20. async def play(ctx, url : str):
  21.     song = os.path.isfile("song.mp3")
  22.     try:
  23.         if song:
  24.             os.remove("song.mp3")
  25.     except PermissionError:
  26.         await ctx.send("Piosenka jeszcze leci! Użyj komendy stop.")
  27.         return
  28.  
  29.     voiceChannel = discord.utils.get(ctx.guild.voice_channels, name="rozmówki")
  30.     await voiceChannel.connect()
  31.     voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
  32.  
  33.     ydl_opts = {
  34.         'format': 'bestaudio/best',
  35.         'postprocessors': [{
  36.             'key': 'FFmpegExtractAudio',
  37.             'preferredcodec': 'mp3',
  38.             'preferredquality': '192',
  39.         }],
  40.     }
  41.  
  42.     with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  43.         ydl.download([url])
  44.     for file in os.listdir("./"):
  45.         if file.endswith(".mp3"):
  46.             os.rename(file, "song.mp3")
  47.     voice.play(discord.FFmpegPCMAudio("song.mp3"))
  48.  
  49. @client.command()
  50. async def leave(ctx):
  51.     voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
  52.     if voice.is_connected():
  53.         await voice.disconnect()
  54.     else:
  55.         await ctx.send("Bot nie jest aktywny na kanale")
  56.  
  57. @client.command()
  58. async def stop(ctx):
  59.     voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
  60.     if voice.is_playing():
  61.         voice.stop()
  62.     else:
  63.         await ctx.send("Nic aktualnie nie gra")
  64.  
  65. @client.command()
  66. async def pause(ctx):
  67.     voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
  68.     if voice.is_playing():
  69.         voice.pause()
  70.     else:
  71.         await ctx.send("Piosenka jest zapauzowana")
  72.  
  73. @client.command()
  74. async def resume(ctx):
  75.     voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
  76.     if voice.is_paused():
  77.         voice.resume()
  78.     else:
  79.         await ctx.send("Piosenka nie jest zpauzowana")
  80.  
  81. client.run(your_token_here)
  82.  
Advertisement
Add Comment
Please, Sign In to add comment