-
Notifications
You must be signed in to change notification settings - Fork 0
/
guild_config.py
280 lines (235 loc) · 11.5 KB
/
guild_config.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
Library of some sort for making GuildConfig clients.
Describes GuildConfig interface that can be used in other modules.
Describes GuildConfigCog methods that GuildConfig class uses to get and set data.
"""
from dataclasses import dataclass
from enum import Enum
import sys
import traceback
from typing import Dict, Iterable, List, Optional, Tuple
import discord
from discord.ext import commands
IMMUTABLE_COMMANDS = ('command', 'config', 'say', 'say_dm')
INFO_CHANNEL_TYPES = ('welcome', 'log', 'mod-list') # reports are not yet implemented
# TODO: mod-list list, time interval customization
# TODO: Module that stores data in separate channel in the discord guild itself.
# TODO: Localization?
# TODO: Configurable messages (for example, on memeber join)
# Enums
class CommandDisability(Enum):
NONE = 0
GLOBAL = 1
BLACKLISTED = 2
WHITELISTED = 3
# Errors/Exceptions
class GuildConfigError(commands.errors.CommandError):
pass
class InfoChannelNotFoundError(GuildConfigError):
def __init__(self, ic_name: str):
self.ic_name = ic_name
class CommandDisabledError(GuildConfigError):
def __init__(self, guild: discord.Guild, command_name: str, channel: discord.TextChannel, disability: CommandDisability):
self.guild = guild
self.command_name = command_name
self.channel = channel
self.disability = disability
# Raised if bot doesn't have this command
class CommandNotFoundError(GuildConfigError):
def __init__(self, name: str):
self.name = name
# Raised if command can't be filtered
class CommandImmutableError(GuildConfigError):
def __init__(self, name: str):
self.name = name
# Data structures
@dataclass(frozen=True)
class CommandFilter:
name: str
filter_type: CommandDisability
filter_list: List[discord.TextChannel]
enabled: bool
guild: discord.Guild
# TypedDict is not available in Python 3.7
# class InfoChannelSpec(TypedDict):
# channel: discord.TextChannel = None
# enabled: bool = None
def default_guild_config_data(guild: discord.Guild) -> dict:
default_channel = guild.system_channel.id if guild.system_channel is not None else guild.text_channels[0].id # type: ignore
guild_config_data = {
"guild_name": guild.name,
"default_roles": [],
"info_channels": {
"welcome": {
"channel_id": default_channel,
"enabled": False
},
"log": {
"channel_id": default_channel,
"enabled": False
},
"mod-list": {
"channel_id": default_channel,
"enabled": False
}
}
}
return guild_config_data
# Updates DB info by itself, can be easily cached
class GuildConfig:
guild: discord.Guild
def __init__(self, guild: discord.Guild, guild_config_data: dict, guild_config_cog):
# guild_config_data type is assumed to be dict but not always is.
self.guild = guild
self.raw_data = guild_config_data
self.guild_config_cog = guild_config_cog
# Update info channel's specs.
async def update_info_channel(self, ic_name: str, state: Optional[bool] = None, new_channel: Optional[discord.TextChannel] = None):
if ic_name in INFO_CHANNEL_TYPES:
update_data = {}
if state is not None:
update_data['enabled'] = state
if new_channel is not None:
update_data['channel'] = new_channel
if update_data != {}:
new_data = await self.guild_config_cog.update_guild(self.guild, info_channels={ic_name: update_data})
if new_data is not None:
self.raw_data = new_data
else:
raise InfoChannelNotFoundError(ic_name)
# Get discord.TextChannel for welcome channel
async def get_welcome_channel(self) -> Optional[discord.TextChannel]:
if self.raw_data['info_channels']['welcome']['enabled']:
return self.guild.get_channel(self.raw_data['info_channels']['welcome']['channel_id'])
else:
return None
# Set the welcome channel
async def set_welcome_channel(self, new_channel: discord.TextChannel):
await self.update_info_channel('welcome', new_channel=new_channel)
# Enable welcome channel
async def enable_welcome_channel(self):
await self.update_info_channel('welcome', state=True)
# Disable welcome channel
async def disable_welcome_channel(self):
await self.update_info_channel('welcome', state=False)
# Below all the same for log channel
async def get_log_channel(self) -> Optional[discord.TextChannel]:
if self.raw_data['info_channels']['log']['enabled']:
return self.guild.get_channel(self.raw_data['info_channels']['log']['channel_id'])
else:
return None
async def set_log_channel(self, new_channel: discord.TextChannel):
await self.update_info_channel('log', new_channel=new_channel)
async def enable_log_channel(self):
await self.update_info_channel('log', state=True)
async def disable_log_channel(self):
await self.update_info_channel('log', state=False)
# Below all the same for mod-list channel
async def get_modlist_channel(self) -> Optional[discord.TextChannel]:
if self.raw_data['info_channels']['log']['enabled']:
return self.guild.get_channel(self.raw_data['info_channels']['mod-list']['channel_id'])
else:
return None
async def set_modlist_channel(self, new_channel: discord.TextChannel):
await self.update_info_channel('mod-list', new_channel=new_channel)
async def enable_modlist_channel(self):
await self.update_info_channel('mod-list', state=True)
async def disable_modlist_channel(self):
await self.update_info_channel('mod-list', state=False)
# Get default roles. Return empty list if no roles were set
async def get_default_roles(self) -> List[discord.Role]:
roles = []
for role_id in self.raw_data['default_roles']:
role = self.guild.get_role(role_id)
if role is not None:
roles.append(role)
return roles
# Set default roles. Passing an empty iterable results in removing all default roles.
async def set_default_roles(self, new_roles: Iterable[discord.Role]):
new_data = await self.guild_config_cog.update_guild(self.guild, default_roles=new_roles)
if new_data is not None:
self.raw_data = new_data
# Unimplemented
# def add_xp(self, member: discord.Member, xp_count: int):
# if str(member.id) not in self.raw['members']:
# self.raw['members'][str(member.id)] = 0
# self.raw['members'][str(member.id)] += xp_count
# self.write()
# def get_xp(self, member: discord.Member) -> int:
# return self.raw['members'][str(member.id)]
# Maybe switch to abc meta-class magic?
# Abstract base class for all GuildConfigCog modules. Defines some methods that need to be overriden and adds some checks.
class AbstractGuildConfigCog(commands.Cog):
__gc_cache: Dict[int, GuildConfig] = dict() # Guild ID is key, GuildConfig is the item
__cf_cache: Dict[Tuple[int, str], CommandFilter] = dict() # Tuple of Guild ID and command name is key, CommandFilter is the item
bot: commands.Bot
name = 'GuildConfigCog' # Should not be changed!
gc_type: str = '' # Define the Cog type. FOr example: 'mongo', 'json'
# CommandFilter check
# Suggestion: remove "enabled" property and use CommandDisability.GLOBAL insted.
async def bot_check_once(self, ctx: commands.Context):
# A lot of None checks because of pyright
if (ctx.guild is not None) and (ctx.channel is not None) and (ctx.command is not None):
# Don't bother to check the command filter if command can't be filtered
if ctx.command.name in IMMUTABLE_COMMANDS:
return True
else:
command_filter = await self.get_command_filter(ctx.guild, ctx.command.name) # type: ignore
# Command filter isn't set up, so it's allowed to run
if command_filter is None:
return True
else:
# If command is disabled on the server (guild) globally, it's not allowed to run
if not command_filter.enabled:
raise CommandDisabledError(ctx.guild, ctx.command.name, ctx.channel, CommandDisability.GLOBAL) # type: ignore
# If command was called in a blacklisted channel, it's not allowed to run
if command_filter.filter_type == CommandDisability.BLACKLISTED and ctx.channel in command_filter.filter_list:
raise CommandDisabledError(ctx.guild, ctx.command.name, ctx.channel, command_filter.filter_type) # type: ignore
# If command wasn't called in a whitelisted channel, it's not allowed to run
if command_filter.filter_type == CommandDisability.WHITELISTED and ctx.channel not in command_filter.filter_list:
raise CommandDisabledError(ctx.guild, ctx.command.name, ctx.channel, command_filter.filter_type) # type: ignore
# Default
return True
# Handle CommandDisableError
@commands.Cog.listener()
async def on_command_error(self, ctx:commands.Context, exc: commands.errors.CommandError):
# For now, just ignore if command is disabled
# TODO: message for when command is disabled
# send the message in DM, also would need an opt-out system, so UserConfig will probably need to be brought back...
if isinstance(exc, CommandDisabledError):
return
else:
# The default behaviour. I fell like there is a better way to do this. It was copied from discord.py source
print(f'Ignoring exception in command {ctx.command}:', file=sys.stderr)
traceback.print_exception(type(exc), exc, exc.__traceback__, file=sys.stderr)
# I have a feeling that this is unnecessary
def teardown(self):
self.__gc_cache.clear()
self.__cf_cache.clear()
# Get GuildConfig for given Guild.
# Decide wheter next this one or the next is correct
async def get_config(self, guild: discord.Guild) -> GuildConfig: # type: ignore
pass
async def get_guild(self, guild: discord.Guild) -> GuildConfig:
return await self.get_config(guild)
# Update GuildConfig in the DB
async def update_guild(self,
guild: discord.Guild,
default_roles: List[discord.Role] = None,
info_channels: Dict[str, dict] = None) -> dict: # type: ignore
pass
# Add a new GuildConfig to the DB
async def add_guild(self, guild: discord.Guild):
pass
# Get a CommandFilter from the DB
async def get_command_filter(self, guild: discord.Guild, name: str) -> CommandFilter: # type: ignore
pass
# Update CommandFilter in the DB
async def update_command_filter(self,
guild: discord.Guild,
name: str,
new_channels: List[discord.TextChannel] = None,
append_channels: bool = False,
enabled: bool = None,
filter_type: CommandDisability = None) -> dict: # type: ignore
pass