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