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

Commit 1ea980f

Browse files
committed
Ask before joining an ephemeral thread
1 parent fc42442 commit 1ea980f

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed

scripty_commands/src/cmds/join.rs

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
use std::time::SystemTime;
1+
use std::time::{Duration, SystemTime};
22

33
use humantime::format_rfc3339_seconds;
44
use poise::CreateReply;
55
use scripty_bot_utils::checks::is_guild;
66
use serenity::{
7-
builder::{CreateEmbed, CreateEmbedFooter, CreateForumPost, CreateMessage, CreateThread},
7+
builder::{
8+
CreateActionRow,
9+
CreateButton,
10+
CreateEmbed,
11+
CreateEmbedFooter,
12+
CreateForumPost,
13+
CreateMessage,
14+
CreateThread,
15+
},
16+
collector::ComponentInteractionCollector,
817
model::{
18+
application::ButtonStyle,
919
channel::{AutoArchiveDuration, Channel, ChannelFlags, ChannelType, GuildChannel},
1020
id::GenericChannelId,
1121
},
@@ -42,7 +52,7 @@ pub async fn join(
4252
target_channel: Option<Channel>,
4353

4454
#[description = "Create a new thread for this transcription? Defaults to false."]
45-
create_thread: Option<bool>,
55+
create_thread: Option<bool>,
4656

4757
#[description = "Delete the transcript after the last user has left? Defaults to false."]
4858
ephemeral: Option<bool>,
@@ -127,13 +137,64 @@ pub async fn join(
127137
_ => return Err(Error::expected_guild()),
128138
};
129139

130-
if !(create_thread || target_thread.is_none()) && ephemeral {
131-
ctx.say(format_message!(
132-
resolved_language,
133-
"join-ephemeral-not-thread",
134-
))
135-
.await?;
136-
return Ok(());
140+
if ephemeral && let Some(target_thread) = &target_thread {
141+
const TWO_MINUTES: Duration = Duration::from_secs(2 * 60);
142+
143+
// wants to make an ephemeral thread using an existing one
144+
// this is dangerous, ask before continuing
145+
let resp = ctx
146+
.send(
147+
CreateReply::new()
148+
.content(format_message!(
149+
resolved_language,
150+
"join-ephemeral-with-thread-target-warn",
151+
targetThreadMention: target_thread.mention().to_string()
152+
))
153+
.components(&[CreateActionRow::buttons(&[
154+
CreateButton::new("join-ephemeral-with-thread-target-yes")
155+
.style(ButtonStyle::Danger)
156+
.label(format_message!(resolved_language, "generic-yes")),
157+
CreateButton::new("join-ephemeral-with-thread-target-no")
158+
.style(ButtonStyle::Secondary)
159+
.label(format_message!(resolved_language, "generic-no")),
160+
])]),
161+
)
162+
.await?;
163+
let msg_id = resp.message().await?.id;
164+
let resp = ComponentInteractionCollector::new(ctx.serenity_context())
165+
.author_id(ctx.author().id)
166+
.message_id(msg_id)
167+
.timeout(TWO_MINUTES)
168+
.await;
169+
170+
let resp = match resp {
171+
Some(resp) => resp,
172+
None => {
173+
ctx.send(CreateReply::new().content(format_message!(
174+
resolved_language,
175+
"join-ephemeral-with-thread-target-timed-out"
176+
)))
177+
.await?;
178+
return Ok(());
179+
}
180+
};
181+
resp.defer(ctx.http()).await?;
182+
resp.delete_response(ctx.http()).await?;
183+
let cid = resp.data.custom_id;
184+
if cid == "join-ephemeral-with-thread-target-yes" {
185+
// do nothing :3
186+
} else if cid == "join-ephemeral-with-thread-target-no" {
187+
ctx.send(CreateReply::new().content(format_message!(
188+
resolved_language,
189+
"join-ephemeral-with-thread-target-cancelled"
190+
)))
191+
.await?;
192+
return Ok(());
193+
} else {
194+
return Err(Error::custom(
195+
"got unknown custom data field on boolean question".to_string(),
196+
));
197+
}
137198
}
138199

139200
if target_channel.base.kind == ChannelType::Forum {
@@ -158,9 +219,9 @@ pub async fn join(
158219
ChannelType::Voice | ChannelType::Stage
159220
) {
160221
ctx.say(
161-
format_message!(resolved_language, "join-create-thread-in-unsupported", targetMention: target_channel.mention().to_string()),
162-
)
163-
.await?;
222+
format_message!(resolved_language, "join-create-thread-in-unsupported", targetMention: target_channel.mention().to_string()),
223+
)
224+
.await?;
164225
return Ok(());
165226
}
166227

@@ -180,14 +241,14 @@ pub async fn join(
180241
&& target_channel.flags.contains(ChannelFlags::REQUIRE_TAG)
181242
{
182243
ctx.say(
183-
format_message!(resolved_language, "join-forum-requires-tags", targetMention: target_channel.mention().to_string()),
184-
)
185-
.await?;
244+
format_message!(resolved_language, "join-forum-requires-tags", targetMention: target_channel.mention().to_string()),
245+
)
246+
.await?;
186247
return Ok(());
187248
} else if !is_text_based {
188249
ctx.say(
189-
format_message!(resolved_language, "join-target-not-text-based", targetMention: target_channel.mention().to_string()),
190-
).await?;
250+
format_message!(resolved_language, "join-target-not-text-based", targetMention: target_channel.mention().to_string()),
251+
).await?;
191252
return Ok(());
192253
}
193254

@@ -224,9 +285,9 @@ pub async fn join(
224285
// do we have permission to view and connect to the channel?
225286
if !permissions.connect() || !permissions.view_channel() {
226287
ctx.say(
227-
format_message!(resolved_language, "join-no-permission", targetMention: voice_channel.mention().to_string()),
228-
)
229-
.await?;
288+
format_message!(resolved_language, "join-no-permission", targetMention: voice_channel.mention().to_string()),
289+
)
290+
.await?;
230291
return Ok(());
231292
}
232293

@@ -243,9 +304,9 @@ pub async fn join(
243304
== 0
244305
{
245306
ctx.say(
246-
format_message!(resolved_language, "join-no-one-in-channel", targetMention: voice_channel.mention().to_string()),
247-
)
248-
.await?;
307+
format_message!(resolved_language, "join-no-one-in-channel", targetMention: voice_channel.mention().to_string()),
308+
)
309+
.await?;
249310
return Ok(());
250311
}
251312

@@ -298,10 +359,7 @@ pub async fn join(
298359
} else if let Some(target_thread) = target_thread {
299360
// this channel is a thread
300361

301-
let parent_id = target_channel
302-
.parent_id
303-
.ok_or(Error::custom("thread has no parent".to_string()))?;
304-
(Some(target_thread.id), parent_id)
362+
(Some(target_thread.id), target_thread.parent_id)
305363
} else {
306364
// no threads here
307365
(None, target_channel.id)

scripty_i18n/locales/en.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ join-forum-requires-tags = The forum channel you tried to make me use requires t
4242
join-target-not-text-based = The channel you told me to send transcripts to ({ $targetMention }) is not a text-based channel. Please use a text-based channel, or pick a different channel in the `target_channel` argument.
4343
# This message is shown when the user requests the bot create a new thread in a channel, but the channel doesn't support threads being created (usually voice channels)
4444
join-create-thread-in-unsupported = Discord does not support threads in { $targetMention }. Please use a different channel, or do not create a thread.
45+
join-ephemeral-with-thread-target-warn = You're trying to use { $targetThreadMention } as an ephemeral thread. This will delete it after Scripty leaves! Are you sure you want to do this?
46+
join-ephemeral-with-thread-target-timed-out = Timed out. Rerun if you still want to try again.
47+
join-ephemeral-with-thread-target-cancelled = You selected not to proceed, cancelling.
4548
4649
## Leave command
4750
# This and all attributes show up exclusively in the slash command picker when `leave` is selected.
@@ -561,3 +564,8 @@ delete-data-cancel = No, cancel
561564
## generic strings
562565
# Message shown if a guild has not claimed their free trial of premium. Always appears on its own standalone line in the surrounding message.
563566
free-trial-upsell = We offer 3-day trials of Scripty Premium if you would like to try it out and see if it is right for you. Send the bot a DM to get started with a free trial.
567+
568+
# Generic string for yes
569+
generic-yes = Yes
570+
# Generic string for no
571+
generic-no = No

0 commit comments

Comments
 (0)