Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up adding tracks from playlist (Sourcery refactored) #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Jan 3, 2023

Pull Request #20 refactored by Sourcery.

Since the original Pull Request was opened as a fork in a contributor's
repository, we are unable to create a Pull Request branching from it.

To incorporate these changes, you can either:

  1. Merge this Pull Request instead of the original, or

  2. Ask your contributor to locally incorporate these commits and push them to
    the original Pull Request

    Incorporate changes via command line
    git fetch https://github.com/Darkempire78/Music-Discord-Bot pull/20/head
    git merge --ff-only FETCH_HEAD
    git push

NOTE: As code is pushed to the original Pull Request, Sourcery will
re-run and update (force-push) this Pull Request with new refactorings as
necessary. If Sourcery finds no refactorings at any point, this Pull Request
will be closed automatically.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Help us improve this pull request!

nznaza and others added 2 commits January 3, 2023 00:19
…peeding up adding tracks from a playlist, changed youtube playlist looup function to avoid searching a known playlist url
Comment on lines -32 to +42
return await ctx.send(f'{ctx.author.mention} This command has a cooldown, be sure to wait for '+str(jour)+ "day(s)")
return await ctx.send(
f'{ctx.author.mention} This command has a cooldown, be sure to wait for {str(jour)}day(s)'
)
if heure > 0:
return await ctx.send(f'{ctx.author.mention} This command has a cooldown, be sure to wait for '+str(heure)+ " hour(s)")
return await ctx.send(
f'{ctx.author.mention} This command has a cooldown, be sure to wait for {str(heure)} hour(s)'
)
if minute > 0:
return await ctx.send(f'{ctx.author.mention} This command has a cooldown, be sure to wait for '+ str(minute)+" minute(s)")
return await ctx.send(
f'{ctx.author.mention} This command has a cooldown, be sure to wait for {str(minute)} minute(s)'
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EventsCog.on_command_error refactored with the following changes:

Comment on lines -75 to +105
if (before.channel is not None) and (after.channel is None):
if member == self.bot.user:

player = self.bot.wavelink.get_player(before.channel.guild.id)
if player.is_playing:
await player.destroy()
DBQueue(self.bot.dbConnection).clear(before.channel.guild.id)
DBServer(self.bot.dbConnection).clearMusicParameters(before.channel.guild.id, False, False)
if (
(before.channel is not None)
and (after.channel is None)
and member == self.bot.user
):
player = self.bot.wavelink.get_player(before.channel.guild.id)
if player.is_playing:
await player.destroy()
DBQueue(self.bot.dbConnection).clear(before.channel.guild.id)
DBServer(self.bot.dbConnection).clearMusicParameters(before.channel.guild.id, False, False)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EventsCog.on_voice_state_update refactored with the following changes:

if not trackLinks: # if len(trackLinks) == 0:
return None
return trackLinks
return trackLinks or None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function searchSpotifyPlaylist refactored with the following changes:

This removes the following comments ( why? ):

# if len(trackLinks) == 0:

Comment on lines -76 to +77
if link is None:
return None
return link
return None if link is None else link
if "playlist" in response._real_url.path:
links = await searchDeezerPlaylist(self, ctx, session, response)
if links is None:
return None
return links
return None if links is None else links
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function searchDeezer refactored with the following changes:

Comment on lines -120 to +114
if not trackLinks:
return None
return trackLinks
return trackLinks or None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function searchDeezerPlaylist refactored with the following changes:

@@ -165,21 +157,27 @@ def check(message):
messageContent = int(message.content)
if ((messageContent >= 0) and (messageContent <= 5)):
return message.content

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function searchQuery refactored with the following changes:

Comment on lines -24 to +33

if not await Check().userInVoiceChannel(ctx, self.bot): return
if not await Check().botInVoiceChannel(ctx, self.bot): return
if not await Check().userInVoiceChannel(ctx, self.bot): return
if not await Check().botInVoiceChannel(ctx, self.bot): return
if not await Check().userAndBotInSameVoiceChannel(ctx, self.bot): return

if not ctx.author.guild_permissions.administrator:

users = DBSkip(self.bot.dbConnection).displayUsers(ctx.guild.id)
usersCount = len(users)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CogSkip.skip refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

Comment on lines -68 to +70
queueSize = 0
queueDuration = "00:00"

embed=discord.Embed(title="Song added in the queue", description=f"New song added : **[{trackTitle}]({track.uri})** ({trackDuration})", color=discord.Colour.random())
embed.add_field(name="Place in the queue : ", value=f"`{queueSize}`", inline=True)
embed.add_field(name="Estimated time before playing :", value=f"`{queueDuration}`", inline=True)
embed.set_thumbnail(url=track.thumb)
await ctx.channel.send(embed=embed)
trackDuration = await Utils().durationFormat(track.duration)
trackTitle = track.title.replace("*", "\\*")

if len(tracks) == 1:
if queueSizeAndDuration := DBQueue(
self.bot.dbConnection
).queueSizeAndDuration(ctx.guild.id):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function addTrack refactored with the following changes:

This removes the following comments ( why? ):

# If it's a playlist => Update the same message to do not spam the channel
# Queue size and duration

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Jan 3, 2023

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.01%.

Quality metrics Before After Change
Complexity 14.41 🙂 13.60 🙂 -0.81 👍
Method Length 137.36 😞 137.82 😞 0.46 👎
Working memory 11.87 😞 11.71 😞 -0.16 👍
Quality 43.86% 😞 43.87% 😞 0.01% 👍
Other metrics Before After Change
Lines 517 559 42
Changed files Quality Before Quality After Quality Change
Cogs/events.py 42.31% 😞 42.95% 😞 0.64% 👍
Cogs/play.py 54.94% 🙂 55.23% 🙂 0.29% 👍
Cogs/skip.py 48.43% 😞 48.45% 😞 0.02% 👍
Tools/addTrack.py 8.87% ⛔ 12.58% ⛔ 3.71% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
Tools/addTrack.py addTrack 31 😞 723 ⛔ 21 ⛔ 12.58% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
Cogs/events.py EventsCog.on_command_error 16 🙂 483 ⛔ 15 😞 27.57% 😞 Try splitting into smaller methods. Extract out complex expressions
Cogs/events.py EventsCog.on_voice_state_update 17 🙂 244 ⛔ 14 😞 33.31% 😞 Try splitting into smaller methods. Extract out complex expressions
Cogs/play.py CogPlay.play 24 😞 277 ⛔ 10 😞 33.49% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
Cogs/skip.py CogSkip.skip 8 ⭐ 299 ⛔ 14 😞 38.78% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant