This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
logging.py
586 lines (565 loc) · 22.3 KB
/
logging.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
"""This cog will handle logging all server actions to a specific channel."""
import discord
from discord.ext import commands
from .utils import checks, embeds
class Logging(commands.Cog):
"""General logging cog for guild."""
def __init__(self, bot):
"""Init method."""
super().__init__()
self.bot = bot
self.logger = bot.logger
@commands.group(hidden=True, aliases=['ldbc', 'get_these_errors_outta_here']) # noqa
@commands.is_owner()
async def log_db_cleaning(self, ctx):
"""Clean a deleted channel from the voice log and server log databases.""" # noqa
embed_title = f'Database Cleaning Tool'
if ctx.subcommand_passed is None:
local_embed = discord.Embed(
title=embed_title,
description="""Check the console for channel
errors and pass them as so:\n
ldbc *channel snowflake id*""",
color=0xCCCCCC
)
await ctx.send(embed=local_embed)
return
remove_id = ctx.subcommand_passed
was_log_removed = False
was_voice_removed = False
for guild in self.bot.guilds:
log_channel = await self.bot.pg_utils.get_logger_channels(guild.id)
voice_channel = await self.bot.pg_utils.get_voice_channels(guild.id) # noqa
for remove_id in log_channel:
await self.bot.pg_utils.rem_logger_channel(
guild.id, remove_id, self.bot.logger
)
was_log_removed = True
for remove_id in voice_channel:
await self.bot.pg_utils.rem_voice_channel(
guild.id, remove_id, self.bot.logger
)
was_voice_removed = True
if was_log_removed or was_voice_removed:
local_embed = discord.Embed(
title=embed_title,
description=f'Channel was removed from database',
color=0x419400
)
await ctx.send(embed=local_embed)
return
local_embed = discord.Embed(
title=embed_title,
description="""Channel not found in database,
or you did not give a valid channel id""",
color=0x651111
)
await ctx.send(embed=local_embed)
@commands.group()
@commands.guild_only()
@checks.is_admin()
async def logging(self, ctx):
"""Enable and disable logging to channel."""
if ctx.invoked_subcommand is None:
desc = ''
modlogs = await self.bot.pg_utils.get_logger_channels(
ctx.guild.id)
for channel in ctx.guild.channels:
if channel.id in modlogs:
desc += f'{channel.name} \n'
local_embed = discord.Embed(
title=f'Current log channel list is: ',
description=desc,
color=0x419400
)
await ctx.send(embed=local_embed)
@logging.command()
async def enable(self, ctx):
"""Add channel to the log channel list."""
added_channels = []
desc = ''
try:
success = await \
self.bot.pg_utils.add_logger_channel(
ctx.guild.id, ctx.message.channel.id, self.bot.logger
)
if success:
added_channels.append(ctx.message.channel.name)
if added_channels:
for channel in added_channels:
desc += f'{channel} \n'
local_embed = discord.Embed(
title=f'Channels added to log channel list:',
description=desc,
color=0x419400
)
self.bot.server_settings[ctx.guild.id]['logging_enabled']\
= True
else:
local_embed = discord.Embed(
title=f'Internal error, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(f'Error adding channels {e}')
local_embed = discord.Embed(
title=f'Internal issue, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
@logging.command(aliases=['rem'])
async def disable(self, ctx):
"""Remove channel from the log channel list."""
removed_channels = []
absent_channels = []
desc = ''
try:
try:
success = False
success = await \
self.bot.pg_utils.rem_logger_channel(
ctx.guild.id, ctx.message.channel.id, self.bot.logger
)
except ValueError:
absent_channels.append(ctx.message.channel.name)
if success:
removed_channels.append(ctx.message.channel.name)
if removed_channels:
for channel in removed_channels:
desc += f'{channel} \n'
local_embed = discord.Embed(
title=f'Channels removed from log channel list:',
description=desc,
color=0x419400
)
logs = await self.bot.pg_utils.get_logger_channels(
ctx.guild.id)
if not logs:
self.bot.server_settings[ctx.guild.id]['logging_enabled']\
= False
if absent_channels:
desc = ''
for channel in absent_channels:
desc += f'{channel}\n'
local_embed.add_field(
name='Channels not in log channel list :',
value=desc
)
elif absent_channels:
desc = ''
for channel in absent_channels:
desc += f'{channel}\n'
local_embed = discord.Embed(
title=f'Channels not in log channel list: ',
description=desc,
color=0x651111
)
else:
local_embed = discord.Embed(
title=f'Internal error, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
except Exception as e:
self.bot.logger.warning(f'Issue: {e}')
local_embed = discord.Embed(
title=f'Internal issue, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
@commands.group(aliases=['vclogs', 'prescence_logging'])
@commands.guild_only()
@checks.is_admin()
async def voice_logging(self, ctx):
"""Enable and disable logging to channel."""
if ctx.invoked_subcommand is None:
desc = ''
voicelogs = await self.bot.pg_utils.get_voice_channels(
ctx.guild.id)
for channel in ctx.guild.channels:
if channel.id in voicelogs:
desc += f'{channel.name} \n'
local_embed = discord.Embed(
title=f'Current voice log channel list is: ',
description=desc,
color=0x419400
)
await ctx.send(embed=local_embed)
@voice_logging.command(name='enable')
async def _enable(self, ctx):
"""Add channel to the voice log channel list."""
added_channels = []
desc = ''
try:
success = await \
self.bot.pg_utils.add_voice_channel(
ctx.guild.id, ctx.message.channel.id, self.bot.logger
)
if success:
added_channels.append(ctx.message.channel.name)
if added_channels:
for channel in added_channels:
desc += f'{channel} \n'
local_embed = discord.Embed(
title=f'Channels added to voice log channel list:',
description=desc,
color=0x419400
)
else:
local_embed = discord.Embed(
title=f'Internal error, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(f'Error adding channels {e}')
local_embed = discord.Embed(
title=f'Internal issue, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
@voice_logging.command(name='disable', aliases=['rem'])
async def _disable(self, ctx):
"""Remove channel from the voice log channel list."""
removed_channels = []
absent_channels = []
desc = ''
try:
try:
success = False
success = await \
self.bot.pg_utils.rem_voice_channel(
ctx.guild.id, ctx.message.channel.id, self.bot.logger
)
except ValueError:
absent_channels.append(ctx.message.channel.name)
if success:
removed_channels.append(ctx.message.channel.name)
if removed_channels:
for channel in removed_channels:
desc += f'{channel} \n'
local_embed = discord.Embed(
title=f'Channels removed from voice log channel list:',
description=desc,
color=0x419400
)
if absent_channels:
desc = ''
for channel in absent_channels:
desc += f'{channel}\n'
local_embed.add_field(
name='Channels not in voice log channel list :',
value=desc
)
elif absent_channels:
desc = ''
for channel in absent_channels:
desc += f'{channel}\n'
local_embed = discord.Embed(
title=f'Channels not in voice log channel list: ',
description=desc,
color=0x651111
)
else:
local_embed = discord.Embed(
title=f'Internal error, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
except Exception as e:
self.bot.logger.warning(f'Issue: {e}')
local_embed = discord.Embed(
title=f'Internal issue, please contact @dashwav#7785',
description=' ',
color=0x651111
)
await ctx.send(embed=local_embed)
@commands.Cog.listener()
async def on_member_ban(self, guild, user):
"""Send a message on user ban."""
if not self.bot.server_settings[guild.id]['logging_enabled']:
return
channels = await self.bot.pg_utils.get_logger_channels(
guild.id)
local_embed = embeds.LogBanEmbed(user)
for channel in channels:
try:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging user ban in channel {channel}'
f', error: {e}'
)
@commands.Cog.listener()
async def on_member_join(self, member):
"""Send a message on a user join."""
if not self.bot.server_settings[member.guild.id]['logging_enabled']:
return
channels = await self.bot.pg_utils.get_logger_channels(
member.guild.id)
local_embed = embeds.JoinEmbed(member)
for channel in channels:
try:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging user join in channel {channel}'
f', error: {e}'
)
@commands.Cog.listener()
async def on_member_remove(self, member):
"""Send message on a user leaving."""
if not self.bot.server_settings[member.guild.id]['logging_enabled']:
return
channels = await self.bot.pg_utils.get_logger_channels(
member.guild.id)
local_embed = embeds.LeaveEmbed(member)
for channel in channels:
try:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging user leave in channel {channel}'
f', error: {e}'
)
@commands.Cog.listener()
async def on_message_edit(self, before, after):
"""Send message on a user editing messages."""
if not self.bot.server_settings[before.guild.id]['logging_enabled']:
return
try:
if not before.content.strip() != after.content.strip():
return
channels = await self.bot.pg_utils.get_logger_channels(
before.guild.id)
try:
local_embed = embeds.MessageEditEmbed(
before.author,
before.channel.name,
before.content,
after.content
)
try:
for channel in channels:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.warning(
f'Issue logging message edit in channel {channel}'
f', error: {e}'
)
except Exception as e:
self.bot.logger.warning(
f'Issue making embed for channel {channel}'
f', error: {e}'
)
except AttributeError:
pass
@commands.Cog.listener()
async def on_message_delete(self, message):
"""Send message on a user editing messages."""
if not self.bot.server_settings[message.guild.id]['logging_enabled']:
return
if message.author.bot:
return
channels = await self.bot.pg_utils.get_logger_channels(
message.guild.id)
try:
local_embed = embeds.MessageDeleteEmbed(
message.author,
message.channel.name,
message.content,
)
try:
for channel in channels:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.warning(
f'Issue logging message delete in channel {channel}'
f', error: {e}'
)
except Exception as e:
self.bot.logger.warning(
f'Issue making embed for channel {channel}'
f', error: {e}'
)
@commands.Cog.listener()
async def on_member_update(self, before, after):
"""Send message on a user role or name update."""
if not self.bot.server_settings[before.guild.id]['logging_enabled']:
return
if before.roles == after.roles:
return
channels = await self.bot.pg_utils.get_logger_channels(
before.guild.id)
role_diff = set(after.roles) - (set(before.roles))
for role in role_diff:
local_embed = embeds.RoleAddEmbed(
after,
role.name
)
for channel in channels:
try:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging role change'
f' in channel {channel}, error: {e}'
)
role_diff = set(before.roles) - (set(after.roles))
for role in role_diff:
local_embed = embeds.RoleRemoveEmbed(
after,
role.name
)
for channel in channels:
try:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging role remove in'
f' channel {channel}, error: {e}'
)
@commands.Cog.listener()
async def on_user_update(self, before, after):
"""Send message on a username update."""
if before.name == after.name:
return
user_mutuals = []
for guild in self.bot.guilds:
if before in guild.members:
user_mutuals.append(guild.id)
extended_channels = []
for guild_id in user_mutuals:
extended_channels.extend(
await self.bot.pg_utils.get_logger_channels(
guild_id))
local_embed = embeds.UsernameUpdateEmbed(
after, before.name, after.name)
for channel in extended_channels:
try:
ch = self.bot.get_channel(channel)
await ch.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging name change'
f' in channel {channel}, error {e}'
)
@commands.Cog.listener()
async def on_voice_state_update(self, member, before, after):
"""Send a message on user vc update."""
vc_logging = await self.bot.pg_utils.get_voice_logging(
member.guild.id)
if not vc_logging:
return
vc_channels = await self.bot.pg_utils.get_voice_channels(
member.guild.id
)
if before.channel is None and after.channel:
local_embed = embeds.VoiceChannelStateEmbed(
member, after.channel, 'joined'
)
for channel in vc_channels:
try:
channel = self.bot.get_channel(channel)
await channel.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging voice join in'
f' channel {channel}, error: {e}'
)
elif after.channel is None and before.channel:
local_embed = embeds.VoiceChannelStateEmbed(
member, before.channel, 'left'
)
for channel in vc_channels:
try:
channel = self.bot.get_channel(channel)
await channel.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging voice leave in'
f' channel {channel}, error: {e}'
)
elif before.channel != after.channel:
local_embed = embeds.VoiceChannelMoveEmbed(
member, before.channel, after.channel
)
for channel in vc_channels:
try:
channel = self.bot.get_channel(channel)
await channel.send(embed=local_embed)
except Exception as e:
self.bot.logger.info(
f'Error logging voice move in'
f' channel {channel}, error: {e}'
)
@commands.Cog.listener()
async def on_guild_channel_delete(self, channel):
"""Attempt to remove deleted channel from the logging databases."""
logger = await self.bot.pg_utils.get_logger_channels(channel.guild.id)
vlogger = await self.bot.pg_utils.get_voice_channels(channel.guild.id)
self.bot.logger.info(channel.id)
self.bot.logger.info(logger)
self.bot.logger.info(vlogger)
channel_type = -1
if channel.id in logger and channel.id in vlogger:
channel_type = 0
elif channel.id in logger:
channel_type = 1
elif channel.id in vlogger:
channel_type = 2 # noqa
else:
return
server_del = channel.guild.id
if (channel_type % 2) <= 1:
try:
success = False
success = await \
self.bot.pg_utils.rem_logger_channel(
server_del, channel, self.bot.logger
)
if success:
self.bot.logger.info(
f'Channel deleted from server {server_del}'
f', removed from log db'
)
except Exception as e:
self.bot.logger.info(
f"""Issue removing channel {channel} from log database
after deletion error: {e}"""
)
if (channel_type % 2) == 0:
try:
success = False
success = await \
self.bot.pg_utils.rem_voice_channel(
server_del, channel, self.bot.logger
)
if success:
self.bot.logger.info(
f'Channel deleted from server {server_del}'
f', removed from voice log db'
)
except Exception as e:
self.bot.logger.info(
f"""Issue removing channel {channel} from voice database
after deletion error: {e}"""
)
def setup(bot):
"""General cog loading."""
bot.add_cog(Logging(bot))