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
|
from operator import itemgetter
from django.contrib.postgres import fields as pgfields
from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
from django.db import models
from django.utils import timezone
from .validators import validate_bot_setting_name, validate_tag_embed
class ModelReprMixin:
"""
Adds a `__repr__` method to the model subclassing this
mixin which will display the model's class name along
with all parameters used to construct the object.
"""
def __repr__(self):
attributes = ' '.join(
f'{attribute}={value!r}'
for attribute, value in sorted(
self.__dict__.items(),
key=itemgetter(0)
)
if not attribute.startswith('_')
)
return f'<{self.__class__.__name__}({attributes})>'
class BotSetting(ModelReprMixin, models.Model):
"""A configuration entry for the bot."""
name = models.CharField(
primary_key=True,
max_length=50,
validators=(validate_bot_setting_name,)
)
data = pgfields.JSONField(
help_text="The actual settings of this setting."
)
class DocumentationLink(ModelReprMixin, models.Model):
"""A documentation link used by the `!docs` command of the bot."""
package = models.CharField(
primary_key=True,
max_length=50,
help_text="The Python package name that this documentation link belongs to."
)
base_url = models.URLField(
help_text=(
"The base URL from which documentation will be available for this project. "
"Used to generate links to various symbols within this package."
)
)
inventory_url = models.URLField(
help_text="The URL at which the Sphinx inventory is available for this package."
)
def __str__(self):
return f"{self.package} - {self.base_url}"
class OffTopicChannelName(ModelReprMixin, models.Model):
name = models.CharField(
primary_key=True,
max_length=96,
validators=(RegexValidator(regex=r'^[a-z0-9-]+$'),),
help_text="The actual channel name that will be used on our Discord server."
)
def __str__(self):
return self.name
class Role(ModelReprMixin, models.Model):
"""A role on our Discord server."""
id = models.BigIntegerField( # noqa
primary_key=True,
validators=(
MinValueValidator(
limit_value=0,
message="Role IDs cannot be negative."
),
),
help_text="The role ID, taken from Discord."
)
name = models.CharField(
max_length=100,
help_text="The role name, taken from Discord."
)
colour = models.IntegerField(
validators=(
MinValueValidator(
limit_value=0,
message="Colour hex cannot be negative."
),
),
help_text="The integer value of the colour of this role from Discord."
)
permissions = models.IntegerField(
validators=(
MinValueValidator(
limit_value=0,
message="Role permissions cannot be negative."
),
MaxValueValidator(
limit_value=2 << 32,
message="Role permission bitset exceeds value of having all permissions"
)
),
help_text="The integer value of the permission bitset of this role from Discord."
)
def __str__(self):
return self.name
class SnakeFact(ModelReprMixin, models.Model):
"""A snake fact used by the bot's snake cog."""
fact = models.CharField(
primary_key=True,
max_length=200,
help_text="A fact about snakes."
)
def __str__(self):
return self.fact
class SnakeIdiom(ModelReprMixin, models.Model):
"""A snake idiom used by the snake cog."""
idiom = models.CharField(
primary_key=True,
max_length=140,
help_text="A saying about a snake."
)
def __str__(self):
return self.idiom
class SnakeName(ModelReprMixin, models.Model):
"""A snake name used by the bot's snake cog."""
name = models.CharField(
primary_key=True,
max_length=100,
help_text="The regular name for this snake, e.g. 'Python'.",
validators=[RegexValidator(regex=r'^([^0-9])+$')]
)
scientific = models.CharField(
max_length=150,
help_text="The scientific name for this snake, e.g. 'Python bivittatus'.",
validators=[RegexValidator(regex=r'^([^0-9])+$')]
)
def __str__(self):
return f"{self.name} ({self.scientific})"
class SpecialSnake(ModelReprMixin, models.Model):
"""A special snake's name, info and image from our database used by the bot's snake cog."""
name = models.CharField(
max_length=140,
primary_key=True,
help_text='A special snake name.',
validators=[RegexValidator(regex=r'^([^0-9])+$')]
)
info = models.TextField(
help_text='Info about a special snake.'
)
images = pgfields.ArrayField(
models.URLField(),
help_text='Images displaying this special snake.'
)
def __str__(self):
return self.name
class Tag(ModelReprMixin, models.Model):
"""A tag providing (hopefully) useful information."""
title = models.CharField(
max_length=100,
help_text=(
"The title of this tag, shown in searches and providing "
"a quick overview over what this embed contains."
),
primary_key=True
)
embed = pgfields.JSONField(
help_text="The actual embed shown by this tag.",
validators=(validate_tag_embed,)
)
def __str__(self):
return self.title
class User(ModelReprMixin, models.Model):
"""A Discord user."""
id = models.BigIntegerField( # noqa
primary_key=True,
validators=(
MinValueValidator(
limit_value=0,
message="User IDs cannot be negative."
),
),
help_text="The ID of this user, taken from Discord."
)
name = models.CharField(
max_length=32,
help_text="The username, taken from Discord."
)
discriminator = models.PositiveSmallIntegerField(
validators=(
MaxValueValidator(
limit_value=9999,
message="Discriminators may not exceed `9999`."
),
),
help_text="The discriminator of this user, taken from Discord."
)
avatar_hash = models.CharField(
max_length=100,
help_text=(
"The user's avatar hash, taken from Discord. "
"Null if the user does not have any custom avatar."
),
null=True
)
roles = models.ManyToManyField(
Role,
help_text="Any roles this user has on our server."
)
in_guild = models.BooleanField(
default=True,
help_text="Whether this user is in our server."
)
def __str__(self):
return f"{self.name}#{self.discriminator}"
class Message(ModelReprMixin, models.Model):
id = models.BigIntegerField(
primary_key=True,
help_text="The message ID as taken from Discord.",
validators=(
MinValueValidator(
limit_value=0,
message="Message IDs cannot be negative."
),
)
)
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
help_text="The author of this message."
)
channel_id = models.BigIntegerField(
help_text=(
"The channel ID that this message was "
"sent in, taken from Discord."
),
validators=(
MinValueValidator(
limit_value=0,
message="Channel IDs cannot be negative."
),
)
)
content = models.CharField(
max_length=2_000,
help_text="The content of this message, taken from Discord.",
blank=True
)
embeds = pgfields.ArrayField(
pgfields.JSONField(
validators=(validate_tag_embed,)
),
help_text="Embeds attached to this message."
)
class Meta:
abstract = True
class MessageDeletionContext(ModelReprMixin, models.Model):
actor = models.ForeignKey(
User,
on_delete=models.CASCADE,
help_text=(
"The original actor causing this deletion. Could be the author "
"of a manual clean command invocation, the bot when executing "
"automatic actions, or nothing to indicate that the bulk "
"deletion was not issued by us."
),
null=True
)
creation = models.DateTimeField(
# Consider whether we want to add a validator here that ensures
# the deletion context does not take place in the future.
help_text="When this deletion took place."
)
class DeletedMessage(Message):
deletion_context = models.ForeignKey(
MessageDeletionContext,
help_text="The deletion context this message is part of.",
on_delete=models.CASCADE
)
class Infraction(ModelReprMixin, models.Model):
"""An infraction for a Discord user."""
TYPE_CHOICES = (
("note", "Note"),
("warning", "Warning"),
("watch", "Watch"),
("mute", "Mute"),
("kick", "Kick"),
("ban", "Ban"),
("superstar", "Superstar")
)
inserted_at = models.DateTimeField(
default=timezone.now,
help_text="The date and time of the creation of this infraction."
)
expires_at = models.DateTimeField(
null=True,
help_text=(
"The date and time of the expiration of this infraction. "
"Null if the infraction is permanent or it can't expire."
)
)
active = models.BooleanField(
default=True,
help_text="Whether the infraction is still active."
)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='infractions_received',
help_text="The user to which the infraction was applied."
)
actor = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='infractions_given',
help_text="The user which applied the infraction."
)
type = models.CharField(
max_length=9,
choices=TYPE_CHOICES,
help_text="The type of the infraction."
)
reason = models.TextField(
null=True,
help_text="The reason for the infraction."
)
hidden = models.BooleanField(
default=False,
help_text="Whether the infraction is a shadow infraction."
)
def __str__(self):
s = f"#{self.id}: {self.type} on {self.user_id}"
if self.expires_at:
s += f" until {self.expires_at}"
if self.hidden:
s += " (hidden)"
return s
class Reminder(ModelReprMixin, models.Model):
"""A reminder created by a user."""
active = models.BooleanField(
default=True,
help_text=(
"Whether this reminder is still active. "
"If not, it has been sent out to the user."
)
)
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
help_text="The creator of this reminder."
)
channel_id = models.BigIntegerField(
help_text=(
"The channel ID that this message was "
"sent in, taken from Discord."
),
validators=(
MinValueValidator(
limit_value=0,
message="Channel IDs cannot be negative."
),
)
)
content = models.CharField(
max_length=1500,
help_text="The content that the user wants to be reminded of."
)
expiration = models.DateTimeField(
help_text="When this reminder should be sent."
)
def __str__(self):
return f"{self.content} on {self.expiration} by {self.author}"
class Nomination(ModelReprMixin, models.Model):
"""A helper nomination created by staff."""
active = models.BooleanField(
default=True,
help_text="Whether this nomination is still relevant."
)
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
help_text="The staff member that nominated this user.",
related_name='nomination_set'
)
reason = models.TextField(
help_text="Why this user was nominated."
)
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
help_text="The nominated user.",
primary_key=True,
related_name='nomination'
)
inserted_at = models.DateTimeField(
auto_now_add=True,
help_text="The creation date of this nomination."
)
class LogEntry(ModelReprMixin, models.Model):
"""A log entry generated by one of the PyDis applications."""
application = models.CharField(
max_length=20,
help_text="The application that generated this log entry.",
choices=(
('bot', 'Bot'),
('seasonalbot', 'Seasonalbot'),
('site', 'Website')
)
)
logger_name = models.CharField(
max_length=100,
help_text="The name of the logger that generated this log entry."
)
timestamp = models.DateTimeField(
default=timezone.now,
help_text="The date and time when this entry was created."
)
level = models.CharField(
max_length=8, # 'critical'
choices=(
('debug', 'Debug'),
('info', 'Info'),
('warning', 'Warning'),
('error', 'Error'),
('critical', 'Critical')
),
help_text=(
"The logger level at which this entry was emitted. The levels "
"correspond to the Python `logging` levels."
)
)
module = models.CharField(
max_length=100,
help_text="The fully qualified path of the module generating this log line."
)
line = models.PositiveSmallIntegerField(
help_text="The line at which the log line was emitted."
)
message = models.TextField(
help_text="The textual content of the log line."
)
|