Skip to content

Commit

Permalink
Allow multiple roles to be pinged on auto-threads (#386)
Browse files Browse the repository at this point in the history
* Allow multiple roles to be pinged in auto-threaded channels

* Add migration for new issue

* Fix doc file
  • Loading branch information
NoComment1105 committed Apr 4, 2024
1 parent aab071a commit 8799f52
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 50 deletions.
114 changes: 68 additions & 46 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,71 @@ None
* **Arguments**:
* `channel` - The channel to view the auto-threading settings for. - Channel

---
### Command name: `auto-threading add-roles`
**Description**: Add extra to threads in auto-threaded channels

**Additional Information**: This command will add roles to be pinged alongside the default ping role for this auto-threaded channel

**Required Member Permissions**: Manage Channels

* **Arguments**:
* `role` - A role to invite to threads in this channel - Optional Role

---
### Command name: `auto-threading remove-roles`
**Description**: Remove extra from threads in auto-threaded channels

**Additional Information**: This command will remove roles that have been added to be pinged alongside the default ping role for this auto-threaded channel

**Required Member Permissions**: Manage Channels

* **Arguments**:
* `role` - A role to invite to threads in this channel - Optional Role

---
### Command name: `clear count`
**Description**: Clear a specific count of messages

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `messages` - Number of messages to delete - Int
* `author` - The author of the messages to clear - Optional User

---
### Command name: `clear before`
**Description**: Clear messages before a given message ID

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `before` - The ID of the message to clear before - Snowflake
* `message-count` - The number of messages to clear - Optional Int/Long
* `author` - The author of the messages to clear - Optional User

---
### Command name: `clear after`
**Description**: Clear messages before a given message ID

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `after` - The ID of the message to clear after - Snowflake
* `message-count` - The number of messages to clear - Optional Int/Long
* `author` - The author of the messages to clear - Optional User

---
### Command name: `clear between`
**Description**: Clear messages between 2 message IDs

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `after` - The ID of the message to clear after - Snowflake
* `before` - The ID of the message to clear before - Snowflake
* `author` - The author of the messages to clear - Optional User

---
### Command name: `config moderation`
**Description**: Configure Lily's moderation system
Expand Down Expand Up @@ -318,49 +383,6 @@ None
* `dm` - Whether to send a direct message to the user about the kick - Defaulting Boolean
* `image` - An image you'd like to provide as extra context for the action - Optional Attachment

---
### Command name: `clear count`
**Description**: Clear a specific count of messages

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `messages` - Number of messages to delete - Int
* `author` - The author of the messages to clear - Optional User

---
### Command name: `clear before`
**Description**: Clear messages before a given message ID

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `before` - The ID of the message to clear before - Snowflake
* `message-count` - The number of messages to clear - Optional Int/Long
* `author` - The author of the messages to clear - Optional User

---
### Command name: `clear after`
**Description**: Clear messages before a given message ID

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `after` - The ID of the message to clear after - Snowflake
* `message-count` - The number of messages to clear - Optional Int/Long
* `author` - The author of the messages to clear - Optional User

---
### Command name: `clear between`
**Description**: Clear messages between 2 message IDs

**Required Member Permissions**: Manage Messages

* **Arguments**:
* `after` - The ID of the message to clear after - Snowflake
* `before` - The ID of the message to clear before - Snowflake
* `author` - The author of the messages to clear - Optional User

---
### Command name: `timeout`
**Description**: Times out a user.
Expand Down Expand Up @@ -717,8 +739,8 @@ None
* `clear` - Whether to clear the channel before repopulating it - Defaulting Boolean

---
### Command name: `phishing-check`
**Description**: Check whether a given domain is a known phishing domain.
### Command name: `url-safety-check`
**Description**: Check whether a given domain is a known unsafe domain.

* Arguments:
* `domain` - Domain to check - String
Expand Down Expand Up @@ -767,7 +789,7 @@ None
### Message Command: `Report`

---
### Message Command: `Phishing Check`
### Message Command: `URL Safety Check`

---
## User Commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.hyacinthbots.lilybot.database.Database
import org.hyacinthbots.lilybot.database.entities.AutoThreadingData
import org.koin.core.component.inject
import org.litote.kmongo.eq
import org.litote.kmongo.setValue

/**
* This class contains the functions for interacting with the [AutoThreading Database][AutoThreadingData]. This
Expand Down Expand Up @@ -58,6 +59,21 @@ class AutoThreadingCollection : KordExKoinComponent {
collection.insertOne(inputAutoThreadData)
}

/**
* Updates the extra roles on an auto-threaded channel.
*
* @param inputChannelId The channel to update extra roles for
* @param newRoleIds The new list of role IDs to set
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun updateExtraRoles(inputChannelId: Snowflake, newRoleIds: List<Snowflake?>) {
collection.updateOne(
AutoThreadingData::channelId eq inputChannelId,
setValue(AutoThreadingData::extraRoleIds, newRoleIds)
)
}

/**
* Deletes an auto thread based off of the [inputChannelId].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ data class AutoThreadingData(
val contentAwareNaming: Boolean,
val mention: Boolean,
val creationMessage: String?,
val addModsAndRole: Boolean
val addModsAndRole: Boolean,
val extraRoleIds: MutableList<Snowflake>
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.hyacinthbots.lilybot.database.migrations.config.configV5
import org.hyacinthbots.lilybot.database.migrations.config.configV6
import org.hyacinthbots.lilybot.database.migrations.config.configV7
import org.hyacinthbots.lilybot.database.migrations.main.mainV1
import org.hyacinthbots.lilybot.database.migrations.main.mainV10
import org.hyacinthbots.lilybot.database.migrations.main.mainV2
import org.hyacinthbots.lilybot.database.migrations.main.mainV3
import org.hyacinthbots.lilybot.database.migrations.main.mainV4
Expand Down Expand Up @@ -73,6 +74,7 @@ object Migrator : KordExKoinComponent {
7 -> ::mainV7
8 -> ::mainV8
9 -> ::mainV9
10 -> ::mainV10
else -> break
}(db.mainDatabase)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hyacinthbots.lilybot.database.migrations.main

import org.hyacinthbots.lilybot.database.entities.AutoThreadingData
import org.litote.kmongo.coroutine.CoroutineDatabase
import org.litote.kmongo.exists
import org.litote.kmongo.setValue

suspend fun mainV10(db: CoroutineDatabase) {
with(db.getCollection<AutoThreadingData>()) {
updateMany(AutoThreadingData::extraRoleIds exists false, setValue(AutoThreadingData::extraRoleIds, emptyList()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.message.embed
import kotlinx.coroutines.delay
import kotlinx.datetime.Clock
import org.hyacinthbots.docgenerator.subCommandAdditionalDocumentation
import org.hyacinthbots.lilybot.database.collections.AutoThreadingCollection
import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection
import org.hyacinthbots.lilybot.database.collections.ThreadsCollection
Expand Down Expand Up @@ -136,7 +137,8 @@ class AutoThreading : Extension() {
contentAwareNaming = arguments.contentAwareNaming,
mention = arguments.mention,
creationMessage = message,
addModsAndRole = arguments.addModsAndRole
addModsAndRole = arguments.addModsAndRole,
extraRoleIds = mutableListOf()
)
)

Expand Down Expand Up @@ -312,6 +314,16 @@ class AutoThreading : Extension() {
"None"
}
}
if (autoThread.extraRoleIds.isNotEmpty()) {
var mentions = ""
autoThread.extraRoleIds.forEach {
mentions += "${guild!!.getRoleOrNull(it)?.mention} "
}
field {
name = "Extra Ping Roles"
value = mentions
}
}
field {
name = "Prevent duplicates"
value = autoThread.preventDuplicates.toString()
Expand Down Expand Up @@ -340,6 +352,103 @@ class AutoThreading : Extension() {
}
}
}

ephemeralSubCommand(::ExtraRolesArgs) {
name = "add-roles"
description = "Add extra to threads in auto-threaded channels"

subCommandAdditionalDocumentation {
extraInformation = "This command will add roles to be pinged alongside the default ping role " +
"for this auto-threaded channel"
}

requirePermission(Permission.ManageChannels)

check {
anyGuild()
hasPermission(Permission.ManageChannels)
requireBotPermissions(Permission.SendMessages)
botHasChannelPerms(Permissions(Permission.SendMessages))
}

action {
val autoThread = AutoThreadingCollection().getSingleAutoThread(event.interaction.channelId)
if (autoThread == null) {
respond {
content = "**Error:** This is not an auto-threaded channel!"
}
return@action
}

if (!canPingRole(arguments.role, guild!!.id, this@ephemeralSubCommand.kord)) {
respond {
content = "Lily cannot mention this role. Please fix the role's permissions and try again."
}
return@action
}

if (autoThread.extraRoleIds.contains(arguments.role!!.id)) {
respond {
content = "This role has already been added."
}
return@action
}

val updatedRoles = autoThread.extraRoleIds
updatedRoles.add(arguments.role!!.id)

AutoThreadingCollection().updateExtraRoles(event.interaction.channelId, updatedRoles)

respond {
content = "Role (${arguments.role!!.mention}) added"
}
}
}

ephemeralSubCommand(::ExtraRolesArgs) {
name = "remove-roles"
description = "Remove extra from threads in auto-threaded channels"

subCommandAdditionalDocumentation {
extraInformation = "This command will remove roles that have been added to be pinged alongside " +
"the default ping role for this auto-threaded channel"
}

requirePermission(Permission.ManageChannels)

check {
anyGuild()
hasPermission(Permission.ManageChannels)
requireBotPermissions(Permission.SendMessages)
botHasChannelPerms(Permissions(Permission.SendMessages))
}

action {
val autoThread = AutoThreadingCollection().getSingleAutoThread(event.interaction.channelId)
if (autoThread == null) {
respond {
content = "**Error:** This is not an auto-threaded channel!"
}
return@action
}

if (!autoThread.extraRoleIds.contains(arguments.role!!.id)) {
respond {
content = "This role has not been added."
}
return@action
}

val updatedRoles = autoThread.extraRoleIds
updatedRoles.remove(arguments.role!!.id)

AutoThreadingCollection().updateExtraRoles(event.interaction.channelId, updatedRoles)

respond {
content = "Role (${arguments.role!!.mention}) removed"
}
}
}
}

event<ProxiedMessageCreateEvent> {
Expand Down Expand Up @@ -459,6 +568,13 @@ class AutoThreading : Extension() {
}
}

inner class ExtraRolesArgs : Arguments() {
val role by optionalRole {
name = "role"
description = "A role to invite to threads in this channel"
}
}

inner class AutoThreadingViewArgs : Arguments() {
val channel by channel {
name = "channel"
Expand Down Expand Up @@ -580,14 +696,18 @@ class AutoThreading : Extension() {
if (moderatorRoleId != null) {
moderatorRole = inputThread.guild.getRole(moderatorRoleId)
}
var mentions = ""
inputOptions.extraRoleIds.forEach {
mentions += inputThread.guild.getRole(it).mention
}

if (moderatorRole != null && moderatorRole.mentionable && inputOptions.addModsAndRole) {
threadMessage.edit {
content = role.mention + moderatorRole.mention
content = role.mention + mentions + moderatorRole.mention
}
} else {
threadMessage.edit {
content = role.mention
content = role.mention + mentions
}
}
}
Expand Down

0 comments on commit 8799f52

Please sign in to comment.