Skip to content

Commit

Permalink
feat: random note command
Browse files Browse the repository at this point in the history
  • Loading branch information
mazziechai committed Jan 22, 2025
1 parent 8542fe1 commit b13a622
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,29 @@ class NoteCollection : KordExKoinComponent {
*/
suspend fun getByGuildAndName(guild: Snowflake, name: String) = getByGuildAndNameFlow(guild, name).toList()

suspend fun getRandomNote(guild: Snowflake, name: String) =
suspend fun getRandomNoteFilter(guild: Snowflake, filter: String) =
col.aggregate<Note>(
listOf(
match(
and(
eq(Note::guild.name, guild),
`in`(Note::aliases.name, name)
`in`(Note::aliases.name, filter)
),
), sample(1)
)
).firstOrNull()

suspend fun getRandomNote(guild: Snowflake) =
col.aggregate<Note>(
listOf(
match(
and(
eq(Note::guild.name, guild)
)
), sample(1)
)
).firstOrNull()

/**
* Gets multiple Notes by ID.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ChatCommandsExtension : Extension() {
}

action {
val note = noteCollection.getRandomNote(guild!!.id, arguments.noteName)
val note = noteCollection.getRandomNoteFilter(guild!!.id, arguments.noteName)

if (note == null) {
message.respond {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ViewingExtension : Extension() {
check { anyGuild() }

action {
val note = noteCollection.getRandomNote(guild!!.id, arguments.noteName)
val note = noteCollection.getRandomNoteFilter(guild!!.id, arguments.noteName)

if (note == null) {
respond {
Expand Down Expand Up @@ -172,6 +172,25 @@ class ViewingExtension : Extension() {
}
}
}

ephemeralSlashCommand(::ViewRandomCommandArgs) {
name = Translations.Extensions.Viewing.Random.name
description = Translations.Extensions.Viewing.Random.description

check { anyGuild() }
action {
val note = noteCollection.getRandomNote(guild!!.id)

if (note == null) {
respond {
content = Translations.Error.servernonotes.translate()
}
return@action
}

viewNoteResponse(note, arguments.text != false, guild!!.id)
}
}
}

inner class ViewByNameCommandArgs : Arguments() {
Expand All @@ -193,6 +212,13 @@ class ViewingExtension : Extension() {
}
}

inner class ViewRandomCommandArgs : Arguments() {
val text by optionalBoolean {
name = Translations.Arguments.Text.name
description = Translations.Arguments.Text.description
}
}

/**
* Recursively calls edit to display a [Note].
*
Expand Down Expand Up @@ -309,7 +335,7 @@ class ViewingExtension : Extension() {
guild: GuildBehavior,
arguments: ViewByNameCommandArgs
) {
val note = noteCollection.getRandomNote(guild.id, arguments.noteName)
val note = noteCollection.getRandomNoteFilter(guild.id, arguments.noteName)

if (note == null) {
respond {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/translations/kose/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ This functions similarly to `/peek`, but is public instead of ephemeral.\n\
`/edit (ID)` Creates a modal where you can edit a note you own by ID.\n\
`/delete (ID)` Deletes a note by ID you own after asking for confirmation.\n\n\
See the wiki for more help and commands: https://github.com/mazziechai/kose-kata/wiki
# viewing
extensions.viewing.peek.name=peek
extensions.viewing.peek.description=View a note ephemerally
extensions.viewing.post.name=post
Expand All @@ -120,6 +121,8 @@ extensions.viewing.search.description=Search for a note
extensions.viewing.search.nonotes=I couldn't find any notes matching the query.
extensions.viewing.info.name=info
extensions.viewing.info.description=Gets information about a note. Does not display contents.
extensions.viewing.random.name=peek
extensions.viewing.random.description=View a random note ephemerally
# Info
extensions.info.ids.name=ids
extensions.info.ids.description=List the IDs of notes under a name
Expand Down

0 comments on commit b13a622

Please sign in to comment.