-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ReminderCollection.kt
121 lines (110 loc) · 3.75 KB
/
ReminderCollection.kt
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
package org.hyacinthbots.lilybot.database.collections
import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent
import dev.kord.common.entity.Snowflake
import kotlinx.datetime.DateTimePeriod
import kotlinx.datetime.TimeZone
import kotlinx.datetime.plus
import org.hyacinthbots.lilybot.database.Database
import org.hyacinthbots.lilybot.database.entities.ReminderData
import org.koin.core.component.inject
import org.litote.kmongo.eq
/**
* This class contains the functions for interacting with [the reminder database][ReminderData]. This
* class contains functions for setting reminders, getting reminders based of various parameters, removing reminders and
* repeating them.
*
* @since 4.2.0
* @see getAllReminders
* @see getRemindersForUser
* @see getRemindersForUserInGuild
* @see setReminder
* @see removeReminder
* @see removeGuildReminders
* @see repeatReminder
*/
class ReminderCollection : KordExKoinComponent {
private val db: Database by inject()
@PublishedApi
internal val collection = db.mainDatabase.getCollection<ReminderData>()
/**
* Gets all the reminders currently in the database.
*
* @return A list of reminders in the database
* @author NoComment1105
* @since 4.2.0
*/
suspend fun getAllReminders(): List<ReminderData> = collection.find().toList()
/**
* Gets all the reminders in the database for a specific user.
*
* @param userId The ID of the user to get reminders for
* @return A list of reminders for the given [userId]
* @author NoComment1105
* @since 4.2.0
*/
suspend fun getRemindersForUser(userId: Snowflake): List<ReminderData> =
collection.find(ReminderData::userId eq userId).toList()
/**
* Gets all the reminders in the database for a specific user, in a specific guild.
*
* @param userId The ID of the user to get reminders for
* @param guildId The ID of the guild the reminders should be in
* @return A list of reminders for the given [userId] in the given [guildId]
* @author NoComment1105
* @since 4.2.0
*/
suspend fun getRemindersForUserInGuild(userId: Snowflake, guildId: Snowflake): List<ReminderData> =
getRemindersForUser(userId).filter { it.guildId == guildId }
/**
* Sets a reminder.
*
* @param reminderData The data for the reminder
* @author NoComment1105
* @since 4.2.0
*/
suspend fun setReminder(reminderData: ReminderData) = collection.insertOne(reminderData)
/**
* Removes a reminder from the database.
*
* @param userId The ID of the user the reminder belongs too
* @param number The reminder to remove
* @author NoComment1105
* @since 4.2.0
*/
suspend fun removeReminder(userId: Snowflake, number: Long) =
collection.deleteOne(ReminderData::userId eq userId, ReminderData::id eq number)
/**
* Removes all the reminders for a given guild.
*
* @param guildId The guild to remove reminders for
* @author NoComment1105
* @since 4.2.0
*/
suspend fun removeGuildReminders(guildId: Snowflake) = collection.deleteMany(ReminderData::guildId eq guildId)
/**
* Updates a repeating reminder to be extended by the given [repeatingInterval].
*
* @param originalData The original time the reminder data
* @param repeatingInterval The repeating interval to extend the reminder by
* @author NoComment1105
* @since 4.5.0
*/
suspend fun repeatReminder(originalData: ReminderData, repeatingInterval: DateTimePeriod) {
removeReminder(originalData.userId, originalData.id)
collection.insertOne(
ReminderData(
originalData.guildId,
originalData.remindTime.plus(repeatingInterval, TimeZone.UTC),
originalData.setTime,
originalData.userId,
originalData.channelId,
originalData.messageId,
originalData.dm,
originalData.customMessage,
originalData.repeating,
originalData.repeatingInterval,
originalData.id
)
)
}
}