Passed
Push — master ( 58f506...9afc6c )
by Cyb3r
01:28 queued 11s
created

cogs.errors   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 9

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 3 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ErrorsCog.__init__() 0 3 1
B ErrorsCog.on_command_error() 0 45 7
1
"""Cogs that handles errors"""
2
import logging
3
import random
4
from datetime import datetime
5
from discord.ext import commands
6
import utils
7
8
9
class ErrorsCog(commands.Cog, name="Errors"):
10
    """ErrorsCogs
11
    ---
12
13
    Arguments:
14
        commands {[type]} -- [description]
15
16
    Arguments:
17
    ---
18
        bot {discord.commands.Bot} -- The bot
19
    """
20
    def __init__(self, bot):
21
        self.bot = bot
22
        self.log = logging.getLogger("bot")
23
24
    @commands.Cog.listener()
25
    async def on_command_error(self, ctx, error):
26
        """Reports errors to users"""
27
        if isinstance(error, (commands.errors.MissingRole, commands.errors.CheckFailure)):
28
            error_msg = "You do not have the correct role for this command."
29
        elif isinstance(error, commands.errors.CommandNotFound):
30
            error_msg = "{} is not valid.\nPlease use `$help` to find valid commands.".format(
31
                ctx.message.content)
32
        elif isinstance(error, commands.MissingRequiredArgument):
33
            error_msg = "`{}` has missing required arguments".format(ctx.message.content)
34
        elif isinstance(error, commands.errors.CommandError):
35
            errors = await utils.fetch("errors", "id")
36
            errorID = random.randint(1, 32767)  # nosec
37
            while errorID in errors:
38
                self.log.debug("Error ID had to be regenerated")
39
                errorID = random.randint(1, 32767)  # nosec
40
41
            error_info = [
42
                errorID,
43
                ctx.message.content,
44
                "COG: {} COMMAND: {}".format(ctx.command.cog.qualified_name, ctx.command.name),
45
                str(error),
46
                datetime.utcnow()]
47
48
            self.log.error(error_info)
49
            error_msg = ("There was a command error.\n"
50
                         "Please report it for investgation.\n"
51
                         "Error #{}".format(errorID))
52
            await utils.insert("errors", error_info)
53
        else:
54
            errors = await utils.fetch("errors", "id")
55
            errorID = random.randint(1, 32767)  # nosec
56
            while errorID in errors:
57
                self.log.debug("Error ID had to be regenerated")
58
                errorID = random.randint(1, 32767)  # nosec
59
60
            self.log.error((errorID, error))
61
            error_msg = ("There was an unknown error.\n"
62
                         "Please report it for investigation.\n"
63
                         "Error #{}".format(errorID))
64
            self.log.error("There was the following error: {}".format(error))
65
            await utils.insert("errors", error_info)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable error_info does not seem to be defined.
Loading history...
66
67
        await utils.make_embed(ctx, "FF0000", title="Error:",
68
                               description=error_msg)
69
70
71
def setup(bot):
72
    """Needed for extension loading"""
73
    bot.add_cog(ErrorsCog(bot))
74