Conditions | 10 |
Total Lines | 90 |
Code Lines | 59 |
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 schools.SchoolCog.add_school() 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 | """Schools commands for bot""" |
||
147 | async def add_school( |
||
148 | self, ctx: commands.Context, school_name: str, force: bool = False |
||
149 | ) -> None: # pylint: disable=too-many-branches |
||
150 | """ |
||
151 | Add school |
||
152 | |||
153 | Creates a new role for a school |
||
154 | A region will be automatically added based on the school_list.csv in utils. |
||
155 | |||
156 | :param ctx: Command Context |
||
157 | :type ctx: discord.ext.commands.Context |
||
158 | :param school_name: Name of school to join |
||
159 | :type school_name: str |
||
160 | :param force: Don't get conformation from user |
||
161 | :type force: bool |
||
162 | :return: None |
||
163 | """ |
||
164 | if not await utils.school_check(self.bot.school_list, school_name): |
||
165 | return await cyberjake.error_embed(ctx, message="School name not valid.") |
||
166 | |||
167 | if await utils.select("schools", "school", "school", school_name): |
||
168 | self.bot.log.info( |
||
169 | f"{ctx.author.name} attempted to create a duplicate role for {school_name}" |
||
170 | ) |
||
171 | return await cyberjake.error_embed( |
||
172 | ctx, |
||
173 | f"School role for {school_name} already exists.\n" |
||
174 | f"Use `?join-school {school_name}` to join it", |
||
175 | ) |
||
176 | |||
177 | regions = await utils.fetch("regions", "name") |
||
178 | region = await utils.region_select(self.bot.school_list, school_name) |
||
179 | if region not in regions: |
||
180 | # No region map error |
||
181 | self.bot.log.error( |
||
182 | f"There is no region map for {school_name}, region: {region}, regions: {regions}" |
||
183 | ) |
||
184 | return await cyberjake.error_embed(ctx, f"No region defined for {school_name}") |
||
185 | color = random.randint(0, 16777215) # nosec |
||
186 | if not force: |
||
187 | await cyberjake.make_embed( |
||
188 | ctx, |
||
189 | title=f"You are about to create a new school: {school_name}.", |
||
190 | description="React 👍 to this message in 60 seconds to confirm.", |
||
191 | color=color, |
||
192 | ) |
||
193 | # Gives the member 60 seconds to add the reaction '👍' to the message. |
||
194 | try: |
||
195 | reactions, user = await self.bot.wait_for("reaction_add", timeout=60) |
||
196 | if not await utils.check_react(ctx, user, reactions, "👍"): |
||
197 | raise utils.FailedReactionCheck |
||
198 | except asyncio.TimeoutError: |
||
199 | await cyberjake.error_embed( |
||
200 | ctx, |
||
201 | "Timed out waiting for a reaction. Please reach to the message in 30 seconds", |
||
202 | ) |
||
203 | return |
||
204 | except utils.FailedReactionCheck: |
||
205 | await cyberjake.error_embed( |
||
206 | ctx, "Wrong reaction added or added by the wrong member" |
||
207 | ) |
||
208 | return |
||
209 | |||
210 | added_school = await ctx.guild.create_role( |
||
211 | name=school_name, |
||
212 | color=discord.Color(color), |
||
213 | mentionable=True, |
||
214 | hoist=False, |
||
215 | reason=f"Added by {ctx.author.name}", |
||
216 | ) |
||
217 | data = [ |
||
218 | school_name, |
||
219 | region, |
||
220 | color, |
||
221 | added_school.id, |
||
222 | (ctx.author.name + ctx.author.discriminator), |
||
223 | ctx.author.id, |
||
224 | ] |
||
225 | status = await utils.insert("schools", data) |
||
226 | if status == "error": |
||
227 | await cyberjake.error_embed(ctx, "There was an error with creating the role.") |
||
228 | await added_school.delete(reason="Error in creation") |
||
229 | self.bot.log.warning("Error with School Role creation.") |
||
230 | else: |
||
231 | success_msg = ( |
||
232 | f'School "{school_name}" has been created in {region} with color of 0x{color}' |
||
233 | ) |
||
234 | await cyberjake.make_embed(ctx, color=color, title="Success", description=success_msg) |
||
235 | if not force: |
||
236 | await self.join_school(ctx=ctx, school_name=school_name) |
||
237 | |||
242 |