Passed
Push — master ( dcd1eb...4f7f97 )
by Cyb3r
02:51 queued 01:44
created

bot.cogs.misc.MiscCog.report()   A

Complexity

Conditions 4

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 30
nop 4
dl 0
loc 48
rs 9.16
c 0
b 0
f 0
1
"""Misc features cog for CCC Bot"""
2
import random
3
from datetime import datetime
4
import logging
5
from discord.ext import commands
6
from bot import utils
7
8
log = logging.getLogger("bot")
9
10
11
class MiscCog(commands.Cog, name="Misc"):
12
    """Misc Cog
13
14
    Cog that deal with misc features contains ping, report and uptime.
15
16
    **Commands:**
17
        - `ping`: Testing command that will DM the member pong and then delete the ping message.
18
19
        - `report`: Report command. When triggered it will ask the member for a reason then ping
20
                    all admins with the message. *Might need to be disabled for spam*
21
22
        - `uptime`: Lists uptime for bot and when it was started.
23
24
    """
25
26
    def __init__(self, bot):
27
        self.bot = bot
28
29
    @commands.command(name="ping", help="Testing command that returns pong", hidden=True)
30
    async def ping(self, ctx: commands.Context) -> None:
31
        """Ping
32
33
        Testing command that will message the author pong and delete the author's ping message.
34
35
        :param ctx: Command context
36
        :type ctx: discord.ext.commands.Context
37
        :return: None
38
        """
39
        log.debug("{} has sent ping.".format(ctx.author.name))
40
        await ctx.message.delete()
41
        embed = await utils.make_embed(ctx, send=False, title="PONG")
42
        url = "https://peterfrezzini.com/content/images/2016/12/pong_logo.jpg"
43
        embed.set_image(url=url)
44
        embed.set_footer(text="Image from https://peterfrezzini.com/pong-game-cover/")
45
        await ctx.author.send(embed=embed)
46
47
    @commands.command(name="uptime", help="Gets uptime of bot")
48
    async def uptime(self, ctx: commands.Context) -> None:
49
        """
50
        Uptime
51
52
        Command that shows how long the bot has been online
53
54
        :param ctx: Command context
55
        :type ctx: discord.ext.commands.Context
56
        :return: None
57
        """
58
        uptime = datetime.utcnow() - self.bot.uptime
59
        uptime = ":clock1: Days: {}, Hours: {}, Minutes: {}, Seconds: {}".format(
60
            uptime.days,
61
            uptime.seconds // 3600,  # Hours
62
            (uptime.seconds // 60) % 60,  # Minutes
63
            uptime.seconds % 60,  # Seconds
64
        )
65
66
        start_time = self.bot.uptime.strftime("%Y-%m-%d %H:%M")
67
        list_string_time = self.bot.list_updated.strftime("%Y-%m-%d %H:%M")
68
        description = "Bot has been online since {} UTC\n School list last updated {}".format(
69
            start_time, list_string_time
70
        )
71
72
        await utils.make_embed(
73
            ctx, title=uptime, description=description, footer=self.bot.__version__
74
        )
75
76
    @commands.command(
77
        name="report",
78
        aliases=["contact-admin"],
79
        help="Reporting feature.\n"
80
        "Use if you are experiencing issues with the bot or in the server.",
81
    )
82
    async def report(self, ctx: commands.Context, *, message: str) -> None:
83
        """Report
84
85
        reporting command. When triggered, member will be prompted for a reason. Message will then
86
        be sent to all bot admins.
87
88
        :param ctx: Command context
89
        :type ctx: discord.ext.commands.Context
90
        :param message: Report message sent
91
        :type message: str
92
        :return: None
93
        """
94
        reports = await utils.fetch("reports", "id")
95
        report_id = random.randint(1, 32767)  # nosec
96
        while report_id in reports:
97
            log.warning("report_id had to be regenerated")
98
            report_id = random.randint(1, 32767)  # nosec
99
        await utils.insert(
100
            "reports",
101
            [
102
                report_id,
103
                (ctx.author.name + ctx.author.discriminator),
104
                ctx.author.id,
105
                message,
106
                datetime.utcnow(),
107
            ],
108
        )
109
        channels = await utils.select("admin_channels", "id", "log", "f")
110
        for channel in channels:
111
            to_send = self.bot.get_channel(channel)
112
            if to_send is None:
113
                log.warning("No channel found for id {}".format(channel))
114
            await utils.make_embed(
115
                ctx,
116
                title="New Report",
117
                description="{} submitted the report:\n> {}".format(ctx.author.name, message),
118
            )
119
120
        response_msg = (
121
            "The admins have received your report.\nThey will investigation and may reach out"
122
        )
123
        await utils.make_embed(ctx, title="Report Received", description=response_msg)
124
125
126
def setup(bot):
127
    """Needed for extension loading"""
128
    bot.add_cog(MiscCog(bot))
129