forked from Wizard-Of-Chaos/BotlerIsland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_events.py
321 lines (304 loc) · 12 KB
/
bot_events.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
from datetime import datetime
import asyncio as aio
import discord as dc
from discord.ext import commands
from cogs_textbanks import url_bank, query_bank, response_bank
from bot_common import (
bot, guild_whitelist, CONST_ADMINS, CONST_AUTHOR,
guild_config, member_stalker,
)
image_exts = ('png', 'gif', 'jpg', 'jpeg', 'jpe', 'jfif')
avy_chid = 664541525350547496
att_chid = 696209752434278400
async def grab_avatar(user):
avy_channel = bot.get_channel(avy_chid)
with open('avatar.png', mode='wb') as avatarfile:
try:
await user.avatar_url.save(avatarfile)
except dc.NotFound:
return url_bank.null_avatar
msg_id = hex(member_stalker.member_data['avatar_count'])[2:]
member_stalker.member_data['avatar_count'] += 1
with open('avatar.png', mode='rb') as avatarfile:
await avy_channel.send(
f'`@{user}`: UID {user.id}: MID {msg_id}',
file=dc.File(avatarfile)
)
async for msg in avy_channel.history(limit=16):
if msg.content.split()[-1] == msg_id:
return msg.attachments[0].url
async def grab_attachments(msg):
pass
@bot.event
async def on_ready(): # Bot starts
print(response_bank.bot_startup.format(version='1.4.0'))
for guild in bot.guilds:
if guild.id not in guild_whitelist:
await guild.leave()
print(response_bank.verify_whitelist_complete)
await bot.change_presence(
activity=dc.Game(name=response_bank.online_status)
)
@bot.event
async def on_guild_join(guild): # Bot joins guild
if guild.id not in guild_whitelist:
await guild.leave()
@bot.event
async def on_member_join(member): # Log joined members
guild = member.guild
if not guild_config.getlog(guild, 'usrlog'):
return
member_stalker.update('first_join', member)
await member_stalker.load_roles(member)
embed = dc.Embed(
color=dc.Color.green(),
timestamp=datetime.utcnow(),
description=f':green_circle: {member.mention}: ``{member}`` has joined **{guild}**!\n'
f'The guild now has {guild.member_count} members!\n'
f'This account was created on `{member.created_at.strftime("%d/%m/%Y %H:%M:%S")}`'
)
embed.set_author(name=f'A user has joined the server!')
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name='**User ID**', value=f'`{member.id}`')
await guild_config.log(guild, 'usrlog', embed=embed)
@bot.event
async def on_member_update(bfr, aft): # Log role and nickname changes
guild = bfr.guild
if not guild_config.getlog(guild, 'msglog'):
return
if bfr.nick != aft.nick:
embed = dc.Embed(
color=dc.Color.magenta(),
timestamp=datetime.utcnow(),
description=f'**{bfr}** had their nickname changed to **{aft.nick}**',
)
embed.set_author(name='Nickname Update:', icon_url=aft.avatar_url)
embed.add_field(name='**User ID:**', value=f'`{aft.id}`', inline=False)
await guild_config.log(guild, 'msglog', embed=embed)
if bfr.roles != aft.roles:
embed = dc.Embed(
color=dc.Color.teal(),
timestamp=datetime.utcnow(),
description=f'**{bfr}** had the following roles changed:',
)
embed.set_author(name='Role Update:', icon_url=aft.avatar_url)
rolesprev, rolesnext = set(bfr.roles), set(aft.roles)
embed.add_field(
name='**Roles Added:**',
value=', '.join(f'`{role.name}`' for role in rolesnext-rolesprev) or None,
inline=False
)
embed.add_field(
name='**Roles Removed:**',
value=', '.join(f'`{role.name}`' for role in rolesprev-rolesnext) or None,
inline=False
)
embed.add_field(name='**User ID:**', value=f'`{aft.id}`', inline=False)
await guild_config.log(guild, 'msglog', embed=embed)
@bot.event
async def on_member_remove(member): # Log left/kicked/banned members
guild = member.guild
if not guild_config.getlog(guild, 'usrlog'):
return
member_stalker.update('first_join', member)
member_stalker.update('last_roles', member)
now = datetime.utcnow()
lastseen = member_stalker.get('last_seen', member)
if lastseen is not None:
lastseenmsg = (
f'This user was last seen on `{lastseen.strftime("%d/%m/%Y %H:%M:%S")}` '
f'({max(0, (now-lastseen).days)} days ago)'
)
else:
lastseenmsg = 'This user has not spoken to my knowledge.'
embed = dc.Embed(
color=dc.Color.red(),
timestamp=now,
description=f':red_circle: **{member}** has left **{guild}**!\n'
f'The guild now has {guild.member_count} members!\n{lastseenmsg}'
)
embed.set_author(name=f'A user left or got bucked off.')
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(
name='**Roles Snagged:**',
value=(', '.join(
f'`{guild.get_role(role).name}`'
for role in member_stalker.get('last_roles', member)
)
or None),
inline=False)
embed.add_field(name='**User ID:**', value=f'`{member.id}`')
await guild_config.log(guild, 'usrlog', embed=embed)
@bot.event
async def on_member_ban(guild, user): # Log member full bans
if not guild_config.getlog(guild, 'modlog'):
return
async for entry in guild.audit_logs(limit=256, action=dc.AuditLogAction.ban):
if entry.target.id == user.id:
break
else:
await guild_config.log(
guild,
f'The last ban of {user} `{user.id}` could not be found in the audit log.',
)
return
author = entry.user
embed = dc.Embed(
color=author.color,
timestamp=datetime.utcnow(),
description=f'**{author}** has full banned :hammer: **{user}**!'
)
embed.set_author(name=f'Good riddance.', icon_url=author.avatar_url)
embed.set_thumbnail(url=user.avatar_url)
embed.add_field(
name='**Reason:**',
value=entry.reason or 'None specified.',
inline=False,
)
embed.add_field(
name='**Roles Snagged:**',
value=(', '.join(
f'`{guild.get_role(role).name}`'
for role in member_stalker.get('last_roles', user)
)
or None),
inline=False)
embed.add_field(name='**User ID:**', value=f'`{user.id}`')
await guild_config.log(guild, 'modlog', embed=embed)
@bot.event
async def on_member_unban(guild, user): # Log member full ban appeals
if not guild_config.getlog(guild, 'modlog'):
return
embed = dc.Embed(
color=dc.Color.dark_teal(),
timestamp=datetime.utcnow(),
description=f'**{user}** has been unbanned :angel:!'
)
embed.set_author(name='Parole has been granted.')
embed.set_thumbnail(url=user.avatar_url)
embed.add_field(name='**User ID:**', value=f'`{user.id}`')
await guild_config.log(guild, 'modlog', embed=embed)
@bot.event
async def on_user_update(bfr, aft): # Log avatar, name, discrim changes
for guild in bot.guilds:
if not (guild_config.getlog(guild, 'msglog') and guild.get_member(bfr.id)):
continue
changelog = []
if bfr.name != aft.name:
changelog.append((
'Username Update:',
f'**Old Username:** {bfr}\n**New Username:** {aft}',
))
if bfr.discriminator != aft.discriminator:
changelog.append((
'Discriminator Update:',
f'{bfr} had their discriminator changed from '
f'{bfr.discriminator} to {aft.discriminator}',
))
await aio.sleep(0)
if bfr.avatar != aft.avatar:
changelog.append(('Avatar Update:', f'{bfr} has changed their avatar to:'))
for ctype, desc in changelog:
embed = dc.Embed(
color=dc.Color.purple(),
timestamp=datetime.utcnow(),
description=desc,
)
if ctype.startswith('Avatar'):
embed.set_author(name=ctype, icon_url=await grab_avatar(bfr))
embed.set_thumbnail(url=await grab_avatar(aft))
else:
embed.set_author(name=ctype, icon_url=aft.avatar_url)
embed.add_field(name='**User ID:**', value=f'`{aft.id}`', inline=False)
await guild_config.log(guild, 'msglog', embed=embed)
@bot.event
async def on_voice_state_update(member, bfr, aft): # Log when a member joins and leaves VC
guild = member.guild
if not guild_config.getlog(guild, 'msglog'):
return
changelog = None
if bfr.channel != aft.channel:
if bfr.channel == None:
changelog = f':loud_sound: **{member}** has joined **{aft.channel}**'
elif aft.channel == None:
changelog = f':loud_sound: **{member}** has left **{bfr.channel}**'
if changelog is not None:
embed = dc.Embed(color=dc.Color.blurple(), description=changelog)
await guild_config.log(guild, 'msglog', embed=embed)
@bot.event
async def on_message(msg): # Message posted event
if msg.guild is None:
return
member_stalker.update('last_seen', msg)
ctx = await bot.get_context(msg)
if ctx.valid:
if guild_config.getcmd(ctx):
await bot.process_commands(msg)
elif guild_config.getcmd(ctx) and msg.content.strip().lower() in query_bank.affirmation:
await msg.channel.send(response_bank.affirmation_response)
elif (msg.channel.id in guild_config.getlog(msg.guild, 'autoreact')
and any(any(map(att.url.lower().endswith, image_exts)) for att in msg.attachments)
):
await msg.add_reaction('❤️')
@bot.event
async def on_message_edit(bfr, aft): # Log edited messages
if bfr.author == bot.user or bfr.content == aft.content:
return
guild = bfr.guild
if not guild_config.getlog(guild, 'msglog'):
return
if len(bfr.content) <= 1024:
bfrmsg = bfr.content
long_edit = False
else:
bfrmsg = '`D--> The pre-edit message is too long to contain.`'
long_edit = True
with open('tmpmsg.txt', 'w') as bfrfile:
bfrfile.write(bfr.content)
if len(aft.content) <= 1024:
aftmsg = aft.content
else:
aftmsg = f'`D--> The post-edit message is too long, use this:` {aft.jump_url}'
embed = dc.Embed(color=dc.Color.gold(), timestamp=aft.edited_at)
embed.set_author(
name=f'@{bfr.author} edited a message in #{bfr.channel}:',
icon_url=bfr.author.avatar_url,
)
embed.add_field(name='**Before:**', value=bfrmsg, inline=False)
embed.add_field(name='**After:**', value=aftmsg, inline=False)
embed.add_field(name='**Message ID:**', value=f'`{aft.id}`')
embed.add_field(name='**User ID:**', value=f'`{bfr.author.id}`')
if long_edit:
with open('tmpmsg.txt', 'r') as bfrfile:
await guild_config.log(
guild, 'msglog', embed=embed,
file=dc.File(bfrfile, f'{bfr.id}-old.txt'),
)
else:
await guild_config.log(guild, 'msglog', embed=embed)
@bot.event
async def on_message_delete(msg): # Log deleted messages
if msg.guild is None:
return
guild = msg.channel.guild
if not guild_config.getlog(guild, 'msglog'):
return
embed = dc.Embed(
color=dc.Color.darker_grey(),
timestamp=msg.created_at,
description=msg.content,
)
embed.set_author(
name=f'@{msg.author} deleted a message in #{msg.channel}:',
icon_url=msg.author.avatar_url,
)
embed.add_field(name='**Message ID:**', value=f'`{msg.id}`')
embed.add_field(name='**User ID:**', value=f'`{msg.author.id}`')
if msg.attachments:
# att_channel = bot.get_channel(att_chid)
embed.add_field(
name='**Attachments:**',
value='\n'.join(att.url for att in msg.attachments),
inline=False,
)
await guild_config.log(guild, 'msglog', embed=embed)