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

bot.cogs.misc   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A MiscCog.ping() 0 18 1
A MiscCog.contact_admin() 0 47 4
A MiscCog.__init__() 0 2 1
A MiscCog.Uptime() 0 25 1

1 Function

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