-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModLog.py
361 lines (325 loc) · 21.1 KB
/
ModLog.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
import discord
from discord.ext import commands
from cogs.utils.dataIO.dataIO import js
import os
import datetime
class Modlog:
"""Logs moderation stuff."""
def __init__(self, bot):
self.bot = bot
self.settings = js.load("data/modlog/settings.json")
@commands.group(pass_context=True, no_pm=True)
async def modlogset(self, ctx):
if ctx.message.author.id == "289463060178010123" or ctx.message.author.id == "278830391090216960":
"""Set the settings for the moderation logs."""
if not ctx.invoked_subcommand:
await self.bot.send_cmd_help(ctx)
if ctx.message.server.id not in self.settings:
self.settings[ctx.message.server.id] = {'channel': None, 'disabled': False, 'join': True, 'leave': True, 'voicechat': True, 'msgedit': True, 'msgdelete': True, 'roleedit': True, 'ban': True, 'reactions': True, 'channels': True, 'nicknames': True}
self.save_settings()
else:
self.bot.say("You are not authorized to do that!")
@modlogset.command(pass_context=True, no_pm=True)
async def channel(self, ctx, channel:discord.Channel):
"""Sets the channel the bot should log to."""
self.settings[ctx.message.server.id]['channel'] = channel.id
self.save_settings()
await self.bot.say("Channel set, I will now log to {}.".format(channel.mention))
@modlogset.command(pass_context=True, no_pm=True)
async def disable(self, ctx):
"""Disable the logging system completely."""
if not self.settings[ctx.message.server.id]['disabled']:
self.settings[ctx.message.server.id]['disabled'] = True
self.save_settings()
await self.bot.say("Logging system has been disabled.")
else:
self.settings[ctx.message.server.id]['disabled'] = False
self.save_settings()
await self.bot.say("Logging system has been enabled.")
@modlogset.command(pass_context=True, no_pm=True)
async def toggle(self, ctx, module=None):
"""Toggle what the bot should and what the bot shouldn't log."""
server = ctx.message.server
modules = ['join', 'leave', 'ban', 'voicechat', 'msgedit', 'msgdelete', 'roleedit', 'channels', 'nicknames']
if module == None:
msg = "```py"
try:
msg += "\nChannel: " + str(self.settings[server.id]['channel'])
msg += "\nDisabled: " + str(self.settings[server.id]['disabled'])
msg += "\nJoin: " + str(self.settings[server.id]['join'])
msg += "\nLeave: " + str(self.settings[server.id]['leave'])
msg += "\nBan: " + str(self.settings[server.id]['ban'])
msg += "\nVoicechat: " + str(self.settings[server.id]['voicechat'])
msg += "\nMessage edit: " + str(self.settings[server.id]['msgedit'])
msg += "\nMessage delete: " + str(self.settings[server.id]['msgdelete'])
msg += "\nRole edit: " + str(self.settings[server.id]['roleedit'])
msg += "\nChannels: " + str(self.settings[server.id]['channels'])
msg += "\nNicknames: " + str(self.settings[server.id]['nicknames'])
except KeyError:
pass
msg += "\n\nFalse = not being logged.\nTrue = being logged."
msg += "```"
await self.bot.say(msg + "You can toggle\n{}.".format(", ".join(modules)))
elif module.lower() == 'join':
if self.settings[server.id]['join']:
self.settings[server.id]['join'] = False
self.save_settings()
await self.bot.say("Join logging has been disabled.")
else:
self.settings[server.id]['join'] = True
self.save_settings()
await self.bot.say("Join logging has been enabled.")
elif module.lower() == 'leave':
if self.settings[server.id]['leave']:
self.settings[server.id]['leave'] = False
self.save_settings()
await self.bot.say("Leave (and kick) logging has been disabled.")
else:
self.settings[server.id]['leave'] = True
self.save_settings()
await self.bot.say("Leave (and kick) logging has been enabled.")
elif module.lower() == 'ban':
if self.settings[server.id]['ban']:
self.settings[server.id]['ban'] = False
self.save_settings()
await self.bot.say("Ban logging has been disabled.")
else:
self.settings[server.id]['ban'] = True
self.save_settings()
await self.bot.say("Ban logging has been enabled.")
elif module.lower() == 'voicechat':
if self.settings[server.id]['voicechat']:
self.settings[server.id]['voicechat'] = False
self.save_settings()
await self.bot.say("Voicechat logging has been disabled.")
else:
self.settings[server.id]['voicechat'] = True
self.save_settings()
await self.bot.say("Voicechat logging has been enabled.")
elif module.lower() == 'msgedit':
if self.settings[server.id]['msgedit']:
self.settings[server.id]['msgedit'] = False
self.save_settings()
await self.bot.say("Message edit has been disabled.")
else:
self.settings[server.id]['msgedit'] = True
self.save_settings()
await self.bot.say("Message edit has been enabled.")
elif module.lower() == 'msgdelete':
if self.settings[server.id]['msgdelete']:
self.settings[server.id]['msgdelete'] = False
self.save_settings()
await self.bot.say("Message delete has been disabled.")
else:
self.settings[server.id]['msgdelete'] = True
self.save_settings()
await self.bot.say("Message delete has been enabled.")
elif module.lower() == 'roleedit':
if self.settings[server.id]['roleedit']:
self.settings[server.id]['roleedit'] = False
self.save_settings()
await self.bot.say("Role edit has been disabled.")
else:
self.settings[server.id]['roleedit'] = True
self.save_settings()
await self.bot.say("Role edit has been enabled.")
elif module.lower() == 'channels':
if 'channels' not in self.settings[server.id]:
self.settings[server.id]['channels'] = True
self.save_settings()
return
elif self.settings[server.id]['channels']:
self.settings[server.id]['channels'] = False
self.save_settings()
await self.bot.say("Channels have been disabled.")
else:
self.settings[server.id]['channels'] = True
self.save_settings()
await self.bot.say("Channels have been enabled.")
elif module.lower() == 'nicknames':
if 'nicknames' not in self.settings[server.id]:
self.settings[server.id]['nicknames'] = True
self.save_settings()
await self.bot.say("Nicknames have been enabled.")
elif self.settings[server.id]['nicknames']:
self.settings[server.id]['nicknames'] = False
self.save_settings()
await self.bot.say("Nicknames have been disabled.")
else:
self.settings[server.id]['nicknames'] = True
self.save_settings()
await self.bot.say("Nicknames have been enabled.")
else:
await self.bot.say("That module cannot be toggled, you can toggle\n{}.".format(", ".join(modules)))
async def on_member_join(self, member):
if self.is_module(member.server, 'join'):
await self.log(member.server, "`[{}]` :inbox_tray: **Member Join Log**\n"
"```Member Joined: {}```".format(self.get_time(), str(member)))
async def on_member_remove(self, member):
if self.is_module(member.server, 'leave'):
await self.log(member.server, "`[{}]` :outbox_tray: **Member Leave/Kick Log**\n"
"```Member Left/Kicked: {}```".format(self.get_time(), str(member)))
async def on_member_ban(self, member):
if self.is_module(member.server, 'ban'):
await self.log(member.server, "`[{}]` :hammer: **Member Ban Log**\n"
"```Member Banned: {}```".format(self.get_time(), str(member)))
async def on_voice_state_update(self, before, after):
if self.is_module(before.server, 'voicechat'):
if before.voice_channel != after.voice_channel:
await self.log(before.server, "`[{}]` :bangbang: **Voicechat Log**\n"
"```User: {}"
"\nBefore: {}".format(self.get_time(), str(before), str(before.voice_channel)) +
"\nAfter: {}```".format(str(after.voice_channel)))
async def on_message_edit(self, before, after):
if (before.server != None) and (before.author == before.server.me):
return
if self.is_module(before.server, 'msgedit'):
if before.content != after.content:
await self.log(before.server, "`[{}]` :pencil2: **Message Edit Log**\n"
"```User: {}"
"\nChannel: {}"
"\nBefore: {}".format(self.get_time(), before.author.name, before.channel.name, before.content) +
"\nAfter: {}```".format(after.content))
async def on_message_delete(self, message):
if message.author == message.server.me:
return
if self.is_module(message.server, 'msgdelete'):
await self.log(message.server, "`[{}]` :wastebasket: **Message Delete Log**\n"
"```User: {}\n"
"Channel: {}\n"
"Message: {}\n```".format(self.get_time(), str(message.author), str(message.channel), message.content))
async def on_server_role_create(self, role):
if self.is_module(role.server, 'roleedit'):
perms = role.permissions
await self.log(role.server, "`[{}]` :game_die: **Role Create Log**\n"
"```py\nRole: {}"
"\nColour: {}"
"\nPermissions:"
"\n\tMentionable: {}"
"\n\tDisplay Separatly: {}"
"\n\tAdministrator: {}"
"\n\tCan ban members: {}"
"\n\tCan kick members: {}"
"\n\tCan change nickname: {}"
"\n\tCan connect to voice channels: {}"
"\n\tCan create instant invites: {}"
"\n\tCan deafen members: {}"
"\n\tCan embed links: {}"
"\n\tCan manage channels: {}"
"\n\tCan manage emojis: {}"
"\n\tCan manage messages: {}"
"\n\tCan manage nicknames: {}"
"\n\tCan manage roles: {}"
"\n\tCan manage server: {}"
"\n\tCan mention everyone: {}"
"\n\tCan move members: {}"
"\n\tCan mute members: {}"
"\n\tCan read message history: {}```".format(self.get_time(), str(role.name), str(role.colour), str(role.mentionable), str(role.hoist), str(perms.administrator), str(perms.ban_members), str(perms.kick_members), str(perms.change_nickname), str(perms.connect), str(perms.create_instant_invite), str(perms.deafen_members), str(perms.embed_links), str(perms.manage_channels), str(perms.manage_emojis), str(perms.manage_messages), str(perms.manage_nicknames), str(perms.manage_roles), str(perms.manage_server), str(perms.mention_everyone), str(perms.move_members), str(perms.mute_members), str(perms.read_message_history), str(perms.send_messages), str(perms.speak), str(perms.use_voice_activation), str(perms.manage_webhooks), str(perms.add_reactions)))
async def on_server_role_delete(self, role):
if self.is_module(role.server, 'roleedit'):
await self.log(role.server, "`[{}]` :game_die: **Role Delete Log**\n"
"```Role: {}```".format(self.get_time(), role.name))
async def on_server_role_update(self, before, after):
if self.is_module(before.server, 'roleedit'):
if not (before.permissions == after.permissions) and (before.color == after.color):
perms = before.permissions
perms2 = after.permissions
await self.log(before.server, "`[{}]` :game_die: **Role Edit Log**\n"
"```py\nBefore:\nRole: {}"
"\nColour: {}"
"\nPermissions:"
"\n\tMentionable: {}"
"\n\tDisplay Separatly: {}"
"\n\tAdministrator: {}"
"\n\tCan ban members: {}"
"\n\tCan kick members: {}"
"\n\tCan change nickname: {}"
"\n\tCan connect to voice channels: {}"
"\n\tCan create instant invites: {}"
"\n\tCan deafen members: {}"
"\n\tCan embed links: {}"
"\n\tCan manage channels: {}"
"\n\tCan manage emojis: {}"
"\n\tCan manage messages: {}"
"\n\tCan manage nicknames: {}"
"\n\tCan manage roles: {}"
"\n\tCan manage server: {}"
"\n\tCan mention everyone: {}"
"\n\tCan move members: {}"
"\n\tCan mute members: {}"
"\n\tCan read message history: {}".format(self.get_time(), str(before.name), str(before.colour), str(before.mentionable), str(before.hoist), str(perms.administrator), str(perms.ban_members), str(perms.kick_members), str(perms.change_nickname), str(perms.connect), str(perms.create_instant_invite), str(perms.deafen_members), str(perms.embed_links), str(perms.manage_channels), str(perms.manage_emojis), str(perms.manage_messages), str(perms.manage_nicknames), str(perms.manage_roles), str(perms.manage_server), str(perms.mention_everyone), str(perms.move_members), str(perms.mute_members), str(perms.read_message_history), str(perms.send_messages), str(perms.speak), str(perms.use_voice_activation), str(perms.manage_webhooks), str(perms.add_reactions)) +
"\n\nAfter:\nRole: {}"
"\nColour: {}"
"\nPermissions:"
"\n\tMentionable: {}"
"\n\tDisplay Separatly: {}"
"\n\tAdministrator: {}"
"\n\tCan ban members: {}"
"\n\tCan kick members: {}"
"\n\tCan change nickname: {}"
"\n\tCan connect to voice channels: {}"
"\n\tCan create instant invites: {}"
"\n\tCan deafen members: {}"
"\n\tCan embed links: {}"
"\n\tCan manage channels: {}"
"\n\tCan manage emojis: {}"
"\n\tCan manage messages: {}"
"\n\tCan manage nicknames: {}"
"\n\tCan manage roles: {}"
"\n\tCan manage server: {}"
"\n\tCan mention everyone: {}"
"\n\tCan move members: {}"
"\n\tCan mute members: {}"
"\n\tCan read message history: {}```".format(str(after.name), str(after.colour), str(after.mentionable), str(after.hoist), str(perms2.administrator), str(perms2.ban_members), str(perms2.kick_members), str(perms2.change_nickname), str(perms2.connect), str(perms2.create_instant_invite), str(perms2.deafen_members), str(perms2.embed_links), str(perms2.manage_channels), str(perms2.manage_emojis), str(perms2.manage_messages), str(perms2.manage_nicknames), str(perms2.manage_roles), str(perms2.manage_server), str(perms2.mention_everyone), str(perms2.move_members), str(perms2.mute_members), str(perms2.read_message_history), str(perms2.send_messages), str(perms2.speak), str(perms2.use_voice_activation), str(perms2.manage_webhooks), str(perms2.add_reactions)))
async def on_channel_create(self, channel):
if self.is_module(channel.server, 'channels'):
await self.log(channel.server, "`[{}]` :pick: **Channel Create Log**\n"
"```Channel: {}```".format(self.get_time(), channel.name))
async def on_channel_delete(self, channel):
if self.is_module(channel.server, 'channels'):
await self.log(channel.server, "`[{}]` :pick: **Channel Delete Log**\n"
"```Channel: {}```".format(self.get_time(), channel.name))
async def on_channel_update(self, before, after):
if self.is_module(before.server, 'channels'):
if not before.name == after.name:
await self.log(before.server, "`[{}]` :pick: **Channel Edit Log**\n"
"```Before: {}\nAfter: {}```".format(self.get_time(), before.name, after.name))
async def on_member_update(self, before, after):
if self.is_module(before.server, 'nicknames'):
if not before.nick == after.nick:
await self.log(before.server, "`[{}]` :warning: **Nickname Change Log**\n"
"```User: {}\nBefore: {}\nAfter: {}```".format(self.get_time(), str(before), before.nick, after.nick))
async def log(self, server, message):
channel = discord.utils.get(server.channels, id=self.settings[server.id]['channel'])
try:
await self.bot.send_message(channel, message)
except:
pass
def get_time(self):
return datetime.datetime.now().strftime("%X")
def is_module(self, server, module):
if server == None:
return False
elif server.id not in self.settings:
return False
elif not self.settings[server.id]['disabled']:
if (self.settings[server.id]['channel'] != None) and (module in self.settings[server.id]) and (self.settings[server.id][module]):
return True
else:
return False
else:
return False
def save_settings(self):
js.dump(self.settings, "data/modlog/settings.json", overwrite=True)
def check_folders():
if not os.path.exists("data/modlog"):
print("Creating data/modlog folder...")
os.makedirs("data/modlog")
def check_files():
if not os.path.exists("data/modlog/settings.json"):
print("Creating data/modlog/settings.json file...")
js.dump({}, "data/modlog/settings.json")
def setup(bot):
check_folders()
check_files()
bot.add_cog(Modlog(bot))