-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontext_menus.py
More file actions
335 lines (318 loc) · 14.3 KB
/
context_menus.py
File metadata and controls
335 lines (318 loc) · 14.3 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from __future__ import annotations
import re
import shlex
import discord
from discord.ext.commands import HybridCommand
from redbot.core.i18n import Translator, cog_i18n
from pylav.constants.regex import SOURCE_INPUT_MATCH_MERGED
from pylav.extension.red.utils.decorators import is_dj_logic
from pylav.extension.red.utils.validators import valid_query_attachment
from pylav.players.query.obj import Query
from pylav.type_hints.bot import DISCORD_COG_TYPE_MIXIN, DISCORD_INTERACTION_TYPE
_ = Translator("PyLavPlayer", __file__)
YOUTUBE_MUSIC_ACTIVITY = re.compile(r"i\.ytimg\.com/vi/([A-Za-z0-9_\-]{11}).*\.jpg", re.IGNORECASE)
@cog_i18n(_)
class ContextMenus(DISCORD_COG_TYPE_MIXIN):
command_play: HybridCommand
async def _context_message_play(self, interaction: DISCORD_INTERACTION_TYPE, message: discord.Message) -> None:
await interaction.response.defer(ephemeral=True)
if not interaction.guild:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_("I can not play songs in direct messages. Sorry, try again on a server."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
is_dj = await is_dj_logic(interaction)
if not is_dj:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_("You need to be a disc jockey in this server to play tracks in this server."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
if player := self.pylav.get_player(interaction.guild.id):
config = player.config
else:
config = self.pylav.player_config_manager.get_config(interaction.guild.id)
if (channel_id := await config.fetch_text_channel_id()) != 0 and channel_id != interaction.channel.id:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
messageable=interaction,
description=_(
"This command is unavailable in this channel. Please use {channel_name_variable_do_not_translate} instead."
).format(
channel_name_variable_do_not_translate=(
channel.mention
if (channel := interaction.guild.get_channel_or_thread(channel_id))
else channel_id
)
),
),
ephemeral=True,
wait=True,
)
return
content = await self._reconstruct_msg_content(message)
try:
content_parts = shlex.split(content)
except ValueError:
content_parts = content.split()
valid_matches = set()
for part in content_parts:
part = part.strip()
for __ in SOURCE_INPUT_MATCH_MERGED.finditer(part):
valid_matches.add(part)
for attachment in message.attachments:
if valid_query_attachment(attachment.filename):
valid_matches.add(attachment.url)
if not valid_matches:
message = _("I could not find any suitable matches in this message.")
elif len(valid_matches) > 1:
message = _("I found many suitable matches in this message.")
else:
message = _("I found a single suitable match in this message.")
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=message,
messageable=interaction,
),
ephemeral=True,
wait=True,
)
await self.command_play.callback(self, interaction, query="\n".join(valid_matches)) # type: ignore
@staticmethod
async def _reconstruct_msg_content(message):
content = message.content.strip()
for embed in message.embeds:
if embed.description:
content += f" {embed.description}"
if embed.title:
content += f" {embed.title}"
if embed.url and valid_query_attachment(embed.url):
content += f" {embed.url}"
if embed.footer:
if embed.footer.text:
content += f" {embed.footer.text}"
if embed.footer.icon_url and valid_query_attachment(embed.footer.icon_url):
content += f" {embed.footer.icon_url}"
if (
hasattr(embed, "video")
and embed.video
and embed.video.url
and ("www.youtube.com" not in embed.video.url or "/embed/" not in embed.video.url)
):
content += f" {embed.video.url}"
for field in embed.fields:
if field.value:
content += f" {field.value}"
if field.name:
content += f" {field.name}"
return content
async def _context_user_play(
self, interaction: DISCORD_INTERACTION_TYPE, member: discord.Member
) -> None: # sourcery skip: low-code-quality
# sourcery no-metrics
await interaction.response.defer(ephemeral=True)
if not interaction.guild:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_("I can not play songs in direct messages. Sorry, try again on a server."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
is_dj = await is_dj_logic(interaction)
if not is_dj:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_("You need to be a disc jockey to play tracks in this server."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
if player := self.pylav.get_player(interaction.guild.id):
config = player.config
else:
config = self.pylav.player_config_manager.get_config(interaction.guild.id)
if (channel_id := await config.fetch_text_channel_id()) != 0 and channel_id != interaction.channel.id:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
messageable=interaction,
description=_(
"This command is unavailable in this channel. Please use {channel_name_variable_do_not_translate} instead."
).format(
channel_name_variable_do_not_translate=(
channel.mention
if (channel := interaction.guild.get_channel_or_thread(channel_id))
else channel_id
)
),
),
ephemeral=True,
wait=True,
)
return
# The member returned by this interaction doesn't have any activities.
member = interaction.guild.get_member(member.id)
spotify_activity = next((a for a in member.activities if isinstance(a, discord.Spotify)), None)
apple_music_activity = next(
(a for a in member.activities if a.name in ["Apple Music", "Cider", "Cider-2", "AppleMusic"]), None
)
youtube_music_activity = next((a for a in member.activities if a.name in ["YouTube Music"]), None)
if not any([spotify_activity, apple_music_activity, youtube_music_activity]):
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_(
"I could not find any supported activity in the activities {user_name_variable_do_not_translate} is partaking."
).format(user_name_variable_do_not_translate=member.mention),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
if spotify_activity and spotify_activity.track_id:
await self.command_play.callback(
self,
interaction,
query=f"https://open.spotify.com/track/{spotify_activity.track_id}",
)
elif apple_music_activity:
assert isinstance(apple_music_activity, discord.activity.Activity)
album = apple_music_activity.assets.get("large_text")
artist = apple_music_activity.state
track = apple_music_activity.details
# TODO: This should use the button URL instead of a search for 100% accuracy
# Currently discord.py doesnt provide the button URLs, the day they do this can be updated
search_string = "amsearch:" if (album or artist or track) else ""
if album:
search_string += f"{album} "
if artist:
search_string += f"{artist} "
if track:
search_string += f"{track}"
if not search_string:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_(
"I could not find a valid Apple Music track in the activity "
"{user_name_variable_do_not_translate} is partaking in."
).format(user_name_variable_do_not_translate=member.mention),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
response = await self.pylav.get_tracks(
await Query.from_string(search_string),
player=interaction.client.pylav.get_player(interaction.guild.id),
)
match response.loadType:
case "track":
tracks = [response.data]
case "search":
tracks = response.data
case "playlist":
tracks = response.data.tracks
case __:
tracks = []
if not tracks:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_("I could not find any tracks matching {query_variable_do_not_translate}.").format(
query_variable_do_not_translate=search_string
),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
await self.command_play.callback(
self,
interaction,
query=tracks[0],
)
elif youtube_music_activity and youtube_music_activity.application_id == 1143202598460076053:
assert isinstance(youtube_music_activity, discord.activity.Activity)
if image_url := youtube_music_activity.assets.get("large_image"):
track_id_match = YOUTUBE_MUSIC_ACTIVITY.search(image_url)
if track_id_match and track_id_match.group(1):
await self.command_play.callback(
self,
interaction,
query=f"https://music.youtube.com/watch?v={track_id_match.group(1)}",
)
return
track = apple_music_activity.details
# TODO: This should use the button URL instead of a search for 100% accuracy
# Currently discord.py doesnt provide the button URLs, the day they do this can be updated
search_string = "ytmsearch:" if (track) else ""
if track:
search_string += f"{track}"
if not search_string:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_(
"I could not find a valid YouTube Music track in the activity "
"{user_name_variable_do_not_translate} is partaking in."
).format(user_name_variable_do_not_translate=member.mention),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
response = await self.pylav.get_tracks(
await Query.from_string(search_string),
player=interaction.client.pylav.get_player(interaction.guild.id),
)
match response.loadType:
case "track":
tracks = [response.data]
case "search":
tracks = response.data
case "playlist":
tracks = response.data.tracks
case __:
tracks = []
if not tracks:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_("I could not find any tracks matching {query_variable_do_not_translate}.").format(
query_variable_do_not_translate=search_string
),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
return
await self.command_play.callback(
self,
interaction,
query=tracks[0],
)
else:
await interaction.followup.send(
embed=await self.pylav.construct_embed(
description=_(
"I could not figure out what {user_name_variable_do_not_translate} is listening to."
).format(user_name_variable_do_not_translate=member.mention),
messageable=interaction,
),
ephemeral=True,
wait=True,
)