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