Passed
Push — master ( dcd1eb...4f7f97 )
by Cyb3r
02:51 queued 01:44
created

bot.cogs.tasks   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TaskCog.report_errors() 0 24 3
A TaskCog.__init__() 0 3 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 3 1
1
"""Cog for tasks that are scheduled to run"""
2
import logging
3
from datetime import datetime
4
from discord.ext import commands, tasks
5
from bot import utils
6
7
log = logging.getLogger("bot")
8
9
10
class TaskCog(commands.Cog, name="Tasks"):
11
    """
12
    Task Cog
13
14
        Cog that holds tasks for the bot
15
16
    **Tasks:**
17
        - :ref:`error_report`: Output all errors for a day to admin channels.
18
19
    """
20
21
    def __init__(self, bot):
22
        self.bot = bot
23
        self.report_errors.start()  # pylint: disable=no-member
24
25
    @tasks.loop(hours=24.0)
26
    async def report_errors(self) -> None:
27
        """
28
        Report error
29
30
        Every 24 hours, all errors for the current day are send to the admin channels.
31
32
        :return: None
33
        """
34
        date = datetime.utcnow().strftime("%Y-%m-%d")
35
        error_record = [
36
            error
37
            for error in await utils.select(
38
                "errors", "id, message, error, ack", "date_trunc('day', time)", date
39
            )
40
            if error[-1] is False
41
        ]
42
        if not error_record:
43
            errors = "No errors found for {}".format(date)
44
        else:
45
            errors = "Errors for {}.\n".format(date)
46
            for error in error_record:
47
                errors += f"- {error[0]}: {error[1]}; {error[2]}\n\n"
48
        await utils.admin_log(self.bot, errors, True)
49
50
51
def setup(bot):
52
    """Needed for extension loading"""
53
    bot.add_cog(TaskCog(bot))
54