Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. import asyncio
  2.  
  3. import discord
  4. import youtube_dl
  5.  
  6. from discord.ext import commands
  7.  
  8. # Suppress noise about console usage from errors
  9. youtube_dl.utils.bug_reports_message = lambda: ''
  10.  
  11.  
  12. ytdl_format_options = {
  13.     'format': 'bestaudio/best',
  14.     'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
  15.     'restrictfilenames': True,
  16.     'noplaylist': True,
  17.     'nocheckcertificate': True,
  18.     'ignoreerrors': False,
  19.     'logtostderr': False,
  20.     'quiet': True,
  21.     'no_warnings': True,
  22.     'default_search': 'auto',
  23.     'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
  24. }
  25.  
  26. ffmpeg_options = {
  27.     'options': '-vn'
  28. }
  29.  
  30. client = commands.Bot(command_prefix='?')
  31.  
  32.  
  33. @client.event
  34. async def on_ready():
  35.     print('anti-black is ready!')
  36.  
  37.  
  38. @client.command()
  39. async def dude(ctx):
  40.     await ctx.send("yeet and flex on my applewatch ni...")
  41.  
  42.  
  43.  
  44. ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
  45.  
  46.  
  47. class YTDLSource(discord.PCMVolumeTransformer):
  48.     def __init__(self, source, *, data, volume=0.5):
  49.         super().__init__(source, volume)
  50.  
  51.         self.data = data
  52.  
  53.         self.title = data.get('title')
  54.         self.url = data.get('url')
  55.  
  56.     @classmethod
  57.     async def from_url(cls, url, *, loop=None, stream=False):
  58.         loop = loop or asyncio.get_event_loop()
  59.         data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
  60.  
  61.         if 'entries' in data:
  62.             # take first item from a playlist
  63.             data = data['entries'][0]
  64.  
  65.         filename = data['url'] if stream else ytdl.prepare_filename(data)
  66.         return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
  67.  
  68.  
  69. class Music(commands.Cog):
  70.     def __init__(self, bot):
  71.         self.bot = bot
  72.  
  73.  
  74.  
  75.     @commands.command()
  76.     async def join(self, ctx, *, channel: discord.VoiceChannel):
  77.         """Joins a voice channel"""
  78.  
  79.         if ctx.voice_client is not None:
  80.             return await ctx.voice_client.move_to(channel)
  81.  
  82.         await channel.connect()
  83.  
  84.     @commands.command()
  85.     async def play(self, ctx, *, query):
  86.         """Plays a file from the local filesystem"""
  87.  
  88.         source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(query))
  89.         ctx.voice_client.play(source, after=lambda e: print('Player error: %s' % e) if e else None)
  90.  
  91.         await ctx.send('Now playing: {}'.format(query))
  92.  
  93.     @commands.command()
  94.     async def yt(self, ctx, *, url):
  95.         """Plays from a url (almost anything youtube_dl supports)"""
  96.  
  97.         async with ctx.typing():
  98.             player = await YTDLSource.from_url(url, loop=self.bot.loop)
  99.             ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
  100.  
  101.         await ctx.send('Now playing: {}'.format(player.title))
  102.  
  103.     @commands.command()
  104.     async def stream(self, ctx, *, url):
  105.         """Streams from a url (same as yt, but doesn't predownload)"""
  106.  
  107.         async with ctx.typing():
  108.             player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
  109.             ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
  110.  
  111.         await ctx.send('Now playing: {}'.format(player.title))
  112.  
  113.     @commands.command()
  114.     async def volume(self, ctx, volume: int):
  115.         """Changes the player's volume"""
  116.  
  117.         if ctx.voice_client is None:
  118.             return await ctx.send("Not connected to a voice channel.")
  119.  
  120.         ctx.voice_client.source.volume = volume / 100
  121.         await ctx.send("Changed volume to {}%".format(volume))
  122.  
  123.     @commands.command()
  124.     async def stop(self, ctx):
  125.         """Stops and disconnects the bot from voice"""
  126.  
  127.         await ctx.voice_client.disconnect()
  128.  
  129.     @play.before_invoke
  130.     @yt.before_invoke
  131.     @stream.before_invoke
  132.     async def ensure_voice(self, ctx):
  133.         if ctx.voice_client is None:
  134.             if ctx.author.voice:
  135.                 await ctx.author.voice.channel.connect()
  136.             else:
  137.                 await ctx.send("You are not connected to a voice channel.")
  138.                 raise commands.CommandError("Author not connected to a voice channel.")
  139.         elif ctx.voice_client.is_playing():
  140.             ctx.voice_client.stop()
  141.  
  142. bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"),
  143.                    description='Relatively simple music bot example')
  144.  
  145. @bot.event
  146. async def on_ready():
  147.     print('Logged in as {0} ({0.id})'.format(bot.user))
  148.     print('------')
  149.  
  150.  
  151. bot.add_cog(Music(bot))
  152. bot.run('TOKAN')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement