-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to disable their IRC forwarding per-guild
Intended for use with PluralKit
- Loading branch information
1 parent
5882412
commit a14f033
Showing
6 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/kotlin/org/randomcat/agorabot/commands/RelayCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.randomcat.agorabot.commands | ||
|
||
import org.randomcat.agorabot.commands.base.BaseCommand | ||
import org.randomcat.agorabot.commands.base.BaseCommandImplReceiver | ||
import org.randomcat.agorabot.commands.base.BaseCommandStrategy | ||
import org.randomcat.agorabot.commands.base.help.help | ||
import org.randomcat.agorabot.commands.base.requirements.discord.InGuildSimple | ||
import org.randomcat.agorabot.commands.base.requirements.discord.currentGuildId | ||
import org.randomcat.agorabot.commands.base.requirements.discord.currentMessageEvent | ||
import org.randomcat.agorabot.commands.base.requires | ||
import org.randomcat.agorabot.guild_state.UserState | ||
import org.randomcat.agorabot.guild_state.UserStateMap | ||
import org.randomcat.agorabot.guild_state.update | ||
import org.randomcat.agorabot.irc.RELAY_USER_STATE_KEY | ||
import org.randomcat.agorabot.irc.RelayUserState | ||
|
||
private fun UserState.updateRelayState(mapper: (RelayUserState) -> RelayUserState) { | ||
update<RelayUserState>(RELAY_USER_STATE_KEY) { | ||
mapper(it ?: RelayUserState.DEFAULT) | ||
} | ||
} | ||
|
||
class RelayCommand( | ||
strategy: BaseCommandStrategy, | ||
private val userStateMap: UserStateMap, | ||
) : BaseCommand(strategy) { | ||
override fun BaseCommandImplReceiver.impl() { | ||
subcommands { | ||
subcommand("self") { | ||
subcommand("disable") { | ||
noArgs().help("Disables forwarding of messages from this account in this Guild") | ||
.requires(InGuildSimple) { | ||
val guildId = currentGuildId | ||
|
||
userStateMap.stateForUser(currentMessageEvent.author.id).updateRelayState { old -> | ||
when (old) { | ||
is RelayUserState.Version0 -> { | ||
RelayUserState.Version0(disabledGuilds = buildSet { | ||
addAll(old.disabledGuilds) | ||
add(guildId) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
respond("Disabled relay forwarding for your account in this Guild") | ||
} | ||
} | ||
|
||
subcommand("enable") { | ||
noArgs().help("Enables forwarding of messages from this account in this Guild") | ||
.requires(InGuildSimple) { | ||
val guildId = currentGuildId | ||
|
||
userStateMap.stateForUser(currentMessageEvent.author.id).updateRelayState { old -> | ||
when (old) { | ||
is RelayUserState.Version0 -> { | ||
RelayUserState.Version0(disabledGuilds = buildSet { | ||
addAll(old.disabledGuilds) | ||
remove(guildId) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
respond("Enabled relay forwarding for your account in this Guild") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.randomcat.agorabot.irc | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
const val RELAY_USER_STATE_KEY = "relay" | ||
|
||
@Serializable | ||
sealed class RelayUserState { | ||
companion object { | ||
val DEFAULT = Version0() | ||
} | ||
|
||
@Serializable | ||
@SerialName("RelayUserStateV0") | ||
data class Version0( | ||
val disabledGuilds: Set<String> = setOf(), | ||
) : RelayUserState() | ||
} |