Conditions | 8 |
Total Lines | 76 |
Code Lines | 55 |
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""" |
||
144 | @commands.command(name="add-school", |
||
145 | help="Adds a new school and makes a role for it .", |
||
146 | description="Creates a new school") |
||
147 | @commands.has_role("new") |
||
148 | async def add_school(self, ctx, *, school_name: str): # noqa: E501 pylint: disable=too-many-branches,line-too-long |
||
149 | """add_school |
||
150 | --- |
||
151 | |||
152 | Enables users to create a school role. They are required to have the role "new". Schools |
||
153 | will automatically be assigned a region based on the schools.csv in utils. |
||
154 | |||
155 | Arguments: |
||
156 | --- |
||
157 | ctx {discord.ext.commands.Context} -- Context of the command. |
||
158 | school_name {str} -- Name of the school the user wants to join. |
||
159 | |||
160 | Raises: |
||
161 | --- |
||
162 | utils.FailedReactionCheck: Expection is raised if the reaction check does not validate. |
||
163 | """ |
||
164 | if not await utils.school_check(school_name): |
||
165 | return await utils.make_embed( |
||
166 | ctx, "FF0000", title="Error: School name not valid.") |
||
167 | |||
168 | r_regions = await utils.fetch("regions", "name") |
||
169 | region = await utils.region_select(school_name) |
||
170 | if region not in r_regions: |
||
171 | # No region map error |
||
172 | self.log.error("There is no region map for {}, region: {}, {}".format(school_name, |
||
173 | region, |
||
174 | r_regions)) |
||
175 | return await utils.make_embed( |
||
176 | ctx, "FF0000", title="Error: There is no region mapped") |
||
177 | |||
178 | await utils.make_embed( |
||
179 | ctx, title="You are about to create a new school: {}.".format(school_name), |
||
180 | description="React 👍 to confirm.") |
||
181 | # Gives the user 30 seconds to add the reaction '👍' to the message. |
||
182 | try: |
||
183 | reactions, user = await self.bot.wait_for("reaction_add", timeout=30) |
||
184 | if not await utils.check_react(ctx, user, reactions, "👍"): |
||
185 | raise utils.FailedReactionCheck |
||
186 | except asyncio.TimeoutError: |
||
187 | await utils.make_embed(ctx, "FF0000", title="Error:", |
||
188 | description="Timed out.") |
||
189 | except utils.FailedReactionCheck: |
||
190 | await utils.make_embed( |
||
191 | ctx, "FF0000", title="Error:", |
||
192 | description="Most likely the wrong react was added or by the wrong user.") |
||
193 | else: |
||
194 | color = int("0x%06x" % 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="Added by {}".format(ctx.author.name)) |
||
201 | data = [school_name, |
||
202 | region, |
||
203 | color, |
||
204 | added_school.id, |
||
205 | (ctx.author.name+ctx.author.discriminator), |
||
206 | ctx.author.id] |
||
207 | status = await utils.insert("schools", data) |
||
208 | if status == "error": |
||
209 | await utils.make_embed( |
||
210 | ctx, "FF0000", title="Error", |
||
211 | description="There was an error with creating the role.") |
||
212 | rrole = discord.utils.get(ctx.guild.roles, name=school_name) |
||
213 | await rrole.delete(reason="Error in creation") |
||
214 | self.log.warning("due to error with School Role creation.") |
||
215 | else: |
||
216 | success_msg = ("School \"{}\" has been created in {} with color of 0x{}" |
||
217 | .format(school_name, region, color)) |
||
218 | await utils.make_embed(ctx, color=color, title="Success", |
||
219 | description=success_msg) |
||
220 | |||
225 |