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

bot.cogs.misc.setup()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 3
rs 10
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
        :return: None
37
        """
38
        log.debug("{} has sent ping.".format(ctx.author.name))
39
        await ctx.message.delete()
40
        embed = await utils.make_embed(ctx, send=False, title="PONG")
41
        url = "https://peterfrezzini.com/content/images/2016/12/pong_logo.jpg"
42
        embed.set_image(url=url)
43
        embed.set_footer(text="Image from https://peterfrezzini.com/pong-game-cover/")
44
        await ctx.author.send(embed=embed)
45
46
    @commands.command(name="uptime", help="Gets uptime of bot")
47
    async def uptime(self, ctx: commands.Context) -> None:
48
        """
49
        Uptime
50
51
        Command that shows how long the bot has been online
52
53
        :param ctx: Command context
54
        :return: None
55
        """
56
        uptime = datetime.utcnow() - self.bot.uptime
57
        uptime = ":clock1: Days: {}, Hours: {}, Minutes: {}, Seconds: {}".format(
58
            uptime.days,
59
            uptime.seconds // 3600,  # Hours
60
            (uptime.seconds // 60) % 60,  # Minutes
61
            uptime.seconds % 60,  # Seconds
62
        )
63
64
        start_time = self.bot.uptime.strftime("%Y-%m-%d %H:%M")
65
        list_string_time = self.bot.list_updated.strftime("%Y-%m-%d %H:%M")
66
        description = "Bot has been online since {} UTC\n School list last updated {}".format(
67
            start_time, list_string_time
68
        )
69
70
        await utils.make_embed(
71
            ctx, title=uptime, description=description, footer=self.bot.__version__
72
        )
73
74
    @commands.command(
75
        name="report",
76
        aliases=["contact-admin"],
77
        help="Reporting feature.\n"
78
        "Use if you are experiencing issues with the bot or in the server.",
79
    )
80
    async def report(self, ctx: commands.Context, *, message: str) -> None:
81
        """Report
82
83
        reporting command. When triggered, member will be prompted for a reason. Message will then
84
        be sent to all bot admins.
85
86
        :param ctx: Command context
87
        :param message: Report message sent
88
        :type message: str
89
        :return: None
90
        """
91
        reports = await utils.fetch("reports", "id")
92
        report_id = random.randint(1, 32767)  # nosec
93
        while report_id in reports:
94
            log.warning("report_id had to be regenerated")
95
            report_id = random.randint(1, 32767)  # nosec
96
        await utils.insert(
97
            "reports",
98
            [
99
                report_id,
100
                (ctx.author.name + ctx.author.discriminator),
101
                ctx.author.id,
102
                message,
103
                datetime.utcnow(),
104
            ],
105
        )
106
        channels = await utils.select("admin_channels", "id", "log", "f")
107
        for channel in channels:
108
            to_send = self.bot.get_channel(channel)
109
            if to_send is None:
110
                log.warning("No channel found for id {}".format(channel))
111
            await utils.make_embed(
112
                ctx,
113
                title="New Report",
114
                description="{} submitted the report:\n> {}".format(ctx.author.name, message),
115
            )
116
117
        response_msg = (
118
            "The admins have received your report.\nThey will investigation and may reach out"
119
        )
120
        await utils.make_embed(ctx, title="Report Received", description=response_msg)
121
122
123
def setup(bot):
124
    """Needed for extension loading"""
125
    bot.add_cog(MiscCog(bot))
126