Passed
Pull Request — master (#50)
by Cyb3r
01:40
created

bot.cogs.tasks   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

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

2 Methods

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