Conditions | 9 |
Total Lines | 93 |
Code Lines | 58 |
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""" |
||
192 | @commands.command( |
||
193 | name="add-school", |
||
194 | help="Adds a new school and makes a role for it.\n" |
||
195 | "Only schools on the list are allowed to join.\n" |
||
196 | "List: https://github.com/Competitive-Cyber-Clubs/School-List/blob/master/school_list.csv", |
||
197 | description="Creates a new school", |
||
198 | ) |
||
199 | @commands.has_role("new") |
||
200 | async def add_school( |
||
201 | self, ctx: commands.Context, *, school_name: str |
||
202 | ) -> None: # pylint: disable=too-many-branches |
||
203 | """ |
||
204 | Add school |
||
205 | |||
206 | Enables users to create a school role. They are required to have the role "new". Schools |
||
207 | will automatically be assigned a region based on the school_list.csv in utils. |
||
208 | |||
209 | **Raises:** |
||
210 | |||
211 | utils.FailedReactionCheck: Exception is raised if the reaction check does not validate. |
||
212 | |||
213 | :param ctx: Command Context |
||
214 | :type ctx: discord.ext.commands.Context |
||
215 | :param school_name: Name of school to join |
||
216 | :type school_name: str |
||
217 | :return: None |
||
218 | """ |
||
219 | if not await utils.school_check(self.bot.school_list, school_name): |
||
220 | return await utils.error_message(ctx, message="School name not valid.") |
||
221 | |||
222 | if await utils.select("schools", "school", "school", school_name): |
||
223 | self.bot.log.info( |
||
224 | f"{ctx.author.name} attempted to create a duplicate role for {school_name}" |
||
225 | ) |
||
226 | return await utils.error_message( |
||
227 | ctx, |
||
228 | f"School role for {school_name} already exists.\n" |
||
229 | f"Use `?join-school {school_name}` to join it", |
||
230 | ) |
||
231 | |||
232 | regions = await utils.fetch("regions", "name") |
||
233 | region = await utils.region_select(self.bot.school_list, school_name) |
||
234 | if region not in regions: |
||
235 | # No region map error |
||
236 | self.bot.log.error( |
||
237 | f"There is no region map for {school_name}, region: {region}, regions: {regions}" |
||
238 | ) |
||
239 | return await utils.error_message(ctx, f"No region defined for {school_name}") |
||
240 | color = random.randint(0, 16777215) # nosec |
||
241 | await utils.make_embed( |
||
242 | ctx, |
||
243 | title=f"You are about to create a new school: {school_name}.", |
||
244 | description="React 👍 to this message in 60 seconds to confirm.", |
||
245 | color=color, |
||
246 | ) |
||
247 | # Gives the member 60 seconds to add the reaction '👍' to the message. |
||
248 | try: |
||
249 | reactions, user = await self.bot.wait_for("reaction_add", timeout=60) |
||
250 | if not await utils.check_react(ctx, user, reactions, "👍"): |
||
251 | raise utils.FailedReactionCheck |
||
252 | except asyncio.TimeoutError: |
||
253 | await utils.error_message( |
||
254 | ctx, "Timed out waiting for a reaction. Please reach to the message in 30 seconds" |
||
255 | ) |
||
256 | except utils.FailedReactionCheck: |
||
257 | await utils.error_message(ctx, "Wrong reaction added or added by the wrong member") |
||
258 | else: |
||
259 | added_school = await ctx.guild.create_role( |
||
260 | name=school_name, |
||
261 | color=discord.Color(color), |
||
262 | mentionable=True, |
||
263 | hoist=False, |
||
264 | reason=f"Added by {ctx.author.name}", |
||
265 | ) |
||
266 | data = [ |
||
267 | school_name, |
||
268 | region, |
||
269 | color, |
||
270 | added_school.id, |
||
271 | (ctx.author.name + ctx.author.discriminator), |
||
272 | ctx.author.id, |
||
273 | ] |
||
274 | status = await utils.insert("schools", data) |
||
275 | if status == "error": |
||
276 | await utils.error_message(ctx, "There was an error with creating the role.") |
||
277 | await added_school.delete(reason="Error in creation") |
||
278 | self.bot.log.warning("Error with School Role creation.") |
||
279 | else: |
||
280 | success_msg = ( |
||
281 | f'School "{school_name}" has been created in {region} with color of 0x{color}' |
||
282 | ) |
||
283 | await utils.make_embed(ctx, color=color, title="Success", description=success_msg) |
||
284 | await self.join_school(ctx=ctx, school_name=school_name) |
||
285 | |||
290 |