Total Complexity | 5 |
Total Lines | 54 |
Duplicated Lines | 0 % |
Changes | 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 |