Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import discord
  2. import ffmpeg
  3. import youtube_dl
  4. import _asyncio
  5.  
  6. from discord.ext import commands
  7.  
  8. TOKEN = ''
  9.  
  10. client = commands.Bot(command_prefix='!')
  11.  
  12. players = {}
  13.  
  14. @client.event
  15. async def on_ready():
  16. print('Bot ist Online!')
  17.  
  18.  
  19. @client.command(pass_context=True)
  20. async def join(ctx):
  21. channel = ctx.message.author.voice.channel
  22. await channel.connect()
  23.  
  24.  
  25. @client.command(pass_context=True)
  26. async def leave(ctx):
  27. await ctx.voice_client.disconnect()
  28.  
  29. @client.command(pass_context=True)
  30. async def play(ctx, url):
  31. guild = ctx.message.guild
  32.  
  33. source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url))
  34. ctx.voice_client.play(source, after=lambda e: print('Player error: %s' % e) if e else None)
  35.  
  36.  
  37.  
  38. ytdl_format_options = {
  39. 'format': 'bestaudio/best',
  40. 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
  41. 'restrictfilenames': True,
  42. 'noplaylist': True,
  43. 'nocheckcertificate': True,
  44. 'ignoreerrors': False,
  45. 'logtostderr': False,
  46. 'quiet': True,
  47. 'no_warnings': True,
  48. 'default_search': 'auto',
  49. 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
  50. }
  51.  
  52. ffmpeg_options = {
  53. 'options': '-vn'
  54. }
  55.  
  56. ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
  57.  
  58. class YTDLSource(discord.PCMVolumeTransformer):
  59. def __init__(self, source, *, data, volume=0.5):
  60. super().__init__(source, volume)
  61.  
  62. self.data = data
  63.  
  64. self.title = data.get('title')
  65. self.url = data.get('url')
  66.  
  67. @classmethod
  68. async def from_url(cls, url, *, loop=None, stream=False):
  69. loop = loop or _asyncio.get_event_loop()
  70. data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
  71.  
  72. if 'entries' in data:
  73. # take first item from a playlist
  74. data = data['entries'][0]
  75.  
  76. filename = data['url'] if stream else ytdl.prepare_filename(data)
  77. return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
  78.  
  79.  
  80. client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement