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

api.command.is_command()   C

Complexity

Conditions 9

Size

Total Lines 49
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 49
rs 6.6666
c 0
b 0
f 0
cc 9
nop 3
1
#    Copyright 2017 Starbot Discord Project
2
# 
3
#    Licensed under the Apache License, Version 2.0 (the "License");
4
#    you may not use this file except in compliance with the License.
5
#    You may obtain a copy of the License at
6
# 
7
#        http://www.apache.org/licenses/LICENSE-2.0
8
# 
9
#    Unless required by applicable law or agreed to in writing, software
10
#    distributed under the License is distributed on an "AS IS" BASIS,
11
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
#    See the License for the specific language governing permissions and
13
#    limitations under the License.
14
15
class Command():
16
    '''Store information about a command'''
17
    def __init__(self, plugin, name, func, shortdesc='no description', devcommand=False):
18
        self.plugin:     Plugin = plugin
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Plugin does not seem to be defined.
Loading history...
19
        self.name:       str = name
20
        self.func:       function = func
21
        self.shortdesc:  str = shortdesc
22
        self.devcommand: bool = devcommand
23
24
def is_command(message_in, prefix, command):
25
    '''Check if a given message is a command'''
26
27
    # Get user.
28
    if message_in.guild:
29
        me = message_in.guild.me
30
    else:
31
        me = message_in.channel.me
32
33
    #First we check if the message starts with our prefix
34
    if message_in.content.startswith(prefix):
35
        pass
36
37
    #Otherwise, we check if the message starts with a ping for the bot
38
    elif message_in.content.startswith(me.mention):
39
        pass
40
41
    #Otherwise, the inputted message does not start with a valid bot trigger
42
    else:
43
        return False
44
45
    # The first part of the message, before the first space
46
    command_try = message_in.content.split(' ')
47
48
    # If the first part of the message is equal to the server prefix + the command name
49
    # This would be used for commands with arguments
50
    if command_try[0] == prefix + command.name:
51
        return True
52
53
    # If the entire inputted message is equal to a command
54
    # This will mean the command has no arguments
55
    elif message_in.content == prefix + command.name:
56
        return True
57
58
    # First check that the first word in the message is a mention for the bot, a.k.a. an @ ping
59
    # Then we check that the second word in the message is the name of a command
60
    # This would mean that the command has been used with arguments
61
    elif command_try[0] == me.mention and command_try[1] == command.name:
62
        return True
63
64
    # First we check that the firct word in the message is a mention for the bot
65
    # Then we check that the rest of the message is equal to the name of a command
66
    # This would mean that the command has no arguments
67
    elif message_in.content == me.mention + command.name:
68
        return True
69
70
    # We have exhausted the possibilities for running a command, so it must not be a command.
71
    else:
72
        return False
73