SHARE
TWEET

Satan Gay

a guest Feb 16th, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import asyncio
  2. import functools
  3. import itertools
  4. import math
  5. import random
  6.  
  7. import discord
  8. import youtube_dl
  9. from async_timeout import timeout
  10. from discord.ext import commands
  11.  
  12. # Silence useless bug reports messages
  13. youtube_dl.utils.bug_reports_message = lambda: ''
  14.  
  15.  
  16. class VoiceError(Exception):
  17.     pass
  18.  
  19.  
  20. class YTDLError(Exception):
  21.     pass
  22.  
  23.  
  24. class YTDLSource(discord.PCMVolumeTransformer):
  25.     YTDL_OPTIONS = {
  26.         'format': 'bestaudio/best',
  27.         'extractaudio': True,
  28.         'audioformat': 'mp3',
  29.         'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
  30.         'restrictfilenames': True,
  31.         'noplaylist': True,
  32.         'nocheckcertificate': True,
  33.         'ignoreerrors': False,
  34.         'logtostderr': False,
  35.         'quiet': True,
  36.         'no_warnings': True,
  37.         'default_search': 'auto',
  38.         'source_address': '0.0.0.0',
  39.     }
  40.  
  41.     FFMPEG_OPTIONS = {
  42.         'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
  43.         'options': '-vn',
  44.     }
  45.  
  46.     ytdl = youtube_dl.YoutubeDL(YTDL_OPTIONS)
  47.  
  48.     def __init__(self, ctx: commands.Context, source: discord.FFmpegPCMAudio, *, data: dict, volume: float = 0.5):
  49.         super().__init__(source, volume)
  50.  
  51.         self.requester = ctx.author
  52.         self.channel = ctx.channel
  53.         self.data = data
  54.  
  55.         self.uploader = data.get('uploader')
  56.         self.uploader_url = data.get('uploader_url')
  57.         date = data.get('upload_date')
  58.         self.upload_date = date[6:8] + '.' + date[4:6] + '.' + date[0:4]
  59.         self.title = data.get('title')
  60.         self.thumbnail = data.get('thumbnail')
  61.         self.description = data.get('description')
  62.         self.duration = self.parse_duration(int(data.get('duration')))
  63.         self.tags = data.get('tags')
  64.         self.url = data.get('webpage_url')
  65.         self.views = data.get('view_count')
  66.         self.likes = data.get('like_count')
  67.         self.dislikes = data.get('dislike_count')
  68.         self.stream_url = data.get('url')
  69.  
  70.     def __str__(self):
  71.         return '**{0.title}** by **{0.uploader}**'.format(self)
  72.  
  73.     @classmethod
  74.     async def create_source(cls, ctx: commands.Context, search: str, *, loop: asyncio.BaseEventLoop = None):
  75.         loop = loop or asyncio.get_event_loop()
  76.  
  77.         partial = functools.partial(cls.ytdl.extract_info, search, download=False, process=False)
  78.         data = await loop.run_in_executor(None, partial)
  79.  
  80.         if data is None:
  81.             raise YTDLError('Couldn\'t find anything that matches `{}`'.format(search))
  82.  
  83.         if 'entries' not in data:
  84.             process_info = data
  85.         else:
  86.             process_info = None
  87.             for entry in data['entries']:
  88.                 if entry:
  89.                     process_info = entry
  90.                     break
  91.  
  92.             if process_info is None:
  93.                 raise YTDLError('Couldn\'t find anything that matches `{}`'.format(search))
  94.  
  95.         webpage_url = process_info['webpage_url']
  96.         partial = functools.partial(cls.ytdl.extract_info, webpage_url, download=False)
  97.         processed_info = await loop.run_in_executor(None, partial)
  98.  
  99.         if processed_info is None:
  100.             raise YTDLError('Couldn\'t fetch `{}`'.format(webpage_url))
  101.  
  102.         if 'entries' not in processed_info:
  103.             info = processed_info
  104.         else:
  105.             info = None
  106.             while info is None:
  107.                 try:
  108.                     info = processed_info['entries'].pop(0)
  109.                 except IndexError:
  110.                     raise YTDLError('Couldn\'t retrieve any matches for `{}`'.format(webpage_url))
  111.  
  112.         return cls(ctx, discord.FFmpegPCMAudio(info['url'], **cls.FFMPEG_OPTIONS), data=info)
  113.  
  114.     @staticmethod
  115.     def parse_duration(duration: int):
  116.         minutes, seconds = divmod(duration, 60)
  117.         hours, minutes = divmod(minutes, 60)
  118.         days, hours = divmod(hours, 24)
  119.  
  120.         duration = []
  121.         if days > 0:
  122.             duration.append('{} days'.format(days))
  123.         if hours > 0:
  124.             duration.append('{} hours'.format(hours))
  125.         if minutes > 0:
  126.             duration.append('{} minutes'.format(minutes))
  127.         if seconds > 0:
  128.             duration.append('{} seconds'.format(seconds))
  129.  
  130.         return ', '.join(duration)
  131.  
  132.  
  133. class Song:
  134.     __slots__ = ('source', 'requester')
  135.  
  136.     def __init__(self, source: YTDLSource):
  137.         self.source = source
  138.         self.requester = source.requester
  139.  
  140.     def create_embed(self):
  141.         embed = (discord.Embed(title='Now playing',
  142.                                description='```css\n{0.source.title}\n```'.format(self),
  143.                                color=discord.Color.blurple())
  144.                  .add_field(name='Duration', value=self.source.duration)
  145.                  .add_field(name='Requested by', value=self.requester.mention)
  146.                  .add_field(name='Uploader', value='[{0.source.uploader}]({0.source.uploader_url})'.format(self))
  147.                  .add_field(name='URL', value='[Click]({0.source.url})'.format(self))
  148.                  .set_thumbnail(url=self.source.thumbnail))
  149.  
  150.         return embed
  151.  
  152.  
  153. class SongQueue(asyncio.Queue):
  154.     def __getitem__(self, item):
  155.         if isinstance(item, slice):
  156.             return list(itertools.islice(self._queue, item.start, item.stop, item.step))
  157.         else:
  158.             return self._queue[item]
  159.  
  160.     def __iter__(self):
  161.         return self._queue.__iter__()
  162.  
  163.     def __len__(self):
  164.         return self.qsize()
  165.  
  166.     def clear(self):
  167.         self._queue.clear()
  168.  
  169.     def shuffle(self):
  170.         random.shuffle(self._queue)
  171.  
  172.     def remove(self, index: int):
  173.         del self._queue[index]
  174.  
  175.  
  176. class VoiceState:
  177.     def __init__(self, bot: commands.Bot, ctx: commands.Context):
  178.         self.bot = bot
  179.         self._ctx = ctx
  180.  
  181.         self.current = None
  182.         self.voice = None
  183.         self.next = asyncio.Event()
  184.         self.songs = SongQueue()
  185.  
  186.         self._loop = False
  187.         self._volume = 0.5
  188.         self.skip_votes = set()
  189.  
  190.         self.audio_player = bot.loop.create_task(self.audio_player_task())
  191.  
  192.     def __del__(self):
  193.         self.audio_player.cancel()
  194.  
  195.     @property
  196.     def loop(self):
  197.         return self._loop
  198.  
  199.     @loop.setter
  200.     def loop(self, value: bool):
  201.         self._loop = value
  202.  
  203.     @property
  204.     def volume(self):
  205.         return self._volume
  206.  
  207.     @volume.setter
  208.     def volume(self, value: float):
  209.         self._volume = value
  210.  
  211.     @property
  212.     def is_playing(self):
  213.         return self.voice and self.current
  214.  
  215.     async def audio_player_task(self):
  216.         while True:
  217.             self.next.clear()
  218.  
  219.             if not self.loop:
  220.                 # Try to get the next song within 3 minutes.
  221.                 # If no song will be added to the queue in time,
  222.                 # the player will disconnect due to performance
  223.                 # reasons.
  224.                 try:
  225.                     async with timeout(180):  # 3 minutes
  226.                         self.current = await self.songs.get()
  227.                 except asyncio.TimeoutError:
  228.                     self.bot.loop.create_task(self.stop())
  229.                     return
  230.  
  231.             self.current.source.volume = self._volume
  232.             self.voice.play(self.current.source, after=self.play_next_song)
  233.             await self.current.source.channel.send(embed=self.current.create_embed())
  234.  
  235.             await self.next.wait()
  236.  
  237.     def play_next_song(self, error=None):
  238.         if error:
  239.             raise VoiceError(str(error))
  240.  
  241.         self.next.set()
  242.  
  243.     def skip(self):
  244.         self.skip_votes.clear()
  245.  
  246.         if self.is_playing:
  247.             self.voice.stop()
  248.  
  249.     async def stop(self):
  250.         self.songs.clear()
  251.  
  252.         if self.voice:
  253.             await self.voice.disconnect()
  254.             self.voice = None
  255.  
  256.  
  257. class Music(commands.Cog):
  258.     def __init__(self, bot: commands.Bot):
  259.         self.bot = bot
  260.         self.voice_states = {}
  261.  
  262.     def get_voice_state(self, ctx: commands.Context):
  263.         state = self.voice_states.get(ctx.guild.id)
  264.         if not state:
  265.             state = VoiceState(self.bot, ctx)
  266.             self.voice_states[ctx.guild.id] = state
  267.  
  268.         return state
  269.  
  270.     def cog_unload(self):
  271.         for state in self.voice_states.values():
  272.             self.bot.loop.create_task(state.stop())
  273.  
  274.     def cog_check(self, ctx: commands.Context):
  275.         if not ctx.guild:
  276.             raise commands.NoPrivateMessage('This command can\'t be used in DM channels.')
  277.  
  278.         return True
  279.  
  280.     async def cog_before_invoke(self, ctx: commands.Context):
  281.         ctx.voice_state = self.get_voice_state(ctx)
  282.  
  283.     async def cog_command_error(self, ctx: commands.Context, error: commands.CommandError):
  284.         await ctx.send('An error occurred: {}'.format(str(error)))
  285.  
  286.     @commands.command(name='join', invoke_without_subcommand=True)
  287.     async def _join(self, ctx: commands.Context):
  288.         """Joins a voice channel."""
  289.  
  290.         destination = ctx.author.voice.channel
  291.         if ctx.voice_state.voice:
  292.             await ctx.voice_state.voice.move_to(destination)
  293.             return
  294.  
  295.         ctx.voice_state.voice = await destination.connect()
  296.  
  297.     @commands.command(name='summon')
  298.     @commands.has_permissions(manage_guild=True)
  299.     async def _summon(self, ctx: commands.Context, *, channel: discord.VoiceChannel = None):
  300.         """Summons the bot to a voice channel.
  301.         If no channel was specified, it joins your channel.
  302.         """
  303.  
  304.         if not channel and not ctx.author.voice:
  305.             raise VoiceError('You are neither connected to a voice channel nor specified a channel to join.')
  306.  
  307.         destination = channel or ctx.author.voice.channel
  308.         if ctx.voice_state.voice:
  309.             await ctx.voice_state.voice.move_to(destination)
  310.             return
  311.  
  312.         ctx.voice_state.voice = await destination.connect()
  313.  
  314.     @commands.command(name='leave', aliases=['disconnect'])
  315.     @commands.has_permissions(manage_guild=True)
  316.     async def _leave(self, ctx: commands.Context):
  317.         """Clears the queue and leaves the voice channel."""
  318.  
  319.         if not ctx.voice_state.voice:
  320.             return await ctx.send('Not connected to any voice channel.')
  321.  
  322.         await ctx.voice_state.stop()
  323.         del self.voice_states[ctx.guild.id]
  324.  
  325.     @commands.command(name='volume')
  326.     async def _volume(self, ctx: commands.Context, *, volume: int):
  327.         """Sets the volume of the player."""
  328.  
  329.         if not ctx.voice_state.is_playing:
  330.             return await ctx.send('Nothing being played at the moment.')
  331.  
  332.         if 0 > volume > 100:
  333.             return await ctx.send('Volume must be between 0 and 100')
  334.  
  335.         ctx.voice_state.volume = volume / 100
  336.         await ctx.send('Volume of the player set to {}%'.format(volume))
  337.  
  338.     @commands.command(name='now', aliases=['current', 'playing'])
  339.     async def _now(self, ctx: commands.Context):
  340.         """Displays the currently playing song."""
  341.  
  342.         await ctx.send(embed=ctx.voice_state.current.create_embed())
  343.  
  344.     @commands.command(name='pause')
  345.     @commands.has_permissions(manage_guild=True)
  346.     async def _pause(self, ctx: commands.Context):
  347.         """Pauses the currently playing song."""
  348.  
  349.         if not ctx.voice_state.is_playing and ctx.voice_state.voice.is_playing():
  350.             ctx.voice_state.voice.pause()
  351.             await ctx.message.add_reaction('⏯')
  352.  
  353.     @commands.command(name='resume')
  354.     @commands.has_permissions(manage_guild=True)
  355.     async def _resume(self, ctx: commands.Context):
  356.         """Resumes a currently paused song."""
  357.  
  358.         if not ctx.voice_state.is_playing and ctx.voice_state.voice.is_paused():
  359.             ctx.voice_state.voice.resume()
  360.             await ctx.message.add_reaction('⏯')
  361.  
  362.     @commands.command(name='stop')
  363.     @commands.has_permissions(manage_guild=True)
  364.     async def _stop(self, ctx: commands.Context):
  365.         """Stops playing song and clears the queue."""
  366.  
  367.         ctx.voice_state.songs.clear()
  368.  
  369.         if not ctx.voice_state.is_playing:
  370.             ctx.voice_state.voice.stop()
  371.             await ctx.message.add_reaction('⏹')
  372.  
  373.     @commands.command(name='skip')
  374.     async def _skip(self, ctx: commands.Context):
  375.         """Vote to skip a song. The requester can automatically skip.
  376.         3 skip votes are needed for the song to be skipped.
  377.         """
  378.  
  379.         if not ctx.voice_state.is_playing:
  380.             return await ctx.send('Not playing any music right now...')
  381.  
  382.         voter = ctx.message.author
  383.         if voter == ctx.voice_state.current.requester:
  384.             await ctx.message.add_reaction('⏭')
  385.             ctx.voice_state.skip()
  386.  
  387.         elif voter.id not in ctx.voice_state.skip_votes:
  388.             ctx.voice_state.skip_votes.add(voter.id)
  389.             total_votes = len(ctx.voice_state.skip_votes)
  390.  
  391.             if total_votes >= 3:
  392.                 await ctx.message.add_reaction('⏭')
  393.                 ctx.voice_state.skip()
  394.             else:
  395.                 await ctx.send('Skip vote added, currently at **{}/3**'.format(total_votes))
  396.  
  397.         else:
  398.             await ctx.send('You have already voted to skip this song.')
  399.  
  400.     @commands.command(name='queue')
  401.     async def _queue(self, ctx: commands.Context, *, page: int = 1):
  402.         """Shows the player's queue.
  403.         You can optionally specify the page to show. Each page contains 10 elements.
  404.         """
  405.  
  406.         if len(ctx.voice_state.songs) == 0:
  407.             return await ctx.send('Empty queue.')
  408.  
  409.         items_per_page = 10
  410.         pages = math.ceil(len(ctx.voice_state.songs) / items_per_page)
  411.  
  412.         start = (page - 1) * items_per_page
  413.         end = start + items_per_page
  414.  
  415.         queue = ''
  416.         for i, song in enumerate(ctx.voice_state.songs[start:end], start=start):
  417.             queue += '`{0}.` [**{1.source.title}**]({1.source.url})\n'.format(i + 1, song)
  418.  
  419.         embed = (discord.Embed(description='**{} tracks:**\n\n{}'.format(len(ctx.voice_state.songs), queue))
  420.                  .set_footer(text='Viewing page {}/{}'.format(page, pages)))
  421.         await ctx.send(embed=embed)
  422.  
  423.     @commands.command(name='shuffle')
  424.     async def _shuffle(self, ctx: commands.Context):
  425.         """Shuffles the queue."""
  426.  
  427.         if len(ctx.voice_state.songs) == 0:
  428.             return await ctx.send('Empty queue.')
  429.  
  430.         ctx.voice_state.songs.shuffle()
  431.         await ctx.message.add_reaction('✅')
  432.  
  433.     @commands.command(name='remove')
  434.     async def _remove(self, ctx: commands.Context, index: int):
  435.         """Removes a song from the queue at a given index."""
  436.  
  437.         if len(ctx.voice_state.songs) == 0:
  438.             return await ctx.send('Empty queue.')
  439.  
  440.         ctx.voice_state.songs.remove(index - 1)
  441.         await ctx.message.add_reaction('✅')
  442.  
  443.     @commands.command(name='loop')
  444.     async def _loop(self, ctx: commands.Context):
  445.         """Loops the currently playing song.
  446.         Invoke this command again to unloop the song.
  447.         """
  448.  
  449.         if not ctx.voice_state.is_playing:
  450.             return await ctx.send('Nothing being played at the moment.')
  451.  
  452.         # Inverse boolean value to loop and unloop.
  453.         ctx.voice_state.loop = not ctx.voice_state.loop
  454.         await ctx.message.add_reaction('✅')
  455.  
  456.     @commands.command(name='play')
  457.     async def _play(self, ctx: commands.Context, *, search: str):
  458.         """Plays a song.
  459.         If there are songs in the queue, this will be queued until the
  460.         other songs finished playing.
  461.         This command automatically searches from various sites if no URL is provided.
  462.         A list of these sites can be found here: https://rg3.github.io/youtube-dl/supportedsites.html
  463.         """
  464.  
  465.         if not ctx.voice_state.voice:
  466.             await ctx.invoke(self._join)
  467.  
  468.         async with ctx.typing():
  469.             try:
  470.                 source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop)
  471.             except YTDLError as e:
  472.                 await ctx.send('An error occurred while processing this request: {}'.format(str(e)))
  473.             else:
  474.                 song = Song(source)
  475.  
  476.                 await ctx.voice_state.songs.put(song)
  477.                 await ctx.send('Enqueued {}'.format(str(source)))
  478.  
  479.     @_join.before_invoke
  480.     @_play.before_invoke
  481.     async def ensure_voice_state(self, ctx: commands.Context):
  482.         if not ctx.author.voice or not ctx.author.voice.channel:
  483.             raise commands.CommandError('You are not connected to any voice channel.')
  484.  
  485.         if ctx.voice_client:
  486.             if ctx.voice_client.channel != ctx.author.voice.channel:
  487.                 raise commands.CommandError('Bot is already in a voice channel.')
  488.  
  489.  
  490. bot = commands.Bot('music.', description='Yet another music bot.')
  491. bot.add_cog(Music(bot))
  492.  
  493.  
  494. @bot.event
  495. async def on_ready():
  496.     print('Logged in as:\n{0.user.name}\n{0.user.id}'.format(bot))
  497.  
  498. bot.run('Token')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top