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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
import asyncio
import contextlib
import logging
import socket
from traceback import format_exc
from typing import List, Optional
import async_timeout
import discord
from aiohttp import AsyncResolver, ClientSession, TCPConnector
from discord import DiscordException, Embed
from discord.ext import commands
from bot.constants import Channels, Client
log = logging.getLogger(__name__)
__all__ = ('SeasonalBot', 'bot')
class SeasonalBot(commands.Bot):
"""Base bot instance."""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.http_session = ClientSession(
connector=TCPConnector(resolver=AsyncResolver(), family=socket.AF_INET)
)
def load_extensions(self, exts: List[str]) -> None:
"""Unload all current extensions, then load the given extensions."""
# Unload all cogs
extensions = list(self.extensions.keys())
for extension in extensions:
if extension != "bot.seasons": # We shouldn't unload the manager.
self.unload_extension(extension)
# Load in the list of cogs that was passed in here
for extension in exts:
cog = extension.split(".")[-1]
try:
self.load_extension(extension)
log.info(f'Successfully loaded extension: {cog}')
except Exception as e:
log.error(f'Failed to load extension {cog}: {repr(e)} {format_exc()}')
async def send_log(self, title: str, details: str = None, *, icon: str = None) -> None:
"""Send an embed message to the devlog channel."""
devlog = self.get_channel(Channels.devlog)
if not devlog:
log.warning("Log failed to send. Devlog channel not found.")
return
if not icon:
icon = self.user.avatar_url_as(format="png")
embed = Embed(description=details)
embed.set_author(name=title, icon_url=icon)
await devlog.send(embed=embed)
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)
@property
def member(self) -> Optional[discord.Member]:
"""Retrieves the guild member object for the bot."""
guild = bot.get_guild(Client.guild)
if not guild:
return None
return guild.me
async def set_avatar(self, url: str) -> bool:
"""Sets the bot's avatar based on a URL."""
# Track old avatar hash for later comparison
old_avatar = bot.user.avatar
image = await self._fetch_image(url)
with contextlib.suppress(discord.HTTPException, asyncio.TimeoutError):
async with async_timeout.timeout(5):
await bot.user.edit(avatar=image)
if bot.user.avatar != old_avatar:
log.debug(f"Avatar changed to {url}")
return True
log.warning(f"Changing avatar failed: {url}")
return False
async def set_banner(self, url: str) -> bool:
"""Sets the guild's banner based on the provided `url`."""
guild = bot.get_guild(Client.guild)
old_banner = guild.banner
image = await self._fetch_image(url)
with contextlib.suppress(discord.HTTPException, asyncio.TimeoutError):
async with async_timeout.timeout(5):
await guild.edit(banner=image)
new_banner = bot.get_guild(Client.guild).banner
if new_banner != old_banner:
log.debug(f"Banner changed to {url}")
return True
log.warning(f"Changing banner failed: {url}")
return False
async def set_icon(self, url: str) -> bool:
"""Sets the guild's icon based on a URL."""
guild = bot.get_guild(Client.guild)
# Track old icon hash for later comparison
old_icon = guild.icon
image = await self._fetch_image(url)
with contextlib.suppress(discord.HTTPException, asyncio.TimeoutError):
async with async_timeout.timeout(5):
await guild.edit(icon=image)
new_icon = bot.get_guild(Client.guild).icon
if new_icon != old_icon:
log.debug(f"Icon changed to {url}")
return True
log.warning(f"Changing icon failed: {url}")
return False
async def _fetch_image(self, url: str) -> bytes:
"""Retrieve an image based on a URL."""
log.debug(f"Getting image from: {url}")
async with self.http_session.get(url) as resp:
return await resp.read()
async def set_username(self, new_name: str, nick_only: bool = False) -> Optional[bool]:
"""
Set the bot username and/or nickname to given new name.
Returns True/False based on success, or None if nickname fallback also failed.
"""
old_username = self.user.name
if nick_only:
return await self.set_nickname(new_name)
if old_username == new_name:
# since the username is correct, make sure nickname is removed
return await self.set_nickname()
log.debug(f"Changing username to {new_name}")
with contextlib.suppress(discord.HTTPException):
await bot.user.edit(username=new_name, nick=None)
if not new_name == self.member.display_name:
# name didn't change, try to changing nickname as fallback
if await self.set_nickname(new_name):
log.warning(f"Changing username failed, changed nickname instead.")
return False
log.warning(f"Changing username and nickname failed.")
return None
return True
async def set_nickname(self, new_name: str = None) -> bool:
"""Set the bot nickname in the main guild."""
old_display_name = self.member.display_name
if old_display_name == new_name:
return False
log.debug(f"Changing nickname to {new_name}")
with contextlib.suppress(discord.HTTPException):
await self.member.edit(nick=new_name)
return not old_display_name == self.member.display_name
bot = SeasonalBot(command_prefix=Client.prefix)
|