-
Notifications
You must be signed in to change notification settings - Fork 6
/
delete_all.py
85 lines (68 loc) · 3.33 KB
/
delete_all.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
"""Contains cog classes for any delete_all interactions."""
from collections.abc import Sequence
__all__: Sequence[str] = ("DeleteAllCommandsCog",)
import discord
from db.core.models import AssignedCommitteeAction, DiscordReminder, GroupMadeMember
from db.core.models.utils import AsyncBaseModel
from utils import CommandChecks, TeXBotApplicationContext, TeXBotBaseCog
class DeleteAllCommandsCog(TeXBotBaseCog):
"""Cog class that defines the "/delete-all" command group and command call-back methods."""
delete_all: discord.SlashCommandGroup = discord.SlashCommandGroup(
name="delete-all",
description=(
"Delete all instances of the selected object type from the backend database"
),
)
@staticmethod
async def _delete_all(ctx: TeXBotApplicationContext, delete_model: type[AsyncBaseModel]) -> None: # noqa: E501
"""Perform the actual deletion process of all instances of the given model class."""
# noinspection PyProtectedMember
await delete_model._default_manager.all().adelete()
delete_model_instances_name_plural: str = (
delete_model.INSTANCES_NAME_PLURAL
if hasattr(delete_model, "INSTANCES_NAME_PLURAL")
else "objects"
)
await ctx.respond(
f"All {delete_model_instances_name_plural} deleted successfully.",
ephemeral=True,
)
@delete_all.command(
name="reminders",
description="Deletes all Reminders from the backend database.",
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def delete_all_reminders(self, ctx: TeXBotApplicationContext) -> None:
"""
Definition & callback response of the "delete_all_reminders" command.
The "delete_all_reminders" command uses the _delete_all() function
to delete all `DiscordReminder` instance objects stored in the database.
"""
await self._delete_all(ctx, delete_model=DiscordReminder)
@delete_all.command(
name="group-made-members",
description="Deletes all Group Made Members from the backend database.",
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def delete_all_group_made_members(self, ctx: TeXBotApplicationContext) -> None:
"""
Definition & callback response of the "delete_all_group_made_members" command.
The "delete_all_group_made_members" command uses the _delete_all() function
to delete all `GroupMadeMember` instance objects stored in the database.
"""
await self._delete_all(ctx, delete_model=GroupMadeMember)
@delete_all.command(
name="actions",
description="Deletes all the Actions from the backend database.",
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def delete_all_actions(self, ctx: TeXBotApplicationContext) -> None:
"""
Definition & callback respoonse of the "delete-all-actions" command.
The "delete-all-actions" command uses the _delete_all() function
to delete all `Action` instance objects stored in the database.
"""
await self._delete_all(ctx, delete_model=AssignedCommitteeAction)