-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.py
145 lines (102 loc) · 5.3 KB
/
music.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Original file https://github.com/Devoxin/Lavalink.py/blob/master/examples/music.py
import lavalink
from discord.ext import commands
import discord
import re
import os
import subprocess
import urllib.request
url_rx = re.compile(r'https?://(?:www\.)?.+')
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
dirname = os.path.dirname(__file__)
lavalink_folder = os.path.join(dirname, 'lavalink')
lavalink_server = os.path.join(lavalink_folder, 'Lavalink.jar')
if not os.path.isfile(lavalink_server):
urllib.request.urlretrieve('https://ci.fredboat.com/repository/download/Lavalink_Build/.lastSuccessful/Lavalink.jar?guest=1', lavalink_server)
subprocess.Popen(['java', '-jar', lavalink_server], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=lavalink_folder)
if not hasattr(bot, 'lavalink'):
bot.lavalink = lavalink.Client(bot.user.id)
bot.lavalink.add_node('127.0.0.1', 2333, 'youshallnotpass', 'eu', 'default-node', reconnect_attempts=-1)
bot.add_listener(bot.lavalink.voice_update_handler, 'on_socket_response')
lavalink.add_event_hook(self.track_hook)
def cog_unload(self):
self.bot.lavalink._event_hooks.clear()
async def cog_before_invoke(self, ctx):
guild_check = ctx.guild is not None
if guild_check:
await self.ensure_voice(ctx)
return guild_check
async def cog_command_error(self, ctx, error):
if isinstance(error, commands.CommandInvokeError):
await ctx.send(error.original)
async def ensure_voice(self, ctx):
player = self.bot.lavalink.player_manager.create(ctx.guild.id, endpoint=str(ctx.guild.region))
should_connect = ctx.command.name in ('play',)
if not ctx.author.voice or not ctx.author.voice.channel:
raise commands.CommandInvokeError('Únete a un canal de voz primero.')
if not player.is_connected:
if not should_connect:
raise commands.CommandInvokeError('No conectado.')
permissions = ctx.author.voice.channel.permissions_for(ctx.me)
if not permissions.connect or not permissions.speak:
raise commands.CommandInvokeError('Necesito permisos para conectarme y hablar en este canal.')
player.store('channel', ctx.channel.id)
await self.connect_to(ctx.guild.id, str(ctx.author.voice.channel.id))
else:
if int(player.channel_id) != ctx.author.voice.channel.id:
raise commands.CommandInvokeError('Necesitas estar en mi canal de voz.')
async def track_hook(self, event):
if isinstance(event, lavalink.events.QueueEndEvent):
guild_id = int(event.player.guild_id)
await self.connect_to(guild_id, None)
async def connect_to(self, guild_id: int, channel_id: str):
ws = self.bot._connection._get_websocket(guild_id)
await ws.voice_state(str(guild_id), channel_id)
@commands.command(aliases=['p'])
async def play(self, ctx, *, query: str):
player = self.bot.lavalink.player_manager.get(ctx.guild.id)
query = query.strip('<>')
if not url_rx.match(query):
query = f'ytsearch:{query}'
results = await player.node.get_tracks(query)
if not results or not results['tracks']:
return await ctx.send('No hay resultados!')
embed = discord.Embed(color=discord.Color.blue())
if results['loadType'] == 'PLAYLIST_LOADED':
tracks = results['tracks']
for track in tracks:
player.add(requester=ctx.author.id, track=track)
embed.title = 'Playlist añadida!'
embed.description = f'{results["playlistInfo"]["name"]} - {len(tracks)} tracks'
else:
track = results['tracks'][0]
embed.title = 'Canción añadida'
embed.description = f'[{track["info"]["title"]}]({track["info"]["uri"]})'
track = lavalink.models.AudioTrack(track, ctx.author.id, recommended=True)
player.add(requester=ctx.author.id, track=track)
await ctx.send(embed=embed)
if not player.is_playing:
await player.play()
@commands.command(aliases=['dc'])
async def disconnect(self, ctx):
player = self.bot.lavalink.player_manager.get(ctx.guild.id)
if not player.is_connected:
return await ctx.send('No conectado')
if not ctx.author.voice or (player.is_connected and ctx.author.voice.channel.id != int(player.channel_id)):
return await ctx.send('No estás en mi canal de voz!')
player.queue.clear()
await player.stop()
await self.connect_to(ctx.guild.id, None)
@commands.command(aliases=['q'])
async def queue(self, ctx):
player = self.bot.lavalink.player_manager.get(ctx.guild.id)
queue_elements = ''
queue_elements += f'1) {player.current.title} - {player.position_timestamp} segundos transcurridos\n'
for idx, track in enumerate(player.queue, 2):
queue_elements += f'{idx}) {track.title}\n'
queue_elements = f'```{queue_elements}```'
await ctx.send(queue_elements)
def setup(bot):
bot.add_cog(Music(bot))