| Total Complexity | 8 |
| Total Lines | 49 |
| Duplicated Lines | 83.67 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | import json |
||
| 2 | |||
| 3 | import discord |
||
| 4 | from discord.ext import commands |
||
| 5 | from discord_slash import SlashContext |
||
| 6 | |||
| 7 | |||
| 8 | View Code Duplication | class BaseCog(commands.Cog): |
|
| 9 | def __init__(self, bot): |
||
| 10 | self.bot = bot |
||
| 11 | print(f"[INFO]: Init {self.__class__.__name__}") |
||
| 12 | |||
| 13 | # Find monster in config |
||
| 14 | async def get_monster(self, ctx: SlashContext, name: str): |
||
| 15 | """ |
||
| 16 | |||
| 17 | :param ctx: |
||
| 18 | :type ctx: |
||
| 19 | :param name: |
||
| 20 | :type name: |
||
| 21 | :return: |
||
| 22 | :rtype: |
||
| 23 | """ |
||
| 24 | monster = [] |
||
| 25 | name = name.lower() |
||
| 26 | |||
| 27 | for monsters in self.bot.config["commands"]: |
||
| 28 | if monsters["name"].lower() == name: |
||
| 29 | monster = monsters |
||
| 30 | break |
||
| 31 | for monster_triggers in monsters["triggers"]: |
||
| 32 | if monster_triggers == name: |
||
| 33 | monster = monsters |
||
| 34 | |||
| 35 | if not monster: |
||
| 36 | print("Monster not found") |
||
| 37 | await ctx.send("Monster not found", hidden=True) |
||
| 38 | return |
||
| 39 | |||
| 40 | monster["role"] = discord.utils.get(ctx.guild.roles, name=monster["name"]) |
||
| 41 | if not monster["role"]: |
||
| 42 | print(f"Failed to fetch roleID for monster {monster['name']}") |
||
| 43 | await ctx.send("Role not found", hidden=True) |
||
| 44 | return |
||
| 45 | |||
| 46 | else: |
||
| 47 | monster["role"] = monster["role"].id |
||
| 48 | return monster |
||
| 49 |