aboutsummaryrefslogtreecommitdiffstats
path: root/bot/bot.py
blob: da19bdd99362a02b9239d1ab9103caf958ea1210 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
from typing import Optional

import discord
from discord import DiscordException, Embed
from discord.ext import commands
from pydis_core import BotBase
from pydis_core.utils import scheduling

from bot import constants, exts

log = logging.getLogger(__name__)

__all__ = ("Bot", )


class Bot(BotBase):
    """
    Base bot instance.

    While in debug mode, the asset upload methods (avatar, banner, ...) will not
    perform the upload, and will instead only log the passed download urls and pretend
    that the upload was successful. See the `mock_in_debug` decorator for further details.
    """

    name = constants.Client.name

    @property
    def member(self) -> Optional[discord.Member]:
        """Retrieves the guild member object for the bot."""
        guild = self.get_guild(constants.Client.guild)
        if not guild:
            return None
        return guild.me

    async def on_command_error(self, context: commands.Context, exception: DiscordException) -> None:
        """Check command errors for UserInputError and reset the cooldown if thrown."""
        if isinstance(exception, commands.UserInputError):
            context.command.reset_cooldown(context)
        else:
            await super().on_command_error(context, exception)

    async def log_to_dev_log(self, title: str, details: str = None, *, icon: str = None) -> None:
        """Send an embed message to the dev-log channel."""
        devlog = self.get_channel(constants.Channels.devlog)

        if not icon:
            icon = self.user.display_avatar.url

        embed = Embed(description=details)
        embed.set_author(name=title, icon_url=icon)

        await devlog.send(embed=embed)

    async def setup_hook(self) -> None:
        """Default async initialisation method for discord.py."""
        await super().setup_hook()

        # This is not awaited to avoid a deadlock with any cogs that have
        # wait_until_guild_available in their cog_load method.
        scheduling.create_task(self.load_extensions(exts))

    async def invoke_help_command(self, ctx: commands.Context) -> None:
        """Invoke the help command or default help command if help extensions is not loaded."""
        if "bot.exts.core.help" in ctx.bot.extensions:
            help_command = ctx.bot.get_command("help")
            await ctx.invoke(help_command, ctx.command.qualified_name)
            return
        await ctx.send_help(ctx.command)