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

cogs.tasks   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

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