Conditions | 14 |
Total Lines | 79 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like build.cogs.spotstatscog.SpotStatsCog.get_spotting_stats() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import discord |
||
99 | @cog_ext.cog_slash(name="mySpottingStats", guild_ids=cogbase.GUILD_IDS, |
||
100 | description=" ", |
||
101 | default_permission=True |
||
102 | # , permissions=cogbase.PERMISSION_MODS |
||
103 | ) |
||
104 | async def get_spotting_stats(self, ctx): |
||
105 | await ctx.send("Generating spots stats", delete_after=5) |
||
106 | guild = self.bot.get_guild(self.bot.guild[0]) |
||
107 | channel = self.bot.get_channel(self.bot.ch_logs) |
||
108 | leges_list = [] |
||
109 | rares_list = [] |
||
110 | async for message in channel.history(limit=None, oldest_first=True): |
||
111 | if message.content.startswith("[PingLog]") and str(ctx.author.id) in message.content: |
||
112 | for monster in self.bot.config["commands"]: |
||
113 | if monster["name"] in message.content: |
||
114 | monster_name = monster["name"] |
||
115 | print(monster_name) |
||
|
|||
116 | role = get(guild.roles, name=monster_name) |
||
117 | if role: |
||
118 | leges_list.append(self.create_spotted_list(role, 1)) |
||
119 | rares_list.append(self.create_spotted_list(role, 0)) |
||
120 | |||
121 | leges_list = list(filter(None, leges_list)) |
||
122 | leges_counter = Counter(leges_list) |
||
123 | leges_counter = OrderedDict(leges_counter.most_common()) |
||
124 | leges_print = [] |
||
125 | leges_total = 0 |
||
126 | for key, value in leges_counter.items(): |
||
127 | spotting_stats = [f"{key}: **{value}**"] |
||
128 | leges_print.append(spotting_stats) |
||
129 | leges_total += value |
||
130 | leges_print = ['\n'.join([elem for elem in sublist]) for sublist in leges_print] |
||
131 | leges_print = "\n".join(leges_print) |
||
132 | leges_color = int(self.hex_to_int % (163, 140, 21), 16) |
||
133 | embed_command = discord.Embed(title=f"Legendary", description=leges_print, color=leges_color) |
||
134 | embed_command.add_field(name="Total", value=f"**{leges_total}**", inline=False) |
||
135 | if self.lege_total != 0: |
||
136 | percentage_leges = round(leges_total / self.lege_total * 100, 2) |
||
137 | embed_command.add_field(name="Server %", value=f"**{percentage_leges}%**", inline=False) |
||
138 | dt_string = self.bot.get_current_time() |
||
139 | embed_command.set_footer(text=f"{dt_string}") |
||
140 | await ctx.author.send(embed=embed_command) |
||
141 | |||
142 | rares_list = list(filter(None, rares_list)) |
||
143 | rares_counter = Counter(rares_list) |
||
144 | rares_counter = OrderedDict(rares_counter.most_common()) |
||
145 | rares_print = [] |
||
146 | rares_total = 0 |
||
147 | for key, value in rares_counter.items(): |
||
148 | spotting_stats = [f"{key}: **{value}**"] |
||
149 | rares_print.append(spotting_stats) |
||
150 | rares_total += value |
||
151 | rares_print = ['\n'.join([elem for elem in sublist]) for sublist in rares_print] |
||
152 | rares_print = "\n".join(rares_print) |
||
153 | rares_color = int(self.hex_to_int % (17, 93, 178), 16) |
||
154 | embed_command = discord.Embed(title=f"Rare", description=rares_print, color=rares_color) |
||
155 | embed_command.add_field(name="Total", value=f"**{rares_total}**", inline=False) |
||
156 | if self.rare_total != 0: |
||
157 | percentage_rares = round(rares_total / self.rare_total * 100, 2) |
||
158 | embed_command.add_field(name="Server %", value=f"**{percentage_rares}%**", inline=False) |
||
159 | dt_string = self.bot.get_current_time() |
||
160 | embed_command.set_footer(text=f"{dt_string}") |
||
161 | await ctx.author.send(embed=embed_command) |
||
162 | |||
163 | common_ch = self.bot.get_channel(self.bot.ch_common) |
||
164 | common_total = 0 |
||
165 | async for message in common_ch.history(limit=None, oldest_first=True): |
||
166 | self.common_total += 1 |
||
167 | if ctx.author == message.author: |
||
168 | common_total += 1 |
||
169 | embed_command = discord.Embed(title=f"Common") |
||
170 | embed_command.add_field(name="Total", value=f"**{common_total}**", inline=False) |
||
171 | if self.common_total != 0: |
||
172 | percentage_common = round(common_total / self.common_total * 100, 2) |
||
173 | embed_command.add_field(name="Server %", value=f"**{percentage_common}%**", inline=False) |
||
174 | dt_string = self.bot.get_current_time() |
||
175 | embed_command.set_footer(text=f"{dt_string}") |
||
176 | await ctx.author.send(embed=embed_command) |
||
177 | self.create_log_msg(f"Spotting stats created for {ctx.author}") |
||
178 | |||
182 |