Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.09 KB | None | 0 0
  1. """
  2. This is an example cog that shows how you would make use of Lavalink.py.
  3. This example cog requires that you have python 3.6 or higher due to the f-strings.
  4. """
  5. import math
  6. import re
  7.  
  8. import discord
  9. import lavalink
  10. from discord.ext import commands
  11.  
  12. url_rx = re.compile('https?:\\/\\/(?:www\\.)?.+') # noqa: W605
  13.  
  14.  
  15. class musica(commands.Cog):
  16. def __init__(self, client):
  17. self.client = client
  18.  
  19. if not hasattr(client, 'lavalink'): # This ensures the client isn't overwritten during cog reloads.
  20. client.lavalink = lavalink.Client(client.user.id)
  21. client.lavalink.add_node('luiz13lavalink.glitch.me', 80, 'youshallnotpass', 'us', 'default-node') # Host, Port, Password, Region, Name
  22. client.add_listener(client.lavalink.voice_update_handler, 'on_socket_response')
  23.  
  24. client.lavalink.add_event_hook(self.track_hook)
  25.  
  26. def cog_unload(self):
  27. self.client.lavalink._event_hooks.clear()
  28.  
  29. async def cog_before_invoke(self, ctx):
  30. guild_check = ctx.guild is not None
  31. # This is essentially the same as `@commands.guild_only()`
  32. # except it saves us repeating ourselves (and also a few lines).
  33.  
  34. if guild_check:
  35. await self.ensure_voice(ctx)
  36. # Ensure that the bot and command author share a mutual voicechannel.
  37.  
  38. return guild_check
  39.  
  40. async def cog_command_error(self, ctx, error):
  41. if isinstance(error, commands.CommandInvokeError):
  42. await ctx.send(error.original)
  43. # The above handles errors thrown in this cog and shows them to the user.
  44. # This shouldn't be a problem as the only errors thrown in this cog are from `ensure_voice`
  45. # which contain a reason string, such as "Join a voicechannel" etc. You can modify the above
  46. # if you want to do things differently.
  47.  
  48. async def track_hook(self, event):
  49. if isinstance(event, lavalink.events.QueueEndEvent):
  50. guild_id = int(event.player.guild_id)
  51. await self.connect_to(guild_id, None)
  52. # Disconnect from the channel -- there's nothing else to play.
  53.  
  54. async def connect_to(self, guild_id: int, channel_id: str):
  55. """ Connects to the given voicechannel ID. A channel_id of `None` means disconnect. """
  56. ws = self.client._connection._get_websocket(guild_id)
  57. await ws.voice_state(str(guild_id), channel_id)
  58. # The above looks dirty, we could alternatively use `bot.shards[shard_id].ws` but that assumes
  59. # the bot instance is an AutoShardedBot.
  60.  
  61. @commands.command(aliases=['p'])
  62. async def play(self, ctx, *, query: str):
  63. """ Searches and plays a song from a given query. """
  64. player = self.client.lavalink.players.get(ctx.guild.id)
  65.  
  66. query = query.strip('<>')
  67.  
  68. if not url_rx.match(query):
  69. query = f'ytsearch:{query}'
  70.  
  71. results = await player.node.get_tracks(query)
  72.  
  73. if not results or not results['tracks']:
  74. return await ctx.send('Nada encontrado!')
  75.  
  76. embed = discord.Embed(color=discord.Color.blurple())
  77.  
  78. if results['loadType'] == 'PLAYLIST_LOADED':
  79. tracks = results['tracks']
  80.  
  81. for track in tracks:
  82. player.add(requester=ctx.author.id, track=track)
  83.  
  84. embed.title = 'Lista de reprodução adicionada!'
  85. embed.description = f'{results["playlistInfo"]["name"]} - {len(tracks)} tracks'
  86. else:
  87. track = results['tracks'][0]
  88. embed.title = 'Música adicionada'
  89. embed.description = f'[{track["info"]["title"]}]({track["info"]["uri"]})'
  90. player.add(requester=ctx.author.id, track=track)
  91.  
  92. await ctx.send(embed=embed)
  93.  
  94. if not player.is_playing:
  95. await player.play()
  96.  
  97. @commands.command()
  98. async def seek(self, ctx, *, seconds: int):
  99. """ Seeks to a given position in a track. """
  100. player = self.client.lavalink.players.get(ctx.guild.id)
  101.  
  102. track_time = player.position + (seconds * 1000)
  103. await player.seek(track_time)
  104.  
  105. await ctx.send(f'Faixa movida para **{lavalink.utils.format_time(track_time)}**')
  106.  
  107. @commands.command(aliases=['forceskip'])
  108. async def skip(self, ctx):
  109. """ Skips the current track. """
  110. player = self.client.lavalink.players.get(ctx.guild.id)
  111.  
  112. if not player.is_playing:
  113. return await ctx.send('Nada esta tocando no momento.')
  114.  
  115. await player.skip()
  116. await ctx.send('⏭ | Música pulada.')
  117.  
  118. @commands.command()
  119. async def stop(self, ctx):
  120. """ Stops the player and clears its queue. """
  121. player = self.client.lavalink.players.get(ctx.guild.id)
  122.  
  123. if not player.is_playing:
  124. return await ctx.send('Nada esta tocando no momento.')
  125.  
  126. player.queue.clear()
  127. await player.stop()
  128. await ctx.send('⏹ | Parei todas as músicas.')
  129.  
  130. @commands.command(aliases=['np', 'n', 'playing'])
  131. async def now(self, ctx):
  132. """ Shows some stats about the currently playing song. """
  133. player = self.client.lavalink.players.get(ctx.guild.id)
  134.  
  135. if not player.current:
  136. return await ctx.send('Nada está tocando no momento.')
  137.  
  138. position = lavalink.utils.format_time(player.position)
  139. if player.current.stream:
  140. duration = '🔴 LIVE'
  141. else:
  142. duration = lavalink.utils.format_time(player.current.duration)
  143. song = f'**[{player.current.title}]({player.current.uri})**\n({position}/{duration})'
  144.  
  145. embed = discord.Embed(color=discord.Color.blurple(),
  146. title='Agora tocando', description=song)
  147. await ctx.send(embed=embed)
  148.  
  149. @commands.command(aliases=['q'])
  150. async def queue(self, ctx, page: int = 1):
  151. """ Shows the player's queue. """
  152. player = self.client.lavalink.players.get(ctx.guild.id)
  153.  
  154. if not player.queue:
  155. return await ctx.send('Não a mais músicas na lista.')
  156.  
  157. items_per_page = 10
  158. pages = math.ceil(len(player.queue) / items_per_page)
  159.  
  160. start = (page - 1) * items_per_page
  161. end = start + items_per_page
  162.  
  163. queue_list = ''
  164. for index, track in enumerate(player.queue[start:end], start=start):
  165. queue_list += f'`{index + 1}.` [**{track.title}**]({track.uri})\n'
  166.  
  167. embed = discord.Embed(colour=discord.Color.blurple(),
  168. description=f'**{len(player.queue)} tracks**\n\n{queue_list}')
  169. embed.set_footer(text=f'Olhando página {page}/{pages}')
  170. await ctx.send(embed=embed)
  171.  
  172. @commands.command(aliases=['resume'])
  173. async def pause(self, ctx):
  174. """ Pauses/Resumes the current track. """
  175. player = self.client.lavalink.players.get(ctx.guild.id)
  176.  
  177. if not player.is_playing:
  178. return await ctx.send('Nada está tocando no momento.')
  179.  
  180. if player.paused:
  181. await player.set_pause(False)
  182. await ctx.send('⏯ | Música resumida')
  183. else:
  184. await player.set_pause(True)
  185. await ctx.send('⏯ | Música pausada')
  186.  
  187. @commands.command(aliases=['vol'])
  188. async def volume(self, ctx, volume: int = None):
  189. """ Changes the player's volume (0-1000). """
  190. player = self.client.lavalink.players.get(ctx.guild.id)
  191.  
  192. if not volume:
  193. return await ctx.send(f'🔈 | {player.volume}%')
  194.  
  195. await player.set_volume(volume) # Lavalink will automatically cap values between, or equal to 0-1000.
  196. await ctx.send(f'🔈 | Volume setado para {player.volume}%')
  197.  
  198. @commands.command()
  199. async def shuffle(self, ctx):
  200. """ Shuffles the player's queue. """
  201. player = self.client.lavalink.players.get(ctx.guild.id)
  202. if not player.is_playing:
  203. return await ctx.send('Nada está tocando no momento.')
  204.  
  205. player.shuffle = not player.shuffle
  206. await ctx.send('🔀 | Modo aleatório' + ('enabled' if player.shuffle else 'disabled'))
  207.  
  208. @commands.command(aliases=['loop'])
  209. async def repeat(self, ctx):
  210. """ Repeats the current song until the command is invoked again. """
  211. player = self.client.lavalink.players.get(ctx.guild.id)
  212.  
  213. if not player.is_playing:
  214. return await ctx.send('Nada está tocando no momento.')
  215.  
  216. player.repeat = not player.repeat
  217. await ctx.send('🔁 | Modo de repetição' + ('enabled' if player.repeat else 'disabled'))
  218.  
  219. @commands.command()
  220. async def remove(self, ctx, index: int):
  221. """ Removes an item from the player's queue with the given index. """
  222. player = self.client.lavalink.players.get(ctx.guild.id)
  223.  
  224. if not player.queue:
  225. return await ctx.send('Nada está tocando no momento.')
  226.  
  227. if index > len(player.queue) or index < 1:
  228. return await ctx.send(f'Index has to be **between** 1 and {len(player.queue)}')
  229.  
  230. removed = player.queue.pop(index - 1) # Account for 0-index.
  231.  
  232. await ctx.send(f'Removido**{removed.title}** da lista.')
  233.  
  234. @commands.command()
  235. async def find(self, ctx, *, query):
  236. """ Lists the first 10 search results from a given query. """
  237. player = self.client.lavalink.players.get(ctx.guild.id)
  238.  
  239. if not query.startswith('ytsearch:') and not query.startswith('scsearch:'):
  240. query = 'ytsearch:' + query
  241.  
  242. results = await player.node.get_tracks(query)
  243.  
  244. if not results or not results['tracks']:
  245. return await ctx.send('Nada foi encontrado')
  246.  
  247. tracks = results['tracks'][:10] # First 10 results
  248.  
  249. o = ''
  250. for index, track in enumerate(tracks, start=1):
  251. track_title = track['info']['title']
  252. track_uri = track['info']['uri']
  253. o += f'`{index}.` [{track_title}]({track_uri})\n'
  254.  
  255. embed = discord.Embed(color=discord.Color.blurple(), description=o)
  256. await ctx.send(embed=embed)
  257.  
  258. @commands.command(aliases=['dc'])
  259. async def disconnect(self, ctx):
  260. """ Disconnects the player from the voice channel and clears its queue. """
  261. player = self.client.lavalink.players.get(ctx.guild.id)
  262.  
  263. if not player.is_connected:
  264. return await ctx.send('Não estou conectado a nenhum canal de voz.')
  265.  
  266. if not ctx.author.voice or (player.is_connected and ctx.author.voice.channel.id != int(player.channel_id)):
  267. return await ctx.send('Você não esta em meu canal de voz!')
  268.  
  269. player.queue.clear()
  270. await player.stop()
  271. await self.connect_to(ctx.guild.id, None)
  272. await ctx.send('*⃣ | Desconectei do canal de voz.')
  273.  
  274. async def ensure_voice(self, ctx):
  275. """ This check ensures that the bot and command author are in the same voicechannel. """
  276. player = self.client.lavalink.players.create(ctx.guild.id, endpoint=str(ctx.guild.region))
  277. # Create returns a player if one exists, otherwise creates.
  278.  
  279. should_connect = ctx.command.name in ('play') # Add commands that require joining voice to work.
  280.  
  281. if not ctx.author.voice or not ctx.author.voice.channel:
  282. raise commands.CommandInvokeError('Entre em um canal de voz primeiro.')
  283.  
  284. if not player.is_connected:
  285. if not should_connect:
  286. raise commands.CommandInvokeError('Você não esta em um canal de voz.')
  287.  
  288. permissions = ctx.author.voice.channel.permissions_for(ctx.me)
  289.  
  290. if not permissions.connect or not permissions.speak: # Check user limit too?
  291. raise commands.CommandInvokeError('Eu preciso da permissão`CONNECT` é `SPEAK` permissões.')
  292.  
  293. player.store('channel', ctx.channel.id)
  294. await self.connect_to(ctx.guild.id, str(ctx.author.voice.channel.id))
  295. else:
  296. if int(player.channel_id) != ctx.author.voice.channel.id:
  297. raise commands.CommandInvokeError('Você precisa estar no mesmo canal de voz que eu.')
  298.  
  299.  
  300. def setup(client):
  301. client.add_cog(musica(client))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement