Skip to content

Commit

Permalink
fix: prevent the bot from joining afk channels (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: yuua <yuuahp@icloud.com>
  • Loading branch information
yuuahp and yuua authored Oct 29, 2023
1 parent 01815ca commit 4520067
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Main : CliktCommand() {
add(::VoiceLeaveEvent)
add(::VoiceMoveEvent)
add(::TitleEvent)
add(::SelfVoiceJoinEvent)
}
}

Expand Down
35 changes: 25 additions & 10 deletions src/main/kotlin/com/jaoafa/vcspeaker/commands/JoinCommand.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jaoafa.vcspeaker.commands

import com.jaoafa.vcspeaker.tools.discord.ChatCommandExtensions.chatCommand
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.isAfk
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.orFallbackOf
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.respond
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.selfVoiceChannel
Expand All @@ -11,6 +12,8 @@ import com.jaoafa.vcspeaker.tools.discord.VoiceExtensions.move
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChannel
import com.kotlindiscord.kord.extensions.extensions.Extension
import dev.kord.common.entity.ChannelType
import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.behavior.channel.BaseVoiceChannelBehavior

class JoinCommand : Extension() {
override val name = this::class.simpleName!!
Expand All @@ -23,6 +26,22 @@ class JoinCommand : Extension() {
}
}

private suspend fun processJoin(
channel: BaseVoiceChannelBehavior,
guild: GuildBehavior,
replier: suspend (String) -> Unit
) {
if (channel.isAfk()) {
replier("**:zzz: AFK チャンネルには接続できません。**")
return
}

val selfChannel = guild.selfVoiceChannel()

if (selfChannel != null) channel.move(replier)
else channel.join(replier)
}

override suspend fun setup() {
publicSlashCommand("join", "VC に接続します。", ::JoinOptions) {
action {
Expand All @@ -31,11 +50,9 @@ class JoinCommand : Extension() {
respond(it)
} ?: return@action

val selfChannel = guild!!.selfVoiceChannel()
val replier: suspend (String) -> Unit = { respond(it) }

if (selfChannel != null) channel.move(replier)
else channel.join(replier)
processJoin(channel, guild!!) {
respond(it)
}
}
}

Expand All @@ -48,11 +65,9 @@ class JoinCommand : Extension() {
return@action
}

val selfChannel = guild!!.selfVoiceChannel()
val replier: suspend (String) -> Unit = { respond(it) }

if (selfChannel != null) channel.move(replier)
else channel.join(replier)
processJoin(channel, guild!!) {
respond(it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import com.jaoafa.vcspeaker.features.Title.resetTitle
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.authorOf
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.errorColor
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.orFallbackOf
import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSlashCommand
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.respond
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.respondEmbed
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.successColor
import com.jaoafa.vcspeaker.tools.discord.Options
import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSlashCommand
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChannel
import com.kotlindiscord.kord.extensions.extensions.Extension
import dev.kord.common.entity.ChannelType
import dev.kord.core.behavior.channel.asChannelOf
import dev.kord.core.entity.channel.VoiceChannel

class ResetTitleCommand : Extension() {
override val name = this::class.simpleName!!
Expand Down
36 changes: 36 additions & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/events/SelfVoiceJoinEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jaoafa.vcspeaker.events

import com.jaoafa.vcspeaker.VCSpeaker
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.asChannelOf
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.getSettings
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.isAfk
import com.jaoafa.vcspeaker.tools.discord.VoiceExtensions.join
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.event
import dev.kord.core.entity.channel.TextChannel
import dev.kord.core.event.user.VoiceStateUpdateEvent

class SelfVoiceJoinEvent : Extension() {
override val name = this::class.simpleName!!

override suspend fun setup() {
event<VoiceStateUpdateEvent> {
check {
failIf(event.state.getMember().id != VCSpeaker.kord.selfId)
failIf(!(event.state.getChannelOrNull()?.isAfk() ?: false))
}

action {
val guild = event.state.getGuildOrNull() ?: return@action // checked
val channelLeft = event.old?.getChannelOrNull()
val textChannel = guild.getSettings().channelId?.asChannelOf<TextChannel>()

channelLeft?.join {
textChannel?.createMessage(
"**:zzz: AFK チャンネルには接続できないので、${channelLeft.mention} に戻りました。**"
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typealias Options = Arguments
* Discord 関連の拡張関数をまとめたオブジェクト。
*/
object DiscordExtensions {
fun Guild.getSettings() = GuildStore.getOrDefault(this.id)
/**
* 自動入退室が有効化されているかどうか。
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/jaoafa/vcspeaker/voicetext/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Scheduler(
val file = if (!CacheStore.exists(text, voice)) {
val audio = try {
VCSpeaker.voicetext.generateSpeech(text, voice)
} catch (_: Exception) {
} catch (exception: Exception) {
message?.reply {
embed {
title = ":interrobang: Error!"
Expand All @@ -40,6 +40,8 @@ class Scheduler(
}
}

exception.printStackTrace()

return
}

Expand Down

0 comments on commit 4520067

Please sign in to comment.