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