Passed
Pull Request — master (#50)
by Cyb3r
01:19 queued 15s
created

bot.cogs.tasks.TaskCog.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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