From 5872a097e8f9e58265f71086963980dc59b18830 Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Sun, 21 Mar 2021 00:23:54 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- dbltools/dbltools.py | 4 +- dbltools/utils.py | 2 +- dbltoolslite/dbltools.py | 2 +- fivem/fivem.py | 130 ++++++++++++++++----------------------- grafana/grafana.py | 2 +- martools/listeners.py | 9 ++- martools/marttools.py | 8 +-- nsfw/constants.py | 3 +- randimages/core.py | 3 +- spacex/core.py | 30 ++++----- 10 files changed, 82 insertions(+), 111 deletions(-) diff --git a/dbltools/dbltools.py b/dbltools/dbltools.py index 7768386..3cbf5e3 100644 --- a/dbltools/dbltools.py +++ b/dbltools/dbltools.py @@ -158,7 +158,7 @@ async def on_member_join(self, member: discord.Member): await self.bot.wait_until_ready() await self._ready_event.wait() config = await self.config.all() - if not member.guild.id == config["support_server_role"]["guild_id"]: + if member.guild.id != config["support_server_role"]["guild_id"]: return if not config["support_server_role"]["role_id"]: return @@ -445,7 +445,7 @@ async def listdblvotes(self, ctx: commands.Context): votes = [] for user_id, value in votes_count.most_common(): user = self.bot.get_user(int(user_id)) - votes.append((user if user else user_id, humanize_number(value))) + votes.append((user or user_id, humanize_number(value))) msg = tabulate(votes, tablefmt="orgtbl") embeds = [] pages = 1 diff --git a/dbltools/utils.py b/dbltools/utils.py index 33ba656..e9d8751 100644 --- a/dbltools/utils.py +++ b/dbltools/utils.py @@ -26,7 +26,7 @@ def check_weekend(): - return True if datetime.today().weekday() in [4, 5, 6] else False + return datetime.today().weekday() in [4, 5, 6] async def download_widget(session: aiohttp.ClientSession, url: str): diff --git a/dbltoolslite/dbltools.py b/dbltoolslite/dbltools.py index dd01140..8844127 100644 --- a/dbltoolslite/dbltools.py +++ b/dbltoolslite/dbltools.py @@ -223,7 +223,7 @@ async def listdblvotes(self, ctx: commands.Context): votes = [] for user_id, value in votes_count.most_common(): user = self.bot.get_user(int(user_id)) - votes.append((user if user else user_id, humanize_number(value))) + votes.append((user or user_id, humanize_number(value))) msg = tabulate(votes, tablefmt="orgtbl") embeds = [] pages = 1 diff --git a/fivem/fivem.py b/fivem/fivem.py index 97cec27..86a4017 100644 --- a/fivem/fivem.py +++ b/fivem/fivem.py @@ -153,86 +153,60 @@ async def _change_bot_status(self): @commands.group() async def fivemset(self, ctx: commands.Context): """Commands group for FiveM cog.""" - if not ctx.invoked_subcommand: - # Logic from Trusty's welcome.py https://github.com/TrustyJAID/Trusty-cogs/blob/master/welcome/welcome.py#L71 - # TODO This is just a first approach to show current settings. - settings = await self.config.get_raw() - settings_name = dict( - toggled="Custom status toggled:", - ip="FiveM IP address:", - text="Custom status text:", - status="Custom status:", - activity_type="Activity type:", - streamer="Streamer:", - stream_title="Stream title:", + if ctx.invoked_subcommand: + return + # Logic from Trusty's welcome.py https://github.com/TrustyJAID/Trusty-cogs/blob/master/welcome/welcome.py#L71 + # TODO This is just a first approach to show current settings. + settings = await self.config.get_raw() + settings_name = dict( + toggled="Custom status toggled:", + ip="FiveM IP address:", + text="Custom status text:", + status="Custom status:", + activity_type="Activity type:", + streamer="Streamer:", + stream_title="Stream title:", + ) + if ctx.channel.permissions_for(ctx.me).embed_links: + em = discord.Embed( + color=await ctx.embed_colour(), title=f"FiveM settings for {self.bot.user}" ) - if ctx.channel.permissions_for(ctx.me).embed_links: - em = discord.Embed( - color=await ctx.embed_colour(), title=f"FiveM settings for {self.bot.user}" - ) - msg = "" - for attr, name in settings_name.items(): - if attr == "toggled": - if settings[attr]: - msg += f"**{name}** Yes\n" - else: - msg += f"**{name}** No\n" - elif attr == "ip": - if settings[attr]: - msg += f"**{name}** {inline(settings[attr])}\n" - else: - msg += f"**{name}** Not set\n" - elif attr == "text": - if settings[attr]: - msg += f"**{name}**\n{inline(settings[attr])}\n" - else: - msg += f"**{name}** Not set\n" - elif attr == "streamer": - if settings[attr]: - msg += f"**{name}** {inline(settings[attr])}\n" - else: - msg += f"**{name}** Not set\n" - elif attr == "stream_title": - if settings[attr]: - msg += f"**{name}** {inline(settings[attr])}\n" - else: - msg += f"**{name}** Not set\n" - else: + msg = "" + for attr, name in settings_name.items(): + if attr in ["ip", "streamer", "stream_title"]: + if settings[attr]: msg += f"**{name}** {inline(settings[attr])}\n" - em.description = msg - await ctx.send(embed=em) - else: - msg = "```\n" - for attr, name in settings_name.items(): - if attr == "toggled": - if settings[attr]: - msg += f"{name} Yes\n" - else: - msg += f"{name} No\n" - elif attr == "ip": - if settings[attr]: - msg += f"{name} {settings[attr]}\n" - else: - msg += f"{name} Not set\n" - elif attr == "text": - if settings[attr]: - msg += f"{name}\n{settings[attr]}\n" - else: - msg += f"{name} Not set\n" - elif attr == "streamer": - if settings[attr]: - msg += f"{name} {settings[attr]}\n" - else: - msg += f"**{name}** Not set\n" - elif attr == "stream_title": - if settings[attr]: - msg += f"{name} {settings[attr]}\n" - else: - msg += f"{name} Not set\n" else: + msg += f"**{name}** Not set\n" + elif attr == "text": + if settings[attr]: + msg += f"**{name}**\n{inline(settings[attr])}\n" + else: + msg += f"**{name}** Not set\n" + elif attr == "toggled": + msg += f"**{name}** Yes\n" if settings[attr] else f"**{name}** No\n" + else: + msg += f"**{name}** {inline(settings[attr])}\n" + em.description = msg + await ctx.send(embed=em) + else: + msg = "```\n" + for attr, name in settings_name.items(): + if attr in ["ip", "stream_title"]: + msg += f"{name} {settings[attr]}\n" if settings[attr] else f"{name} Not set\n" + elif attr == "streamer": + if settings[attr]: msg += f"{name} {settings[attr]}\n" - msg += "```" - await ctx.send(msg) + else: + msg += f"**{name}** Not set\n" + elif attr == "text": + msg += f"{name}\n{settings[attr]}\n" if settings[attr] else f"{name} Not set\n" + elif attr == "toggled": + msg += f"{name} Yes\n" if settings[attr] else f"{name} No\n" + else: + msg += f"{name} {settings[attr]}\n" + msg += "```" + await ctx.send(msg) @fivemset.command() async def ip(self, ctx: commands.Context, *, ip: str): @@ -283,7 +257,7 @@ async def status(self, ctx: commands.Context, *, status: str): - dnd """ statuses = ["online", "dnd", "idle"] - if not status.lower() in statuses: + if status.lower() not in statuses: await ctx.send_help() return @@ -301,7 +275,7 @@ async def activitytype(self, ctx: commands.Context, *, activity: str): - listening """ activity_types = ["playing", "watching", "listening"] - if not activity.lower() in activity_types: + if activity.lower() not in activity_types: await ctx.send_help() return diff --git a/grafana/grafana.py b/grafana/grafana.py index 3bdcc57..0463aa9 100644 --- a/grafana/grafana.py +++ b/grafana/grafana.py @@ -80,7 +80,7 @@ async def graph( f"panelId={params['panelId']}&fullscreen&orgId={params['orgId']}" f"&from={params['from']}&to={params['to']}" ) - filename = "&".join([f"{k}={v}" for k, v in params.items()]) + filename = "&".join(f"{k}={v}" for k, v in params.items()) return await ctx.send(msg, file=discord.File(file, filename=f"graph-{filename}.png")) @graph.command(name="list") diff --git a/martools/listeners.py b/martools/listeners.py index 5c8b02d..71c8833 100644 --- a/martools/listeners.py +++ b/martools/listeners.py @@ -21,9 +21,12 @@ async def on_command_error(self, ctx, error, unhandled_by_cog=False): if hasattr(ctx.command, "on_error"): return - if ctx.cog: - if commands.Cog._get_overridden_method(ctx.cog.cog_command_error) is not None: - return + if ( + ctx.cog + and commands.Cog._get_overridden_method(ctx.cog.cog_command_error) + is not None + ): + return if isinstance(error, commands.CommandInvokeError): self.upsert("command_error") diff --git a/martools/marttools.py b/martools/marttools.py index b285587..8a199fd 100644 --- a/martools/marttools.py +++ b/martools/marttools.py @@ -174,10 +174,7 @@ async def bankstats(self, ctx: commands.Context): member_account = await bank.get_account(ctx.author) created_at = str(member_account.created_at) no = "1970-01-01 00:00:00" - overall = 0 - for key, value in accounts.items(): - overall += value["balance"] - + overall = sum(value["balance"] for key, value in accounts.items()) em = discord.Embed(color=await ctx.embed_colour()) em.set_author(name=_("{} stats:").format(bank_name), icon_url=icon) em.add_field( @@ -560,12 +557,11 @@ async def serversregions(self, ctx: commands.Context, sort: str = "guilds"): regions[region]["guilds"] += 1 def sort_keys(key: str): - keys = ( + return ( (key[1]["guilds"], key[1]["users"]) if sort != "users" else (key[1]["users"], key[1]["guilds"]) ) - return keys regions_stats = dict(sorted(regions.items(), key=lambda x: sort_keys(x), reverse=True)) diff --git a/nsfw/constants.py b/nsfw/constants.py index dde7e7b..d0aa21f 100644 --- a/nsfw/constants.py +++ b/nsfw/constants.py @@ -11,8 +11,7 @@ def emoji(): "\N{BANANA}", "\N{KISS MARK}", ] - emoji = choice(EMOJIS) - return emoji + return choice(EMOJIS) REDDIT_BASEURL = "https://api.reddit.com/r/{}/random" diff --git a/randimages/core.py b/randimages/core.py index 4573815..7f2b6d1 100644 --- a/randimages/core.py +++ b/randimages/core.py @@ -153,8 +153,7 @@ async def _get_others_imgs( except json.decoder.JSONDecodeError as exception: await self._api_errors_msg(ctx, error_code=exception) return None - data = dict(img=img_data, fact=fact_data) - return data + return dict(img=img_data, fact=fact_data) except aiohttp.client_exceptions.ClientConnectionError: await self._api_errors_msg(ctx, error_code="JSON decode failed") return None diff --git a/spacex/core.py b/spacex/core.py index dbff941..35f4d78 100644 --- a/spacex/core.py +++ b/spacex/core.py @@ -162,10 +162,11 @@ async def _history_texts(data: dict): if data["links"]["reddit"] is not None else "", } - description = ( - "Date: **{date}**\n" "{flight_num}" "Links: **{article}{wikipedia}{reddit}**" + return ( + "Date: **{date}**\n" + "{flight_num}" + "Links: **{article}{wikipedia}{reddit}**" ).format(**description_kwargs) - return description @staticmethod async def _launchpads_texts(data: dict): @@ -183,7 +184,7 @@ async def _launchpads_texts(data: dict): "vehicles": ", ".join(data["vehicles_launched"]), "site_name_ext": data["site_name_long"], } - description = ( + return ( "Status: **{status}**\n" "Region: **{region}**\n" "Location: **{location}**\n" @@ -193,7 +194,6 @@ async def _launchpads_texts(data: dict): "Vehicle{s} launched: **{vehicles}**\n" "Site name long: **{site_name_ext}**" ).format(**description_kwargs) - return description @staticmethod async def _landpads_texts(data: dict): @@ -209,14 +209,13 @@ async def _landpads_texts(data: dict): long=data["location"]["longitude"], ), } - description = ( + return ( "Status: **{status}**\n" "Landing type: **{landing_t}**\n" "Attempted landings: **{att_lands}**\n" "Success landings: **{succ_lands}**\n" "Location: **{location}**" ).format(**description_kwargs) - return description @staticmethod async def _missions_texts(data: dict): @@ -228,12 +227,11 @@ async def _missions_texts(data: dict): if data["twitter"] is not None else "", } - description = ( + return ( data["description"] + "\n**[Wikipedia page]({})**".format(data["wikipedia"]) + "{website}{twitter}" ).format(**description_kwargs) - return description async def _roadster_texts(self, data: dict): date, delta = await self._unix_convert(data["launch_date_unix"]) @@ -249,14 +247,13 @@ async def _roadster_texts(self, data: dict): "m_distance_km": round(data["mars_distance_km"], 2), "m_distance_mi": round(data["mars_distance_mi"], 2), } - roadster_stats = ( + return ( "Launch date: **{launch_date} {ago} ago**\n" "Launch mass: **{mass_kg:,} kg / {mass_lbs:,} lbs**\n" "Actual speed: **{speed_km:,} km/h / {speed_mph:,} mph**\n" "Earth distance: **{e_distance_km:,} km / {e_distance_mi:,} mi**\n" "Mars distance: **{m_distance_km:,} km / {m_distance_mi:,} mi**\n" ).format(**roadster_stats_kwargs) - return roadster_stats @staticmethod async def _rockets_texts(data: dict): @@ -315,11 +312,14 @@ async def _rockets_texts(data: dict): "Fuel amount: **{sec_fuel_amount} tons**\n" "Burn time: **{sec_burn_time} secs**\n" ).format(**stages_stats_kwargs) - payload_weights_stats = "" - for p in data["payload_weights"]: - payload_weights_stats += ( - "Name: **{p_name}**\n" "Weight: **{kg_mass:,} kg / {lb_mass:,} lbs**\n" + payload_weights_stats = "".join( + ( + "Name: **{p_name}**\n" + "Weight: **{kg_mass:,} kg / {lb_mass:,} lbs**\n" ).format(p_name=p["name"], kg_mass=p["kg"], lb_mass=p["lb"]) + for p in data["payload_weights"] + ) + engines_stats_kwargs = { "number": data["engines"]["number"], "type": data["engines"]["type"],