Tasks (Sourcery refactored)#9
Conversation
e4015a3 to
25be4a3
Compare
|
|
||
| self.db: Optional[motor_asyncio.AsyncIOMotorClient] = None | ||
| self.models = list() | ||
| self.models = [] |
There was a problem hiding this comment.
Function Bot.__init__ refactored with the following changes:
- Replace list() with [] (
list-literal)
|
|
||
| if platform == "linux" or platform == "linux2" or platform == "darwin": | ||
| if platform in ["linux", "linux2", "darwin", "win32"]: | ||
| # os.system("clear") | ||
| jurigged.watch(pattern="./extensions/*.py") | ||
|
|
||
| self.logger.info(f"--Pro Clubs Nation Bot {self.config.version}") | ||
| self.logger.info("Connected to {} guild(s)".format(len(self.guilds))) | ||
| self.logger.info(f"Logged in as {self.user}.") | ||
| self.logger.info(f"Extensions: {', '.join(self.ext)}") | ||
|
|
||
|
|
||
| elif platform == "win32": | ||
| # os.system('cls') | ||
| jurigged.watch(pattern="./extensions/*.py") | ||
|
|
||
| self.logger.info(f"--Pro Clubs Nation Bot {self.config.version}") | ||
| self.logger.info("Connected to {} guild(s)".format(len(self.guilds))) | ||
| self.logger.info(f"Connected to {len(self.guilds)} guild(s)") | ||
| self.logger.info(f"Logged in as {self.user}.") | ||
| self.logger.info(f"Extensions: {', '.join(self.ext)}") |
There was a problem hiding this comment.
Function Bot.on_ready refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Replace multiple comparisons of same variable with
inoperator [×2] (merge-comparisons) - Replace call to format with f-string (
use-fstring-for-formatting) - Remove redundant conditional (
remove-redundant-if)
This removes the following comments ( why? ):
# os.system('cls')
| if show_seconds: | ||
| return f"{minutes_fmt} and {seconds_fmt}" | ||
| return f"{minutes_fmt}" | ||
| return f"{minutes_fmt} and {seconds_fmt}" if show_seconds else f"{minutes_fmt}" |
There was a problem hiding this comment.
Function strf_delta refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| async def get_async(self): | ||
| async with httpx.AsyncClient(timeout=None) as client: | ||
| return await client.get(url) | ||
| return await client.get(self) |
There was a problem hiding this comment.
Function BotInfo.get_async refactored with the following changes:
- The first argument to instance methods should be
self(instance-method-first-arg-name)
| guild_list = [] | ||
| guild_list = [f"[ {guild.name} ]" for guild in self.bot.guilds] | ||
|
|
||
| for guild in self.bot.guilds: | ||
| guild_list.append(f"[ {guild.name} ]") |
There was a problem hiding this comment.
Function BotInfo._guild_names refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
|
|
There was a problem hiding this comment.
Found the following improvement in Function BotInfo._server_info:
| if ctx.author.id == 111252573054312448 or ctx.author.id == 421064675267051531: | ||
| await ctx.message.delete() | ||
| threads = await ChannelConverter().convert(ctx, "verification-threads") | ||
| general = await ChannelConverter().convert(ctx, "general-discussion") | ||
| embed = Embed( | ||
| title="Welcome to PCN Discord", | ||
| description=f"""1. Discord is provided as a courtesy by the owners of PCN, meaning that it is a privilege, not a right | ||
| if ctx.author.id not in [111252573054312448, 421064675267051531]: | ||
| return | ||
| await ctx.message.delete() | ||
| threads = await ChannelConverter().convert(ctx, "verification-threads") | ||
| general = await ChannelConverter().convert(ctx, "general-discussion") | ||
| embed = Embed( | ||
| title="Welcome to PCN Discord", | ||
| description=f"""1. Discord is provided as a courtesy by the owners of PCN, meaning that it is a privilege, not a right |
There was a problem hiding this comment.
Function DiscordVerification.verify_init refactored with the following changes:
- Add guard clause (
last-if-guard) - Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| await ctx.author.add_role(await RoleConverter().convert(ctx, "Waiting Verification"), "Waiting for verification.") | ||
| await ctx.author.remove_role(await RoleConverter().convert(ctx, "New Member"), "Verification started") | ||
| await ctx.author.add_role(await RoleConverter().convert(ctx, "Waiting Verification"), "Waiting for verification.") | ||
| await ctx.author.remove_role(await RoleConverter().convert(ctx, "New Member"), "Verification started") | ||
|
|
||
| thread = await channel.create_public_thread( | ||
| name=f"{ctx.author.display_name}'s Verification", | ||
| auto_archive_duration=AutoArchiveDuration.ONE_WEEK, | ||
| reason="Verification Thread", | ||
| # invitable=False, | ||
| ) | ||
| await thread.add_member(ctx.author.id) | ||
| thread = await channel.create_public_thread( | ||
| name=f"{ctx.author.display_name}'s Verification", | ||
| auto_archive_duration=AutoArchiveDuration.ONE_WEEK, | ||
| reason="Verification Thread", | ||
| # invitable=False, | ||
| ) | ||
| await thread.add_member(ctx.author.id) | ||
|
|
||
| if not previous_tag: | ||
| previous_tag = "--" | ||
| if not previous_tag: | ||
| previous_tag = "--" | ||
|
|
||
| await thread.send( | ||
| f"Welcome to your verification thread, {ctx.author.mention}." | ||
| "\nThis thread was created automatically by the bot due to your Gamer Tag not being found.\n\n" | ||
| "**Provided Information:**\n\n" | ||
| f"Gamertag: **{current_tag}**\n" | ||
| f"Previous Gamertags: **{previous_tag}**\n" | ||
| "Verified Onsite: **False**" | ||
| "\n\n**Please make sure you have Direct Messages enabled for this server." | ||
| "\nYou will be notified via DM when access is granted.**" | ||
| "\nTo enble DMs for this server only on mobile, click server name > Allow Direct Messages." | ||
| "\nTo enable DMs for this server only on desktop, click server name > Privacy Settings > Allow Direct Messages." | ||
| "\n\n**Discord verifications are automatically checked every 24 hours.**" | ||
| ) | ||
| await thread.send( | ||
| f"Welcome to your verification thread, {ctx.author.mention}." | ||
| "\nThis thread was created automatically by the bot due to your Gamer Tag not being found.\n\n" | ||
| "**Provided Information:**\n\n" | ||
| f"Gamertag: **{current_tag}**\n" | ||
| f"Previous Gamertags: **{previous_tag}**\n" | ||
| "Verified Onsite: **False**" | ||
| "\n\n**Please make sure you have Direct Messages enabled for this server." | ||
| "\nYou will be notified via DM when access is granted.**" | ||
| "\nTo enble DMs for this server only on mobile, click server name > Allow Direct Messages." | ||
| "\nTo enable DMs for this server only on desktop, click server name > Privacy Settings > Allow Direct Messages." | ||
| "\n\n**Discord verifications are automatically checked every 24 hours.**" | ||
| ) | ||
|
|
||
|
|
||
| await ctx.send(f"{ctx.author.mention} Your verification thread has been created here: {thread.mention}", ephemeral=True) | ||
|
|
||
| existing_verification = await VerificationQueue.find_one({"discord_id": ctx.author.id}) | ||
| if existing_verification is None: | ||
| waiting_verification = VerificationQueue( | ||
| discord_id=ctx.author.id, | ||
| discord_name=ctx.author.display_name, | ||
| gamertag=current_tag, | ||
| status="New", | ||
| reason="Waiting for website verification", | ||
| updated=datetime.now(), | ||
| discord_thread=thread.id, | ||
| ) | ||
| await waiting_verification.save() | ||
| await ctx.send(f"{ctx.author.mention} Your verification thread has been created here: {thread.mention}", ephemeral=True) | ||
|
|
||
| existing_verification = await VerificationQueue.find_one({"discord_id": ctx.author.id}) | ||
| if existing_verification is None: | ||
| waiting_verification = VerificationQueue( | ||
| discord_id=ctx.author.id, | ||
| discord_name=ctx.author.display_name, | ||
| gamertag=current_tag, | ||
| status="New", | ||
| reason="Waiting for website verification", | ||
| updated=datetime.now(), | ||
| discord_thread=thread.id, | ||
| ) | ||
| await waiting_verification.save() |
There was a problem hiding this comment.
Function DiscordVerification.create_thread refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass)
| self.logger.error(e) | ||
| elif gamertag is not None: | ||
| self.logger.error(e) | ||
| else: |
There was a problem hiding this comment.
Function PlayerStats.player_stats refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| db_repsonse = orjson.loads(search_response) | ||
|
|
||
| if not db_repsonse: | ||
| if not (db_repsonse := orjson.loads(search_response)): | ||
| return False | ||
| else: | ||
| for db_player in db_repsonse: | ||
| if len(db_repsonse) <= 1: | ||
| async with session.get(self.bot.config.urls.players.format(db_player['slug'])) as gamertag_lookup: | ||
| if gamertag_lookup.status == 200: | ||
| lookup_player = await gamertag_lookup.text() | ||
| player_found = orjson.loads(lookup_player) | ||
|
|
||
| if len(player_found) < 1: | ||
| return False | ||
| else: | ||
| return True | ||
| else: | ||
| if re.match(doc.gamertag, db_player["title"]['rendered'], flags=re.I): | ||
| async with session.get(self.bot.config.urls.players.format(db_player['slug'])) as gamertag_lookup: | ||
| if gamertag_lookup.status == 200: | ||
| lookup_player = await gamertag_lookup.text() | ||
| player_found = orjson.loads(lookup_player) | ||
|
|
||
| if len(player_found) < 1: | ||
| return False | ||
| else: | ||
| return True | ||
| for db_player in db_repsonse: | ||
| if len(db_repsonse) <= 1: | ||
| async with session.get(self.bot.config.urls.players.format(db_player['slug'])) as gamertag_lookup: | ||
| if gamertag_lookup.status == 200: | ||
| lookup_player = await gamertag_lookup.text() | ||
| player_found = orjson.loads(lookup_player) | ||
|
|
||
| return len(player_found) >= 1 | ||
| elif re.match(doc.gamertag, db_player["title"]['rendered'], flags=re.I): | ||
| async with session.get(self.bot.config.urls.players.format(db_player['slug'])) as gamertag_lookup: | ||
| if gamertag_lookup.status == 200: | ||
| lookup_player = await gamertag_lookup.text() | ||
| player_found = orjson.loads(lookup_player) | ||
|
|
||
| return len(player_found) >= 1 |
There was a problem hiding this comment.
Function fetch_api refactored with the following changes:
- Simplify boolean if expression [×2] (
boolean-if-exp-identity) - Remove unnecessary casts to int, str, float or bool [×2] (
remove-unnecessary-cast) - Swap if/else branches [×2] (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else) - Use named expression to simplify assignment and conditional (
use-named-expression) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif) - Replace if statement with if expression [×2] (
assign-if-exp)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 1.23%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
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! |
Pull Request #8 refactored by Sourcery.
If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
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:
Review changes via command line
To manually merge these changes, make sure you're on the
tasksbranch, then run:Help us improve this pull request!