Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import discord
- from discord import colour
- from discord.channel import VoiceChannel
- import youtube_dl
- import ffmpeg
- import pafy
- from discord.ext import commands
- class Player(commands.Cog):
- def __init__(self, bot):
- self.bot=bot
- self.song_queue ={}
- self.setup()
- def setup(self):
- for guild in self.bot.guilds:
- self.song_queue[guild.id]=[]
- async def check_queue(self, ctx):
- if len(self.song_queue[ctx.guild.id])>0:
- ctx.voice_client.stop()
- await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
- self.song_queue[ctx.guild.id].pop(0)
- async def search_song(self, amount, song, get_url=False):
- info=await self.bot.loop.run_in_executor(None, lambda: youtube_dl.YoutubeDL({'format' : "bestaudio",'quiet' :True }).extract_info(f'ytsearch{amount}:{song}', download=False, ie_key='YoutubeSearch'))
- if len(info['entries']) ==0:return None
- return[entry['webpage_url'] for entry in info ['entries']] if get_url else info
- async def play_song(self, ctx, song):
- url = pafy.new(song).getbestaudio().url
- print(url)
- ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(source=url)), after=lambda error:self.bot.loop.create_task(self.check_queue(ctx)))
- ctx.voice_client.source.volume=0.5
- @commands.command()
- async def join(self, ctx):
- if ctx.author is None:
- await ctx.send('Join a Voice Channel')
- if ctx.voice_client is not None:
- return await ctx.voice_client.disconnect()
- await ctx.author.voice.channel.connect()
- @commands.command()
- async def leave(self, ctx):
- if ctx.voice_client is not None:
- return await ctx.voice_client.disconnect()
- @commands.command()
- async def play(self, ctx, *,song=None):
- if song is None:
- await ctx.send('What is Song Bro?')
- if ctx.voice_client is None:
- return await ctx.send('Join Voice Channel nub')
- #when you fail typing in url
- if not ('youtube.com/watch' in song or 'https://youtu.be/' in song ):
- await ctx.send('Please wait while my amazing code searches for the song in Youtube')
- result= await self.search_song(1, song, get_url=True)
- if result is None:
- return await ctx.send('It is not found, use a link or use the search command =)')
- song=result[0]
- if ctx.voice_client.source is not None:
- queue_len=len(self.song_queue[ctx.guild.id])
- if queue_len < 40:
- self.song_queue[ctx.guild.id].append(song)
- return await ctx.send(f'I am currently playing a song, this song has been added to the queue, song position {queue_len+1}')
- else:
- return await ctx.send('I can only queue 10 songs, skip a song or wait till this one is gone ')
- await self.play_song(ctx, song)
- await ctx.send(f'Now Playing {song} requested by {ctx.author}')
- @commands.command()
- async def search(self, ctx, *, song=None):
- if song is None: return await ctx.send('What is the song buddy?')
- await ctx.send('Searching gimme a sec')
- info= await self.search_song(10, song)
- embed=discord.Embed(title=f'Search Results for {song}:', description='Copy the url for the songs if you need a specific song')
- amount=0
- for entry in info['entries']:
- embed.description += f"[{entry['title']}]({entry['webpage_url']})\n"
- amount +=1
- embed.set_footer(text=f"Displaying the first{amount} results")
- await ctx.send(embed=embed)
- @commands.command()
- async def queue(self, ctx):
- if len(self.song_queue[ctx.guild.id])==0:
- return await ctx.send('No Songs in queue')
- embed=discord.Embed(titile='Queue', description='', colour=discord.Colour.dark_gold())
- i=1
- for url in self.song_queue[ctx.guild.id]:
- embed.description+=f'{i}) {url}\n'
- i+=1
- await ctx.send(embed=embed)
- embed.set_footer('EZ')
- @commands.command()
- async def skip(self, ctx):
- if ctx.voice_client is None:
- return await ctx.send('I am meant to skip nothing?')
- if ctx.author.voice is None:
- return await ctx.send("Don't be slick, join a vc first")
- if ctx.author.voice.channel.id !=ctx.voice_client.channel.id:
- return await ctx.send('I am currently not palying any songs for you')
- poll=discord.Embed(title=f'Vote to skip song by {ctx.author.name}#{ctx.author.discriminator}', description=' 3/4 people must agree')
- poll.add_field(name='Skip', value=':white_check_mark:')
- poll.add_field(name='Stay', value=':no_entry_sign:')
- poll.set_footer(text='Cmn you have 15 secs to do this!')
- poll_msg =await ctx.send(embed=poll)
- poll_id=poll_msg.id
- await poll_msg.add_reaction(u'\u2705')
- await poll_msg.add_reaction(u'\U0001F6AB')
- await asyncio.sleep(15)
- poll_msg=await ctx.channel.fetch_message(poll_id)
- votes={u'\u2705':0, u'\U0001F6AB':0}
- reacted=[]
- for reaction in poll_msg.reactions:
- if reaction.emoji in [u'\u2705', '\U0001F6AB']:
- async for user in reaction.users():
- if user.voice.channel.id == ctx.voice_client.channel.id and user.id not in reacted and not user.bot:
- votes[reaction.emoji]+= 1
- reacted.append(user.id)
- skip=False
- if votes[u'\u2705'] > 0:
- if votes[u'\U0001F6AB'] == 0 or (votes[u'\u2705']+votes[u'\U0001F6AB']) > 0.79:
- skip=True
- embed=discord.Embed(title='Skip Succesful',description='Get Skipped')
- if not skip:
- embed=discord.Embed(title='Skip Unsuccessful', description='Well, everyone loves this song')
- await poll_msg.clear_reactions()
- await poll_msg.edit(embed=embed)
- @commands.command()
- async def pause(ctx):
- voice_client = ctx.message.guild.voice_client
- if voice_client.is_playing():
- await voice_client.pause()
- else:
- await ctx.send("The bot is not playing anything at the moment.")
- @commands.command()
- async def resume(ctx):
- voice_client = ctx.message.guild.voice_client
- if voice_client.is_paused():
- await voice_client.resume()
- else:
- await ctx.send("The bot was not playing anything before this. Use play_song command")
- @commands.command()
- async def stop(ctx):
- voice_client = ctx.message.guild.voice_client
- if voice_client.is_playing():
- await voice_client.stop()
- else:
- await ctx.send("The bot is not playing anything at the moment.")
- def setup(bot):
- bot.add_cog(Player(bot))
Add Comment
Please, Sign In to add comment