misc   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

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