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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import logging
from typing import Optional
import discord
from botcore import BotBase
from botcore.utils import scheduling
from discord import DiscordException, Embed
from discord.ext import commands
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 check_channels(self) -> None:
"""Verifies that all channel constants refer to channels which exist."""
await self.wait_until_guild_available()
if constants.Client.debug:
log.info("Skipping Channels Check.")
return
all_channels_ids = [channel.id for channel in self.get_all_channels()]
for name, channel_id in vars(constants.Channels).items():
if name.startswith("_"):
continue
if channel_id not in all_channels_ids:
log.error(f'Channel "{name}" with ID {channel_id} missing')
async def send_log(self, title: str, details: str = None, *, icon: str = None) -> None:
"""Send an embed message to the devlog channel."""
await self.wait_until_guild_available()
devlog = self.get_channel(constants.Channels.devlog)
if not devlog:
log.info(f"Fetching devlog channel as it wasn't found in the cache (ID: {constants.Channels.devlog})")
try:
devlog = await self.fetch_channel(constants.Channels.devlog)
except discord.HTTPException as discord_exc:
log.exception("Fetch failed", exc_info=discord_exc)
return
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()
await self.check_channels()
# 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))
await self.send_log(self.name, "Connected!")
|