Passed
Branch unstable (79b831)
by Sydney
01:37
created

libs.displayname   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 193
Duplicated Lines 29.02 %

Importance

Changes 0
Metric Value
wmc 42
eloc 118
dl 56
loc 193
rs 9.0399
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A memberForName() 0 16 5
A roleForName() 0 10 4
A name() 0 16 5
C checkRoleForInt() 28 58 9
A serverNick() 0 5 3
A memberForID() 0 10 4
A roleForID() 0 5 3
C checkNameForInt() 28 58 9

How to fix   Duplicated Code    Complexity   

Duplicated Code

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:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like libs.displayname 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
# Copyright (c) 2017 CorpNewt
2
# 
3
# This software is released under the MIT License.
4
# https://opensource.org/licenses/MIT
5
6
import discord
7
8
def name(member : discord.Member):
9
    # A helper function to return the member's display name
10
    nick = name = None
11
    try:
12
        nick = member.nick
13
    except AttributeError:
14
        pass
15
    try:
16
        name = member.name
17
    except AttributeError:
18
        pass
19
    if nick:
20
        return nick
21
    if name:
22
        return name
23
    return None
24
25
def memberForID(id, members, me):
26
    # Check self first.
27
    if me.id == id:
28
        return me
29
30
    # Check other members.
31
    for member in members:
32
        if member.id == id:
33
            return member
34
    return None
35
36
def memberForName(name, members, me):
37
    # Check self first.
38
    if me.display_name.lower() == name.lower():
39
        return me
40
41
    # Check rest of members.
42
    for member in members:
43
        if member.display_name.lower() == name.lower():
44
            return member
45
46
    # No member yet - try ID
47
    memID = ''.join(list(filter(str.isdigit, name)))
48
    newMem = memberForID(memID, members, me)
49
    if newMem:
50
        return newMem
51
    return None
52
53
def roleForID(id, server):
54
    for role in server.roles:
55
        if role.id == id:
56
            return role
57
    return None
58
59
def roleForName(name, server):
60
    for role in server.roles:
61
        if role.name.lower() == name.lower():
62
            return role
63
    # No role yet - try ID
64
    roleID = ''.join(list(filter(str.isdigit, name)))
65
    newRole = roleForID(roleID, server)
66
    if newRole:
67
        return newRole
68
    return None
69
70
def serverNick(user, server):
71
    for member in server.members:
72
        if member.id == user.id:
73
            return name(member)
74
    return None
75
76
def checkNameForInt(name, server):
77
    theList = name.split()
78
    # We see if we have multiple parts split by a space
79
    if len(theList)<2:
80
        # Only one part - no int included (or not separated by space)
81
        # Check if member exists - and if not throw an error, if so, throw a diff error
82
        amember = memberForName(name, server)
83 View Code Duplication
        if amember:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
84
            # We at least have a member
85
            return { "Member" : amember, "Int" : None }
86
        else:
87
            # Now we check if we got an ID instead
88
            # Get just the numbers
89
            memID = ''.join(list(filter(str.isdigit, name)))
90
            newMem = memberForID(memID, server)
91
            if newMem:
92
                # We FOUND it!
93
                return { "Member" : newMem, "Int" : None }
94
            else:
95
                # Nothing was right about this...
96
                return { "Member" : None, "Int" : None }
97
    try:
98
        # Let's cast the last item as an int and catch any exceptions
99
        theInt = int(theList[len(theList)-1])
100
        newMemberName = " ".join(theList[:-1])
101
        amember = memberForName(newMemberName, server)
102
        if amember:
103
            return { "Member" : amember, "Int" : theInt }
104
        else:
105
            # Now we check if we got an ID instead
106
            # Get just the numbers
107
            memID = ''.join(list(filter(str.isdigit, newMemberName)))
108
            newMem = memberForID(memID, server)
109
            if newMem:
110
                # We FOUND it!
111
                return { "Member" : newMem, "Int" : theInt }
112
            else:
113
                # Nothing was right about this...
114
                return { "Member" : None, "Int" : None }
115
    except ValueError:
116
        # Last section wasn't an int
117
        amember = memberForName(name, server)
118 View Code Duplication
        if amember:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
119
            # Name was just a member - return
120
            return { "Member" : amember, "Int" : None }
121
        else:
122
            # Now we check if we got an ID instead
123
            # Get just the numbers
124
            memID = ''.join(list(filter(str.isdigit, name)))
125
            newMem = memberForID(memID, server)
126
            if newMem:
127
                # We FOUND it!
128
                return { "Member" : newMem, "Int" : None }
129
            else:
130
                # Nothing was right about this...
131
                return { "Member" : None, "Int" : None }
132
    # Should never get here
133
    return None
134
135
def checkRoleForInt(name, server):
136
    theList = name.split()
137
    # We see if we have multiple parts split by a space
138
    if len(theList)<2:
139
        # Only one part - no int included (or not separated by space)
140
        # Check if role exists - and if not throw an error, if so, throw a diff error
141
        amember = roleForName(name, server)
142 View Code Duplication
        if amember:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
143
            # We at least have a member
144
            return { "Role" : amember, "Int" : None }
145
        else:
146
            # Now we check if we got an ID instead
147
            # Get just the numbers
148
            memID = ''.join(list(filter(str.isdigit, name)))
149
            newMem = roleForID(memID, server)
150
            if newMem:
151
                # We FOUND it!
152
                return { "Role" : newMem, "Int" : None }
153
            else:
154
                # Nothing was right about this...
155
                return { "Role" : None, "Int" : None }
156
    try:
157
        # Let's cast the last item as an int and catch any exceptions
158
        theInt = int(theList[len(theList)-1])
159
        newMemberName = " ".join(theList[:-1])
160
        amember = roleForName(newMemberName, server)
161
        if amember:
162
            return { "Role" : amember, "Int" : theInt }
163
        else:
164
            # Now we check if we got an ID instead
165
            # Get just the numbers
166
            memID = ''.join(list(filter(str.isdigit, newMemberName)))
167
            newMem = roleForID(memID, server)
168
            if newMem:
169
                # We FOUND it!
170
                return { "Role" : newMem, "Int" : theInt }
171
            else:
172
                # Nothing was right about this...
173
                return { "Role" : None, "Int" : None }
174
    except ValueError:
175
        # Last section wasn't an int
176
        amember = roleForName(name, server)
177 View Code Duplication
        if amember:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
178
            # Name was just a role - return
179
            return { "Role" : amember, "Int" : None }
180
        else:
181
            # Now we check if we got an ID instead
182
            # Get just the numbers
183
            memID = ''.join(list(filter(str.isdigit, name)))
184
            newMem = roleForID(memID, server)
185
            if newMem:
186
                # We FOUND it!
187
                return { "Role" : newMem, "Int" : None }
188
            else:
189
                # Nothing was right about this...
190
                return { "Role" : None, "Int" : None }
191
    # Should never get here
192
    return None
193