Passed
Push — master ( 95e0ba...ca4927 )
by Cyb3r
01:11
created

tasks.setup()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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