TimmyTom

Untitled

Jun 26th, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. import asyncio
  2. import discord
  3. from discord import colour
  4. from discord.channel import VoiceChannel
  5. import youtube_dl
  6. import ffmpeg
  7. import pafy
  8. from discord.ext import commands
  9.  
  10.  
  11.  
  12. class Player(commands.Cog):
  13. def __init__(self, bot):
  14. self.bot=bot
  15. self.song_queue ={}
  16.  
  17. self.setup()
  18.  
  19. def setup(self):
  20. for guild in self.bot.guilds:
  21. self.song_queue[guild.id]=[]
  22.  
  23. async def check_queue(self, ctx):
  24. if len(self.song_queue[ctx.guild.id])>0:
  25. ctx.voice_client.stop()
  26. await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
  27. self.song_queue[ctx.guild.id].pop(0)
  28.  
  29.  
  30. async def search_song(self, amount, song, get_url=False):
  31. 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'))
  32. if len(info['entries']) ==0:return None
  33. return[entry['webpage_url'] for entry in info ['entries']] if get_url else info
  34.  
  35. async def play_song(self, ctx, song):
  36. url = pafy.new(song).getbestaudio().url
  37. print(url)
  38. ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(source=url)), after=lambda error:self.bot.loop.create_task(self.check_queue(ctx)))
  39. ctx.voice_client.source.volume=0.5
  40.  
  41. @commands.command()
  42. async def join(self, ctx):
  43. if ctx.author is None:
  44. await ctx.send('Join a Voice Channel')
  45.  
  46. if ctx.voice_client is not None:
  47. return await ctx.voice_client.disconnect()
  48.  
  49.  
  50.  
  51. await ctx.author.voice.channel.connect()
  52.  
  53. @commands.command()
  54. async def leave(self, ctx):
  55. if ctx.voice_client is not None:
  56. return await ctx.voice_client.disconnect()
  57.  
  58.  
  59. @commands.command()
  60. async def play(self, ctx, *,song=None):
  61. if song is None:
  62. await ctx.send('What is Song Bro?')
  63.  
  64. if ctx.voice_client is None:
  65. return await ctx.send('Join Voice Channel nub')
  66.  
  67. #when you fail typing in url
  68.  
  69. if not ('youtube.com/watch' in song or 'https://youtu.be/' in song ):
  70. await ctx.send('Please wait while my amazing code searches for the song in Youtube')
  71.  
  72. result= await self.search_song(1, song, get_url=True)
  73.  
  74. if result is None:
  75. return await ctx.send('It is not found, use a link or use the search command =)')
  76.  
  77. song=result[0]
  78.  
  79.  
  80.  
  81.  
  82. if ctx.voice_client.source is not None:
  83. queue_len=len(self.song_queue[ctx.guild.id])
  84.  
  85. if queue_len < 40:
  86. self.song_queue[ctx.guild.id].append(song)
  87. return await ctx.send(f'I am currently playing a song, this song has been added to the queue, song position {queue_len+1}')
  88.  
  89. else:
  90. return await ctx.send('I can only queue 10 songs, skip a song or wait till this one is gone ')
  91. await self.play_song(ctx, song)
  92. await ctx.send(f'Now Playing {song} requested by {ctx.author}')
  93.  
  94. @commands.command()
  95. async def search(self, ctx, *, song=None):
  96. if song is None: return await ctx.send('What is the song buddy?')
  97. await ctx.send('Searching gimme a sec')
  98. info= await self.search_song(10, song)
  99. embed=discord.Embed(title=f'Search Results for {song}:', description='Copy the url for the songs if you need a specific song')
  100. amount=0
  101. for entry in info['entries']:
  102. embed.description += f"[{entry['title']}]({entry['webpage_url']})\n"
  103. amount +=1
  104.  
  105. embed.set_footer(text=f"Displaying the first{amount} results")
  106. await ctx.send(embed=embed)
  107.  
  108. @commands.command()
  109. async def queue(self, ctx):
  110. if len(self.song_queue[ctx.guild.id])==0:
  111. return await ctx.send('No Songs in queue')
  112. embed=discord.Embed(titile='Queue', description='', colour=discord.Colour.dark_gold())
  113. i=1
  114. for url in self.song_queue[ctx.guild.id]:
  115. embed.description+=f'{i}) {url}\n'
  116.  
  117. i+=1
  118.  
  119.  
  120. await ctx.send(embed=embed)
  121. embed.set_footer('EZ')
  122.  
  123. @commands.command()
  124. async def skip(self, ctx):
  125. if ctx.voice_client is None:
  126. return await ctx.send('I am meant to skip nothing?')
  127.  
  128. if ctx.author.voice is None:
  129. return await ctx.send("Don't be slick, join a vc first")
  130. if ctx.author.voice.channel.id !=ctx.voice_client.channel.id:
  131. return await ctx.send('I am currently not palying any songs for you')
  132.  
  133. poll=discord.Embed(title=f'Vote to skip song by {ctx.author.name}#{ctx.author.discriminator}', description=' 3/4 people must agree')
  134. poll.add_field(name='Skip', value=':white_check_mark:')
  135. poll.add_field(name='Stay', value=':no_entry_sign:')
  136. poll.set_footer(text='Cmn you have 15 secs to do this!')
  137.  
  138. poll_msg =await ctx.send(embed=poll)
  139. poll_id=poll_msg.id
  140.  
  141. await poll_msg.add_reaction(u'\u2705')
  142. await poll_msg.add_reaction(u'\U0001F6AB')
  143. await asyncio.sleep(15)
  144. poll_msg=await ctx.channel.fetch_message(poll_id)
  145.  
  146. votes={u'\u2705':0, u'\U0001F6AB':0}
  147. reacted=[]
  148. for reaction in poll_msg.reactions:
  149. if reaction.emoji in [u'\u2705', '\U0001F6AB']:
  150. async for user in reaction.users():
  151. if user.voice.channel.id == ctx.voice_client.channel.id and user.id not in reacted and not user.bot:
  152. votes[reaction.emoji]+= 1
  153.  
  154. reacted.append(user.id)
  155.  
  156. skip=False
  157.  
  158. if votes[u'\u2705'] > 0:
  159. if votes[u'\U0001F6AB'] == 0 or (votes[u'\u2705']+votes[u'\U0001F6AB']) > 0.79:
  160. skip=True
  161. embed=discord.Embed(title='Skip Succesful',description='Get Skipped')
  162.  
  163. if not skip:
  164. embed=discord.Embed(title='Skip Unsuccessful', description='Well, everyone loves this song')
  165.  
  166.  
  167. await poll_msg.clear_reactions()
  168. await poll_msg.edit(embed=embed)
  169.  
  170. @commands.command()
  171. async def pause(ctx):
  172. voice_client = ctx.message.guild.voice_client
  173. if voice_client.is_playing():
  174. await voice_client.pause()
  175. else:
  176. await ctx.send("The bot is not playing anything at the moment.")
  177.  
  178.  
  179.  
  180. @commands.command()
  181. async def resume(ctx):
  182. voice_client = ctx.message.guild.voice_client
  183. if voice_client.is_paused():
  184. await voice_client.resume()
  185. else:
  186. await ctx.send("The bot was not playing anything before this. Use play_song command")
  187.  
  188. @commands.command()
  189. async def stop(ctx):
  190. voice_client = ctx.message.guild.voice_client
  191. if voice_client.is_playing():
  192. await voice_client.stop()
  193. else:
  194. await ctx.send("The bot is not playing anything at the moment.")
  195.  
  196. def setup(bot):
  197. bot.add_cog(Player(bot))
Add Comment
Please, Sign In to add comment