Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.

Commit fc42442

Browse files
committed
Allow changing auto-join per-channel
1 parent f1f10da commit fc42442

File tree

4 files changed

+86
-22
lines changed

4 files changed

+86
-22
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE scripty.public.per_voice_channel_settings
2+
ADD COLUMN auto_join_enabled BOOLEAN NOT NULL DEFAULT TRUE;

scripty_bot_utils/src/handler/normal/voice_state_update.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,21 @@ pub async fn voice_state_update(ctx: &Context, new: &VoiceState) {
196196
}
197197
};
198198
let maybe_target = match sqlx::query!(
199-
"SELECT target_channel FROM per_voice_channel_settings WHERE channel_id = $1",
199+
"SELECT auto_join_enabled, target_channel
200+
FROM per_voice_channel_settings
201+
WHERE channel_id = $1",
200202
voice_channel_id.get() as i64
201203
)
202204
.fetch_optional(db)
203205
.await
204-
.map(|maybe_row| maybe_row.and_then(|r| r.target_channel))
206+
.map(|maybe_row| maybe_row.map(|r| (r.target_channel, r.auto_join_enabled)))
205207
{
206-
Ok(maybe_target) => maybe_target.map(|cid| ChannelId::new(cid as u64)),
208+
// auto-join disabled at the channel level
209+
Ok(Some((_, false))) => return,
210+
// auto join enabled, has a target
211+
Ok(Some((Some(target_id), true))) => Some(ChannelId::new(target_id as u64)),
212+
// no special settings, or no target
213+
Ok(None | Some((None, true))) => None,
207214
Err(e) => {
208215
error!(%guild_id, "error fetching target channel from per_voice_channel_settings: {}", e);
209216
return;
Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use scripty_bot_utils::{Context, Error};
2+
use serenity::model::{
3+
channel::{ChannelType, GuildChannel},
4+
mention::Mentionable,
5+
};
26

37
/// Should Scripty automatically join a voice channel when someone joins it?
48
#[poise::command(
@@ -8,7 +12,14 @@ use scripty_bot_utils::{Context, Error};
812
required_permissions = "MANAGE_GUILD",
913
rename = "auto_join"
1014
)]
11-
pub async fn config_auto_join(ctx: Context<'_>, auto_join: bool) -> Result<(), Error> {
15+
pub async fn config_auto_join(
16+
ctx: Context<'_>,
17+
#[description = "Defaults to false"] auto_join: bool,
18+
#[description = "Optionally set a channel this setting should apply to. If not set, the \
19+
server setting is modified."]
20+
#[channel_types("Voice", "Stage")]
21+
modify_channel: Option<GuildChannel>,
22+
) -> Result<(), Error> {
1223
let guild_id = ctx
1324
.guild_id()
1425
.map(|g| g.get())
@@ -17,6 +28,20 @@ pub async fn config_auto_join(ctx: Context<'_>, auto_join: bool) -> Result<(), E
1728
scripty_i18n::get_resolved_language(ctx.author().id.get(), Some(guild_id)).await;
1829
let db = scripty_db::get_db();
1930

31+
if let Some(ref modify_channel) = modify_channel
32+
&& !matches!(
33+
modify_channel.base.kind,
34+
ChannelType::Voice | ChannelType::Stage
35+
) {
36+
ctx.say(format_message!(
37+
resolved_language,
38+
"config-auto-join-modify-channel-must-be-vc",
39+
modifyChannelMention: modify_channel.mention().to_string()
40+
))
41+
.await?;
42+
return Ok(());
43+
}
44+
2045
// check if the user needs to set a default channel first
2146
if auto_join {
2247
let target_channel_set = sqlx::query!(
@@ -39,24 +64,49 @@ pub async fn config_auto_join(ctx: Context<'_>, auto_join: bool) -> Result<(), E
3964
}
4065
}
4166

42-
sqlx::query!(
43-
"INSERT INTO guilds (guild_id, auto_join) VALUES ($1, $2) ON CONFLICT ON CONSTRAINT \
44-
guilds_pkey DO UPDATE SET auto_join = $2",
45-
guild_id as i64,
46-
auto_join
47-
)
48-
.execute(db)
49-
.await?;
50-
51-
ctx.say(format_message!(
52-
resolved_language,
53-
if auto_join {
54-
"config-auto-join-enabled"
55-
} else {
56-
"config-auto-join-disabled"
57-
}
58-
))
59-
.await?;
67+
let fmt_string = if let Some(modify_channel) = modify_channel {
68+
sqlx::query!(
69+
"INSERT INTO per_voice_channel_settings (channel_id, auto_join_enabled)
70+
VALUES ($1, $2)
71+
ON CONFLICT
72+
ON CONSTRAINT per_voice_channel_settings_pkey
73+
DO UPDATE SET auto_join_enabled = $2",
74+
modify_channel.id.get() as i64,
75+
auto_join
76+
)
77+
.execute(db)
78+
.await?;
79+
80+
format_message!(
81+
resolved_language,
82+
if auto_join {
83+
"config-auto-join-enabled-channel"
84+
} else {
85+
"config-auto-join-disabled-channel"
86+
},
87+
modifyChannelMention: modify_channel.mention().to_string()
88+
)
89+
} else {
90+
sqlx::query!(
91+
"INSERT INTO guilds (guild_id, auto_join) VALUES ($1, $2) ON CONFLICT ON CONSTRAINT \
92+
guilds_pkey DO UPDATE SET auto_join = $2",
93+
guild_id as i64,
94+
auto_join
95+
)
96+
.execute(db)
97+
.await?;
98+
99+
format_message!(
100+
resolved_language,
101+
if auto_join {
102+
"config-auto-join-enabled"
103+
} else {
104+
"config-auto-join-disabled"
105+
}
106+
)
107+
};
108+
109+
ctx.say(fmt_string).await?;
60110

61111
Ok(())
62112
}

scripty_i18n/locales/en.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,13 @@ cmds_config_auto_join = auto_join
166166
.description = Should Scripty automatically join a voice channel when someone joins it?
167167
.auto_join = auto_join
168168
.auto_join-description = Defaults to false
169+
.modify_channel = modify_channel
170+
.modify_channel-description = Optionally set a channel this setting should apply to. If not set, the server setting is modified.
169171
config-auto-join-enabled = Scripty will now automatically join VCs when a user does.
170172
config-auto-join-disabled = Scripty will no longer automatically join VCs when a use does.
173+
config-auto-join-enabled-channel = Scripty will now automatically join { $modifyChannelMention } when a user does.
174+
config-auto-join-disabled-channel = Scripty will no longer automatically join { $modifyChannelMention } when a user does.
175+
config-auto-join-modify-channel-must-be-vc = { $modifyChannelMention } isn't a voice channel. Try again with a voice channel.
171176
config-auto-join-needs-target-channel = Enabling auto-join requires a default target channel be set. Do that with `{ $contextPrefix }config default target_channel`.
172177
173178
## config - transcribe only role command

0 commit comments

Comments
 (0)