devops_97

Discord music bot

Apr 1st, 2022
1,595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 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. # play
  10. # stop
  11. # pause
  12. # resume
  13. # leave
  14.  
  15. # skip
  16. # loop?
  17. # add w wersji 3
  18.  
  19. @client.event
  20. async def on_ready():
  21.     print("Bot jest gotowy.")
  22.  
  23. @client.command()
  24. async def play(ctx, url : str):
  25.     song = os.path.isfile("song.mp3")
  26.     try:
  27.         if song:
  28.             os.remove("song.mp3")
  29.     except PermissionError:
  30.         await ctx.send("Piosenka jeszcze leci! Użyj komendy stop.")
  31.         return
  32.  
  33.     voiceChannel = discord.utils.get(ctx.guild.voice_channels, name="rozmówki")
  34.     await voiceChannel.connect()
  35.     voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
  36.  
  37.     ydl_opts = {
  38.         'format': 'bestaudio/best',
  39.         'postprocessors': [{
  40.             'key': 'FFmpegExtractAudio',
  41.             'preferredcodec': 'mp3',
  42.             'preferredquality': '192',
  43.         }],
  44.     }
  45.  
  46.     with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  47.         ydl.download([url])
  48.     for file in os.listdir("./"):
  49.         if file.endswith(".mp3"):
  50.             os.rename(file, "song.mp3")
  51.     voice.play(discord.FFmpegPCMAudio("song.mp3"))
  52.  
  53.  
  54.  
  55. client.run("your_token")
  56.  
Advertisement
Add Comment
Please, Sign In to add comment