| Conditions | 14 |
| Total Lines | 52 |
| Code Lines | 36 |
| 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 chatbot.Chatbot.on_message() 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 discord |
||
| 131 | @commands.Cog.listener() |
||
| 132 | async def on_message(self, message): |
||
| 133 | if not message.guild: |
||
| 134 | return |
||
| 135 | |||
| 136 | guild_plugin_doc = await db.PLUGINS.find_one({"_id": message.guild.id}) |
||
| 137 | ctx = await self.bot.get_context(message) |
||
| 138 | |||
| 139 | if guild_plugin_doc["Chatbot"] and message.channel.id != 803308171577393172: |
||
| 140 | guild_chatbot_doc = await db.CHATBOT.find_one( |
||
| 141 | {"_id": message.guild.id} |
||
| 142 | ) |
||
| 143 | |||
| 144 | def channels(): |
||
| 145 | if ( |
||
| 146 | guild_chatbot_doc is not None |
||
| 147 | and guild_chatbot_doc.get("channels") != [] |
||
| 148 | ): |
||
| 149 | channels = guild_chatbot_doc.get("channels") |
||
| 150 | |||
| 151 | else: |
||
| 152 | channels = [] |
||
| 153 | |||
| 154 | return channels |
||
| 155 | |||
| 156 | if ( |
||
| 157 | self.bot.user in message.mentions |
||
| 158 | and message.author.bot == False |
||
| 159 | or message.channel.id in channels() |
||
| 160 | and message.author.bot == False |
||
| 161 | ): |
||
| 162 | |||
| 163 | content = message.content.replace("<@!843484459113775114>", "") |
||
| 164 | async with request( |
||
| 165 | "POST", |
||
| 166 | f"https://axiol.up.railway.app/ai/chatbot", |
||
| 167 | json={"content": content} |
||
| 168 | ) as response: |
||
| 169 | res = await response.json() |
||
| 170 | |||
| 171 | if res["response"] == "help": |
||
| 172 | await ctx.invoke(self.bot.get_command('help')) |
||
| 173 | |||
| 174 | elif res["tag"] == "prefix": |
||
| 175 | await message.channel.send( |
||
| 176 | res["response"].replace("~", await get_prefix(ctx))) |
||
| 177 | |||
| 178 | else: |
||
| 179 | await message.channel.send(res["response"]) |
||
| 180 | |||
| 181 | elif self.bot.user.mention in message.mentions: |
||
| 182 | await ctx.invoke(self.bot.get_command("help")) |
||
| 183 | |||
| 187 |