| Conditions | 9 |
| Total Lines | 90 |
| Code Lines | 56 |
| 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:
| 1 | """Schools commands for bot""" |
||
| 136 | @commands.command( |
||
| 137 | name="add-school", |
||
| 138 | help="Adds a new school and makes a role for it.\n" |
||
| 139 | "Only schools on the list are allowed to join.\n" |
||
| 140 | "List: https://github.com/Competitive-Cyber-Clubs/School-List/blob/master/school_list.csv", |
||
| 141 | description="Creates a new school", |
||
| 142 | ) |
||
| 143 | @commands.has_role("new")
|
||
| 144 | async def add_school( |
||
| 145 | self, ctx: commands.Context, *, school_name: str |
||
| 146 | ): # pylint: disable=too-many-branches |
||
| 147 | """add_school |
||
| 148 | --- |
||
| 149 | |||
| 150 | Enables users to create a school role. They are required to have the role "new". Schools |
||
| 151 | will automatically be assigned a region based on the school_list.csv in utils. |
||
| 152 | |||
| 153 | Arguments: |
||
| 154 | --- |
||
| 155 | ctx {discord.ext.commands.Context} -- Context of the command.
|
||
| 156 | school_name {str} -- Name of the school the user wants to join.
|
||
| 157 | |||
| 158 | Raises: |
||
| 159 | --- |
||
| 160 | utils.FailedReactionCheck: Exception is raised if the reaction check does not validate. |
||
| 161 | """ |
||
| 162 | if not await utils.school_check(self.bot.school_list, school_name): |
||
| 163 | return await utils.make_embed(ctx, "FF0000", title="Error: School name not valid.") |
||
| 164 | |||
| 165 | if await utils.select("schools", "school", "school", school_name):
|
||
| 166 | self.log.info( |
||
| 167 | "{} attempted to create a duplicate role for {}".format(
|
||
| 168 | ctx.author.name, school_name |
||
| 169 | ) |
||
| 170 | ) |
||
| 171 | return await utils.error_message(ctx, f"School role for {school_name} already exists.")
|
||
| 172 | |||
| 173 | regions = await utils.fetch("regions", "name")
|
||
| 174 | region = await utils.region_select(self.bot.school_list, school_name) |
||
| 175 | if region not in regions: |
||
| 176 | # No region map error |
||
| 177 | self.log.error( |
||
| 178 | "There is no region map for {}, region: {}, {}".format(school_name, region, regions)
|
||
| 179 | ) |
||
| 180 | return await utils.error_message(ctx, f"No region defined for {school_name}")
|
||
| 181 | |||
| 182 | await utils.make_embed( |
||
| 183 | ctx, |
||
| 184 | title=f"You are about to create a new school: {school_name}.",
|
||
| 185 | description="React 👍 to this message confirm.", |
||
| 186 | ) |
||
| 187 | # Gives the user 30 seconds to add the reaction '👍' to the message. |
||
| 188 | try: |
||
| 189 | reactions, user = await self.bot.wait_for("reaction_add", timeout=30)
|
||
| 190 | if not await utils.check_react(ctx, user, reactions, "👍"): |
||
| 191 | raise utils.FailedReactionCheck |
||
| 192 | except asyncio.TimeoutError: |
||
| 193 | await utils.error_message( |
||
| 194 | ctx, "Timed out waiting for a reaction. Please reach to the message in 30 seconds" |
||
| 195 | ) |
||
| 196 | except utils.FailedReactionCheck: |
||
| 197 | await utils.error_message(ctx, "Wrong reaction added or added by the wrong user") |
||
| 198 | else: |
||
| 199 | color = int("0x%06x" % random.randint(0, 0xFFFFFF), 16) # nosec
|
||
| 200 | added_school = await ctx.guild.create_role( |
||
| 201 | name=school_name, |
||
| 202 | color=discord.Color(color), |
||
| 203 | mentionable=True, |
||
| 204 | hoist=False, |
||
| 205 | reason="Added by {}".format(ctx.author.name),
|
||
| 206 | ) |
||
| 207 | data = [ |
||
| 208 | school_name, |
||
| 209 | region, |
||
| 210 | color, |
||
| 211 | added_school.id, |
||
| 212 | (ctx.author.name + ctx.author.discriminator), |
||
| 213 | ctx.author.id, |
||
| 214 | ] |
||
| 215 | status = await utils.insert("schools", data)
|
||
| 216 | if status == "error": |
||
| 217 | await utils.error_message(ctx, "There was an error with creating the role.") |
||
| 218 | await added_school.delete(reason="Error in creation") |
||
| 219 | self.log.warning("Error with School Role creation.")
|
||
| 220 | else: |
||
| 221 | success_msg = 'School "{}" has been created in {} with color of 0x{}'.format(
|
||
| 222 | school_name, region, color |
||
| 223 | ) |
||
| 224 | await utils.make_embed(ctx, color=color, title="Success", description=success_msg) |
||
| 225 | await self.join_school(ctx=ctx, school_name=school_name) |
||
| 226 | |||
| 231 |