| Conditions | 19 |
| Total Lines | 67 |
| Code Lines | 54 |
| 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:
Complex classes like cogs.events.events.on_raw_reaction_add() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import os |
||
| 52 | @commands.Cog.listener() |
||
| 53 | async def on_raw_reaction_add(self, payload): |
||
| 54 | message_id = payload.message_id |
||
| 55 | if message_id == 734044027858452502: # ID of the role message |
||
| 56 | guild_id = payload.guild_id |
||
| 57 | guild = discord.utils.find(lambda g: g.id == guild_id, self.bot.guilds) |
||
|
|
|||
| 58 | |||
| 59 | roles = { |
||
| 60 | "πββοΈ": "He/Him", |
||
| 61 | "πββοΈ": "She/Her", |
||
| 62 | "π": "They/Them", |
||
| 63 | } |
||
| 64 | |||
| 65 | if payload.emoji.name == "πββοΈ": |
||
| 66 | role = discord.utils.get(guild.roles, name="He/Him") |
||
| 67 | if payload.emoji.name == "πββοΈ": |
||
| 68 | role = discord.utils.get(guild.roles, name="She/Her") |
||
| 69 | if payload.emoji.name == "π": |
||
| 70 | role = discord.utils.get(guild.roles, name="They/Them") |
||
| 71 | |||
| 72 | if role is not None: |
||
| 73 | member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members) |
||
| 74 | if member is not None: |
||
| 75 | current_role = list(filter(lambda r: str(r) in list(roles.values()), payload.member.roles)) |
||
| 76 | await member.remove_roles(*current_role) |
||
| 77 | await member.add_roles(role) |
||
| 78 | pronoun_message = await self.bot.get_channel(699675695231533126).fetch_message( |
||
| 79 | 734044027858452502 |
||
| 80 | ) # ID of role message or channel |
||
| 81 | await pronoun_message.remove_reaction(payload.emoji, payload.member) |
||
| 82 | else: |
||
| 83 | print("member not found") |
||
| 84 | |||
| 85 | if message_id == 734610164899905569: |
||
| 86 | guild_id = payload.guild_id |
||
| 87 | guild = discord.utils.find(lambda g: g.id == guild_id, self.bot.guilds) |
||
| 88 | |||
| 89 | roles = { |
||
| 90 | "πΆ": "πΆ Freshman", |
||
| 91 | "βοΈ": "βοΈ Sophomore", |
||
| 92 | "π»": "π» Junior", |
||
| 93 | "π": "π Senior", |
||
| 94 | "π": "π SuperSenior", |
||
| 95 | "π¨βπ": "π¨βπ Alumna", |
||
| 96 | "π": "π Graduate School", |
||
| 97 | "πΎ": "πΎ PhD", |
||
| 98 | } |
||
| 99 | |||
| 100 | role = None |
||
| 101 | |||
| 102 | for x in list(roles.keys()): |
||
| 103 | if payload.emoji.name == x: |
||
| 104 | role = discord.utils.get(guild.roles, name=roles[x]) |
||
| 105 | break |
||
| 106 | |||
| 107 | if role is not None: |
||
| 108 | current_role = list(filter(lambda r: str(r) in list(roles.values()), payload.member.roles)) |
||
| 109 | await payload.member.remove_roles(*current_role) |
||
| 110 | await payload.member.add_roles(role) |
||
| 111 | class_message = await self.bot.get_channel(699675695231533126).fetch_message(734610164899905569) |
||
| 112 | if current_role: |
||
| 113 | for emoji, pre_role in roles.items(): |
||
| 114 | if pre_role == str(current_role[0]): |
||
| 115 | await class_message.remove_reaction(emoji, payload.member) |
||
| 116 | break |
||
| 117 | else: |
||
| 118 | print("role not found") |
||
| 119 | |||
| 173 |