22
22
from pymongo .errors import ConfigurationError
23
23
24
24
try :
25
+ # noinspection PyUnresolvedReferences
25
26
from colorama import init
26
27
27
28
init ()
@@ -94,7 +95,17 @@ def __init__(self):
94
95
sys .exit (0 )
95
96
96
97
self .plugin_db = PluginDatabaseClient (self )
98
+
99
+ logger .line ()
100
+ logger .info ("┌┬┐┌─┐┌┬┐┌┬┐┌─┐┬┬" )
101
+ logger .info ("││││ │ │││││├─┤││" )
102
+ logger .info ("┴ ┴└─┘─┴┘┴ ┴┴ ┴┴┴─┘" )
103
+ logger .info ("v%s" , __version__ )
104
+ logger .info ("Authors: kyb3r, fourjr, Taaku18" )
105
+ logger .line ()
106
+
97
107
self ._load_extensions ()
108
+ logger .line ()
98
109
99
110
@property
100
111
def uptime (self ) -> str :
@@ -168,14 +179,6 @@ async def get_prefix(self, message=None):
168
179
169
180
def _load_extensions (self ):
170
181
"""Adds commands automatically"""
171
- logger .line ()
172
- logger .info ("┌┬┐┌─┐┌┬┐┌┬┐┌─┐┬┬" )
173
- logger .info ("││││ │ │││││├─┤││" )
174
- logger .info ("┴ ┴└─┘─┴┘┴ ┴┴ ┴┴┴─┘" )
175
- logger .info ("v%s" , __version__ )
176
- logger .info ("Authors: kyb3r, fourjr, Taaku18" )
177
- logger .line ()
178
-
179
182
for file in os .listdir ("cogs" ):
180
183
if not file .endswith (".py" ):
181
184
continue
@@ -232,9 +235,12 @@ async def is_owner(self, user: discord.User) -> bool:
232
235
def log_channel (self ) -> typing .Optional [discord .TextChannel ]:
233
236
channel_id = self .config ["log_channel_id" ]
234
237
if channel_id is not None :
235
- channel = self .get_channel (int (channel_id ))
236
- if channel is not None :
237
- return channel
238
+ try :
239
+ channel = self .get_channel (int (channel_id ))
240
+ if channel is not None :
241
+ return channel
242
+ except ValueError :
243
+ pass
238
244
logger .debug ("LOG_CHANNEL_ID was invalid, removed." )
239
245
self .config .remove ("log_channel_id" )
240
246
if self .main_category is not None :
@@ -255,10 +261,6 @@ def log_channel(self) -> typing.Optional[discord.TextChannel]:
255
261
)
256
262
return None
257
263
258
- @property
259
- def is_connected (self ) -> bool :
260
- return self ._connected .is_set ()
261
-
262
264
async def wait_for_connected (self ) -> None :
263
265
await self .wait_until_ready ()
264
266
await self ._connected .wait ()
@@ -277,7 +279,7 @@ def token(self) -> str:
277
279
token = self .config ["token" ]
278
280
if token is None :
279
281
logger .critical (
280
- "TOKEN must be set, set this as bot token found on the Discord Dev Portal."
282
+ "TOKEN must be set, set this as bot token found on the Discord Developer Portal."
281
283
)
282
284
sys .exit (0 )
283
285
return token
@@ -289,6 +291,7 @@ def guild_id(self) -> typing.Optional[int]:
289
291
try :
290
292
return int (str (guild_id ))
291
293
except ValueError :
294
+ self .config .remove ("guild_id" )
292
295
logger .critical ("Invalid GUILD_ID set." )
293
296
return None
294
297
@@ -309,9 +312,12 @@ def modmail_guild(self) -> typing.Optional[discord.Guild]:
309
312
modmail_guild_id = self .config ["modmail_guild_id" ]
310
313
if modmail_guild_id is None :
311
314
return self .guild
312
- guild = discord .utils .get (self .guilds , id = int (modmail_guild_id ))
313
- if guild is not None :
314
- return guild
315
+ try :
316
+ guild = discord .utils .get (self .guilds , id = int (modmail_guild_id ))
317
+ if guild is not None :
318
+ return guild
319
+ except ValueError :
320
+ pass
315
321
self .config .remove ("modmail_guild_id" )
316
322
logger .critical ("Invalid MODMAIL_GUILD_ID set." )
317
323
return self .guild
@@ -325,11 +331,14 @@ def main_category(self) -> typing.Optional[discord.CategoryChannel]:
325
331
if self .modmail_guild is not None :
326
332
category_id = self .config ["main_category_id" ]
327
333
if category_id is not None :
328
- cat = discord .utils .get (
329
- self .modmail_guild .categories , id = int (category_id )
330
- )
331
- if cat is not None :
332
- return cat
334
+ try :
335
+ cat = discord .utils .get (
336
+ self .modmail_guild .categories , id = int (category_id )
337
+ )
338
+ if cat is not None :
339
+ return cat
340
+ except ValueError :
341
+ pass
333
342
self .config .remove ("main_category_id" )
334
343
logger .debug ("MAIN_CATEGORY_ID was invalid, removed." )
335
344
cat = discord .utils .get (self .modmail_guild .categories , name = "Modmail" )
@@ -353,41 +362,34 @@ def blocked_whitelisted_users(self) -> typing.List[str]:
353
362
def prefix (self ) -> str :
354
363
return str (self .config ["prefix" ])
355
364
356
- @property
357
- def mod_color (self ) -> int :
358
- color = self .config ["mod_color" ]
365
+ def _parse_color (self , conf_name ):
366
+ color = self .config [conf_name ]
359
367
try :
360
368
return int (color .lstrip ("#" ), base = 16 )
361
369
except ValueError :
362
- logger .error ("Invalid mod_color provided." )
363
- return int (self .config .remove ("mod_color" ).lstrip ("#" ), base = 16 )
370
+ logger .error ("Invalid %s provided." , conf_name )
371
+ return int (self .config .remove (conf_name ).lstrip ("#" ), base = 16 )
372
+
373
+ @property
374
+ def mod_color (self ) -> int :
375
+ return self ._parse_color ("mod_color" )
364
376
365
377
@property
366
378
def recipient_color (self ) -> int :
367
- color = self .config ["recipient_color" ]
368
- try :
369
- return int (color .lstrip ("#" ), base = 16 )
370
- except ValueError :
371
- logger .error ("Invalid recipient_color provided." )
372
- return int (self .config .remove ("recipient_color" ).lstrip ("#" ), base = 16 )
379
+ return self ._parse_color ("recipient_color" )
373
380
374
381
@property
375
382
def main_color (self ) -> int :
376
- color = self .config ["main_color" ]
377
- try :
378
- return int (color .lstrip ("#" ), base = 16 )
379
- except ValueError :
380
- logger .error ("Invalid main_color provided." )
381
- return int (self .config .remove ("main_color" ).lstrip ("#" ), base = 16 )
383
+ return self ._parse_color ("main_color" )
382
384
383
385
async def on_connect (self ):
384
386
logger .line ()
385
387
try :
386
388
await self .validate_database_connection ()
387
389
except Exception :
390
+ logger .debug ("Logging out due to failed database connection." )
388
391
return await self .logout ()
389
392
390
- logger .line ()
391
393
logger .info ("Connected to gateway." )
392
394
await self .config .refresh ()
393
395
await self .setup_indexes ()
@@ -436,6 +438,8 @@ async def on_ready(self):
436
438
logger .info ("Prefix: %s" , self .prefix )
437
439
logger .info ("Guild Name: %s" , self .guild .name )
438
440
logger .info ("Guild ID: %s" , self .guild .id )
441
+ if self .using_multiple_server_setup :
442
+ logger .info ("Receiving guild ID: %s" , self .modmail_guild .id )
439
443
logger .line ()
440
444
441
445
await self .threads .populate_cache ()
@@ -508,15 +512,16 @@ async def retrieve_emoji(self) -> typing.Tuple[str, str]:
508
512
except commands .BadArgument :
509
513
logger .warning ("Removed sent emoji (%s)." , sent_emoji )
510
514
sent_emoji = self .config .remove ("sent_emoji" )
515
+ await self .config .update ()
511
516
512
517
if blocked_emoji != "disable" :
513
518
try :
514
519
blocked_emoji = await self .convert_emoji (blocked_emoji )
515
520
except commands .BadArgument :
516
521
logger .warning ("Removed blocked emoji (%s)." , blocked_emoji )
517
522
blocked_emoji = self .config .remove ("blocked_emoji" )
523
+ await self .config .update ()
518
524
519
- await self .config .update ()
520
525
return sent_emoji , blocked_emoji
521
526
522
527
async def _process_blocked (self , message : discord .Message ) -> bool :
@@ -531,7 +536,7 @@ async def _process_blocked(self, message: discord.Message) -> bool:
531
536
try :
532
537
await message .add_reaction (sent_emoji )
533
538
except (discord .HTTPException , discord .InvalidArgument ):
534
- pass
539
+ logger . warning ( "Failed to add sent_emoji." , exc_info = True )
535
540
536
541
return False
537
542
@@ -761,6 +766,7 @@ async def get_context(self, message, *, cls=commands.Context):
761
766
async def update_perms (
762
767
self , name : typing .Union [PermissionLevel , str ], value : int , add : bool = True
763
768
) -> None :
769
+ value = int (value )
764
770
if isinstance (name , PermissionLevel ):
765
771
permissions = self .config ["level_permissions" ]
766
772
name = name .name
@@ -1043,19 +1049,6 @@ async def on_command_error(self, context, exception):
1043
1049
else :
1044
1050
logger .error ("Unexpected exception:" , exc_info = exception )
1045
1051
1046
- @staticmethod
1047
- def overwrites (ctx : commands .Context ) -> dict :
1048
- """Permission overwrites for the guild."""
1049
- overwrites = {
1050
- ctx .guild .default_role : discord .PermissionOverwrite (read_messages = False ),
1051
- ctx .guild .me : discord .PermissionOverwrite (read_messages = True ),
1052
- }
1053
-
1054
- for role in ctx .guild .roles :
1055
- if role .permissions .administrator :
1056
- overwrites [role ] = discord .PermissionOverwrite (read_messages = True )
1057
- return overwrites
1058
-
1059
1052
async def validate_database_connection (self ):
1060
1053
try :
1061
1054
await self .db .command ("buildinfo" )
@@ -1113,12 +1106,14 @@ async def before_post_metadata(self):
1113
1106
if not self .guild :
1114
1107
self .metadata_loop .cancel ()
1115
1108
1116
- async def after_post_metadata (self ):
1109
+ @staticmethod
1110
+ async def after_post_metadata ():
1117
1111
logger .info ("Metadata loop has been cancelled." )
1118
1112
1119
1113
1120
1114
if __name__ == "__main__" :
1121
1115
try :
1116
+ # noinspection PyUnresolvedReferences
1122
1117
import uvloop
1123
1118
1124
1119
logger .debug ("Setting up with uvloop." )
0 commit comments