-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
275 lines (215 loc) · 9.06 KB
/
bot.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
"""Main bot file."""
import datetime
import os
import discord
from discord.ext import commands
import functions
import regexes
import settings
if not functions.database_exists():
functions.database_create()
# Enable privileged intent required for events such as `on_member_remove()`.
INTENTS = discord.Intents.default()
INTENTS.members = True
BOT = commands.Bot(
command_prefix=functions.database_guild_prefix_get, intents=INTENTS)
BOT.activity = discord.Game(settings.BOT_ACTIVITY)
async def copypasta_channel_process_message(message):
"""
Process messages sent to copypasta channel.
Args:
message (discord.Message): Message to process.
"""
functions.database_copypasta_channel_last_saved_id_set(
message.guild.id, message.id)
guild_prefix = functions.database_guild_prefix_get(BOT, message)
flag = "import" if message.attachments else "add"
message.content = (f"{guild_prefix}copypasta --{flag} {message.content}")
await BOT.process_commands(message)
@BOT.event
async def on_ready():
"""Will run once bot is done preparing data received from Discord."""
# Save messages sent to copypasta channel while bot was offline.
for guild in BOT.guilds:
copypasta_channel = BOT.get_channel(
functions.database_copypasta_channel_get(guild.id))
if not copypasta_channel:
continue
last_saved_copypasta_id = (
functions.database_copypasta_channel_last_saved_id_get(guild.id))
try:
last_saved_copypasta = await copypasta_channel.fetch_message(
last_saved_copypasta_id)
after = last_saved_copypasta.created_at
except discord.errors.NotFound:
after = None
async for message in copypasta_channel.history(
limit=None, after=after, oldest_first=True):
if message.id == last_saved_copypasta_id:
break
if message.author == BOT.user:
continue
# Check whether or not message invokes bot (i.e.: it is a command).
ctx = await BOT.get_context(message)
if ctx.valid:
continue
await copypasta_channel_process_message(message)
@BOT.event
async def on_message(message):
"""
Will run every time a message is received.
Only runs on channels bot has permission to access and read messages on.
Args:
message (discord.Message): Received message.
"""
if message.author == BOT.user:
return
ctx = await BOT.get_context(message)
# Check whether or not message invokes bot (i.e.: it is a command).
if ctx.valid:
await BOT.process_commands(message)
return
if regexes.MARCO.fullmatch(message.content):
await ctx.send(functions.marco_polo(message.content))
return
guild_prefix = functions.database_guild_prefix_get(BOT, message)
if BOT.user in message.mentions:
await ctx.send(functions.get_localized_object(
message.guild.id, "MENTION_HELP").format(prefix=guild_prefix))
return
copypasta_channel = functions.database_copypasta_channel_get(ctx.guild.id)
if copypasta_channel and message.channel.id == copypasta_channel:
await copypasta_channel_process_message(message)
@BOT.event
async def on_raw_message_delete(payload):
"""
Will run every time a message is deleted, whether it is cached or not.
Only runs on channels bot has permission to access and read messages on.
Args:
payload (discord.RawMessageDeleteEvent): Raw event payload data.
"""
guild_id = payload.guild_id
logging_channel = BOT.get_channel(
functions.database_logging_channel_get(guild_id))
if logging_channel:
message = payload.cached_message
message_id = payload.message_id
creation_time = discord.utils.snowflake_time(message_id)
guild_date_format = functions.get_localized_object(
guild_id, "STRFTIME_DATE")
guild_time_format = functions.get_localized_object(
guild_id, "STRFTIME_TIME")
guild_format = f"{guild_date_format} | {guild_time_format}"
utc_time = datetime.datetime.utcnow()
local_time = functions.utc_to_local(utc_time, guild_id)
channel = BOT.get_channel(payload.channel_id)
embed = discord.Embed(color=settings.EMBED_COLOR)
if message:
embed.set_thumbnail(url=message.author.avatar_url)
embed.add_field(
name=functions.get_localized_object(
guild_id, "LOGGING_MESSAGE_DELETED_FIELD_HEADER_NAME"),
value=functions.get_localized_object(
guild_id,
"LOGGING_MESSAGE_DELETED_FIELD_HEADER_VALUE").format(
channel_name=channel.mention,
message_author=message.author.mention),
inline=False)
embed.add_field(
name=functions.get_localized_object(
guild_id, "LOGGING_MESSAGE_DELETED_FIELD_CONTENT_NAME"),
# `or None` is used just in case message was an embed and
# therefore has no content. This prevents an exception from
# being raised due to an embed field with an empty value.
value=message.content or None,
inline=False)
else:
embed.add_field(
name=functions.get_localized_object(
guild_id, "LOGGING_MESSAGE_DELETED_FIELD_HEADER_NAME"),
value=functions.get_localized_object(
guild_id,
"LOGGING_MESSAGE_DELETED_FIELD_HEADER_VALUE_NO_CACHE").format(
channel_name=channel.mention),
inline=False)
embed.add_field(
name=functions.get_localized_object(
guild_id, "LOGGING_MESSAGE_DELETED_FIELD_CREATION_TIME_NAME"),
value=functions.get_localized_object(
guild_id,
"LOGGING_MESSAGE_DELETED_FIELD_CREATION_TIME_VALUE").format(
local_time=functions.utc_to_local(
creation_time, guild_id).strftime(guild_format),
utc_time=creation_time.strftime(guild_format)),
inline=False)
embed.add_field(
name=functions.get_localized_object(
guild_id, "LOGGING_MESSAGE_DELETED_FIELD_ID_NAME"),
value=f"`{message_id}`",
inline=False)
embed.set_footer(text=functions.get_localized_object(
guild_id, "LOGGING_MESSAGE_DELETED_FOOTER").format(
local_time=local_time.strftime(guild_format),
utc_time=utc_time.strftime(guild_format)))
await logging_channel.send(embed=embed)
@BOT.event
async def on_command_error(ctx, error):
"""
Will run every time an error occurs while trying to run a command.
Args:
ctx (discord.ext.commands.Context): Context passed to function.
error (discord.ext.commands.CommandError): Base exception for all
command related errors.
"""
if isinstance(error, commands.MissingPermissions):
await ctx.send(functions.get_localized_object(
ctx.guild.id, "MISSING_PERMISSIONS").format(
member=ctx.message.author.mention))
elif isinstance(error, commands.BotMissingPermissions):
await ctx.send(functions.get_localized_object(
ctx.guild.id, "BOT_MISSING_PERMISSIONS"))
else:
raise error
await ctx.send(functions.get_localized_object(
ctx.guild.id, "MISSING_PERMISSIONS_DETAILS").format(details=error))
@BOT.event
async def on_guild_join(guild):
"""
Will run every time the bot joins a guild.
Args:
guild (discord.Guild): Guild bot has joined.
"""
functions.database_guild_initialize(guild.id)
general = discord.utils.find(lambda c: any(
name in c.name for name in settings.COMMON_GENERAL_TEXT_CHANNEL_NAMES),
guild.text_channels)
if general and general.permissions_for(guild.me).send_messages:
message = ""
for locale in functions.get_available_locales():
message = functions.get_localized_object(
guild.id, "GUILD_JOIN", locale).format(
locale=locale,
guild_name=guild.name,
prefix=settings.GUILD_DEFAULT_PREFIX)
await general.send(message)
@BOT.event
async def on_guild_remove(guild):
"""
Will run every time the bot leaves a guild.
Args:
guild (discord.Guild): Guild bot has left.
"""
functions.database_guild_purge(guild.id)
@BOT.event
async def on_member_remove(member):
"""
Will run every time a member leaves a guild.
Args:
member (discord.Member): Member who left the guild.
"""
functions.database_birthday_delete(member.guild.id, member.id)
functions.database_copypasta_unban_user(member.guild.id, member.id)
for file in os.listdir("cogs"):
if file.endswith(".py"):
BOT.load_extension(f"cogs.{file[:-3]}")
BOT.run(settings.BOT_TOKEN)