Skip to content

Commit 6758c1b

Browse files
author
Mg Grignard
committed
[Audio] Merge cases to avoid copy/pasted code in [p]play and [p]bumpplay
1 parent bf7bc1e commit 6758c1b

File tree

3 files changed

+36
-94
lines changed

3 files changed

+36
-94
lines changed

redbot/cogs/audio/apis/interface.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -624,39 +624,24 @@ async def spotify_enqueue(
624624
if enqueue:
625625
if len(player.queue) >= 10000:
626626
continue
627-
if guild_data["maxlength"] > 0:
628-
if self.cog.is_track_length_allowed(single_track, guild_data["maxlength"]):
629-
enqueued_tracks += 1
630-
single_track.extras.update(
631-
{
632-
"enqueue_time": int(time.time()),
633-
"vc": player.channel.id,
634-
"requester": ctx.author.id,
635-
}
636-
)
637-
player.add(ctx.author, single_track)
638-
self.bot.dispatch(
639-
"red_audio_track_enqueue",
640-
player.guild,
641-
single_track,
642-
ctx.author,
643-
)
644-
else:
645-
enqueued_tracks += 1
646-
single_track.extras.update(
647-
{
648-
"enqueue_time": int(time.time()),
649-
"vc": player.channel.id,
650-
"requester": ctx.author.id,
651-
}
652-
)
653-
player.add(ctx.author, single_track)
654-
self.bot.dispatch(
655-
"red_audio_track_enqueue",
656-
player.guild,
657-
single_track,
658-
ctx.author,
659-
)
627+
if guild_data["maxlength"] > 0 and not self.cog.is_track_length_allowed(single_track, guild_data["maxlength"]):
628+
continue
629+
630+
enqueued_tracks += 1
631+
single_track.extras.update(
632+
{
633+
"enqueue_time": int(time.time()),
634+
"vc": player.channel.id,
635+
"requester": ctx.author.id,
636+
}
637+
)
638+
player.add(ctx.author, single_track)
639+
self.bot.dispatch(
640+
"red_audio_track_enqueue",
641+
player.guild,
642+
single_track,
643+
ctx.author,
644+
)
660645

661646
if not player.current:
662647
await player.play()

redbot/cogs/audio/core/commands/player.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -295,29 +295,15 @@ async def command_bumpplay(
295295
title=_("Unable To Play Tracks"),
296296
description=_("This track is not allowed in this server."),
297297
)
298-
elif guild_data["maxlength"] > 0:
299-
if self.is_track_length_allowed(single_track, guild_data["maxlength"]):
300-
single_track.requester = ctx.author
301-
single_track.extras.update(
302-
{
303-
"enqueue_time": int(time.time()),
304-
"vc": player.channel.id,
305-
"requester": ctx.author.id,
306-
}
307-
)
308-
player.queue.insert(0, single_track)
309-
player.maybe_shuffle()
310-
self.bot.dispatch(
311-
"red_audio_track_enqueue", player.guild, single_track, ctx.author
312-
)
313-
else:
314-
self.update_player_lock(ctx, False)
315-
return await self.send_embed_msg(
316-
ctx,
317-
title=_("Unable To Play Tracks"),
318-
description=_("Track exceeds maximum length."),
319-
)
320-
298+
elif guild_data["maxlength"] > 0 and not self.is_track_length_allowed(
299+
single_track, guild_data["maxlength"]
300+
):
301+
self.update_player_lock(ctx, False)
302+
return await self.send_embed_msg(
303+
ctx,
304+
title=_("Unable To Play Tracks"),
305+
description=_("Track exceeds maximum length."),
306+
)
321307
else:
322308
single_track.requester = ctx.author
323309
single_track.extras["bumped"] = True

redbot/cogs/audio/core/utilities/player.py

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -455,21 +455,10 @@ async def _enqueue_tracks(
455455
):
456456
log.debug("Query is not allowed in %r (%s)", ctx.guild.name, ctx.guild.id)
457457
continue
458-
elif guild_data["maxlength"] > 0:
459-
if self.is_track_length_allowed(track, guild_data["maxlength"]):
460-
track_len += 1
461-
track.extras.update(
462-
{
463-
"enqueue_time": int(time.time()),
464-
"vc": player.channel.id,
465-
"requester": ctx.author.id,
466-
}
467-
)
468-
player.add(ctx.author, track)
469-
self.bot.dispatch(
470-
"red_audio_track_enqueue", player.guild, track, ctx.author
471-
)
472-
458+
elif guild_data["maxlength"] > 0 and not self.is_track_length_allowed(
459+
track, guild_data["maxlength"]
460+
):
461+
continue
473462
else:
474463
track_len += 1
475464
track.extras.update(
@@ -548,29 +537,11 @@ async def _enqueue_tracks(
548537
return await self.send_embed_msg(
549538
ctx, title=_("This track is not allowed in this server.")
550539
)
551-
elif guild_data["maxlength"] > 0:
552-
if self.is_track_length_allowed(single_track, guild_data["maxlength"]):
553-
single_track.extras.update(
554-
{
555-
"enqueue_time": int(time.time()),
556-
"vc": player.channel.id,
557-
"requester": ctx.author.id,
558-
}
559-
)
560-
player.add(ctx.author, single_track)
561-
player.maybe_shuffle()
562-
self.bot.dispatch(
563-
"red_audio_track_enqueue",
564-
player.guild,
565-
single_track,
566-
ctx.author,
567-
)
568-
else:
569-
self.update_player_lock(ctx, False)
570-
return await self.send_embed_msg(
571-
ctx, title=_("Track exceeds maximum length.")
572-
)
573-
540+
elif guild_data["maxlength"] > 0 and not self.is_track_length_allowed(
541+
single_track, guild_data["maxlength"]
542+
):
543+
self.update_player_lock(ctx, False)
544+
return await self.send_embed_msg(ctx, title=_("Track exceeds maximum length."))
574545
else:
575546
single_track.extras.update(
576547
{

0 commit comments

Comments
 (0)