Skip to content

Commit 05c4533

Browse files
committed
make a big huge mess implementing these settings.
1 parent 0a26b87 commit 05c4533

4 files changed

Lines changed: 68 additions & 25 deletions

File tree

crates/stable-diffusion-bot/src/bot/handlers/image.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ impl MessageText {
193193
.join("\n")
194194
))
195195
}
196+
197+
pub fn new(prompt: &str) -> Self {
198+
Self(format!("`{}`", prompt))
199+
}
196200
}
197201

198202
impl<T> TryFrom<ImgResponse<T>> for MessageText {
@@ -318,6 +322,16 @@ async fn handle_prompt(
318322
msg: Message,
319323
text: String,
320324
) -> anyhow::Result<()> {
325+
if text.is_empty() {
326+
bot.send_message(
327+
msg.chat.id,
328+
teloxide::utils::markdown::escape("A prompt is required to run txt2img."),
329+
)
330+
.parse_mode(teloxide::types::ParseMode::MarkdownV2)
331+
.await?;
332+
return Ok(());
333+
}
334+
321335
bot.send_chat_action(msg.chat.id, ChatAction::UploadPhoto)
322336
.await?;
323337

crates/stable-diffusion-bot/src/bot/handlers/mod.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ pub(crate) use settings::*;
1919
#[derive(BotCommands, Clone)]
2020
#[command(rename_rule = "lowercase", description = "Simple commands")]
2121
pub(crate) enum UnauthenticatedCommands {
22-
#[command(description = "show help message.")]
23-
Help,
2422
#[command(description = "start the bot.")]
2523
Start,
2624
#[command(description = "change settings.")]
2725
Settings,
26+
#[command(description = "show help message.")]
27+
Help,
2828
}
2929

3030
pub(crate) async fn unauthenticated_commands_handler(
@@ -35,17 +35,25 @@ pub(crate) async fn unauthenticated_commands_handler(
3535
cmd: UnauthenticatedCommands,
3636
dialogue: DiffusionDialogue,
3737
) -> anyhow::Result<()> {
38+
let chat = msg.chat.id;
39+
let user = msg.from().unwrap().id;
3840
let text = match cmd {
3941
UnauthenticatedCommands::Help => {
40-
if cfg.chat_is_allowed(&msg.chat.id)
41-
|| cfg.chat_is_allowed(&msg.from().unwrap().id.into())
42-
{
43-
format!(
44-
"{}\n\n{}\n\n{}",
45-
UnauthenticatedCommands::descriptions(),
46-
SettingsCommands::descriptions(),
47-
GenCommands::descriptions()
48-
)
42+
if cfg.chat_is_allowed(&chat) || cfg.chat_is_allowed(&user.into()) {
43+
let unauth = vec![UnauthenticatedCommands::descriptions()];
44+
let settings =
45+
if !cfg.settings.disable_user_settings || cfg.user_is_admin(&user.into()) {
46+
vec![SettingsCommands::descriptions()]
47+
} else {
48+
vec![]
49+
};
50+
let gen = vec![GenCommands::descriptions()];
51+
[unauth, settings, gen]
52+
.iter()
53+
.flatten()
54+
.map(ToString::to_string)
55+
.collect::<Vec<_>>()
56+
.join("\n\n")
4957
} else if msg.chat.is_group() || msg.chat.is_supergroup() {
5058
UnauthenticatedCommands::descriptions()
5159
.username_from_me(&me)
@@ -92,14 +100,6 @@ pub(crate) fn filter_map_settings() -> UpdateHandler<anyhow::Error> {
92100
})
93101
}
94102

95-
pub(crate) fn admin_filter() -> UpdateHandler<anyhow::Error> {
96-
dptree::filter(|cfg: ConfigParameters, upd: Update| {
97-
upd.user()
98-
.map(|user| cfg.user_is_admin(&user.id.into()))
99-
.unwrap_or_default()
100-
})
101-
}
102-
103103
pub(crate) fn auth_filter() -> UpdateHandler<anyhow::Error> {
104104
dptree::filter(|cfg: ConfigParameters, upd: Update| {
105105
upd.chat()

crates/stable-diffusion-bot/src/bot/handlers/settings.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing::error;
1212

1313
use crate::{bot::ConfigParameters, BotState};
1414

15-
use super::{admin_filter, filter_map_bot_state, filter_map_settings, DiffusionDialogue, State};
15+
use super::{filter_map_bot_state, filter_map_settings, DiffusionDialogue, State};
1616

1717
/// BotCommands for settings.
1818
#[derive(BotCommands, Clone)]
@@ -652,10 +652,13 @@ pub(crate) fn settings_schema() -> UpdateHandler<anyhow::Error> {
652652
.branch(filter_settings_state().endpoint(handle_invalid_setting_value));
653653

654654
dptree::entry()
655-
.branch(
656-
dptree::filter(|cfg: ConfigParameters| cfg.settings.disable_user_settings)
657-
.chain(admin_filter()),
658-
)
655+
.chain(dptree::filter(|cfg: ConfigParameters, upd: Update| {
656+
!cfg.settings.disable_user_settings
657+
|| upd
658+
.user()
659+
.map(|user| cfg.user_is_admin(&user.id.into()))
660+
.unwrap_or_default()
661+
}))
659662
.branch(settings_command_handler())
660663
.branch(message_handler)
661664
.branch(callback_handler)

crates/stable-diffusion-bot/src/bot/mod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl StableDiffusionBot {
109109
/// Creates an UpdateHandler for the bot
110110
fn schema() -> UpdateHandler<anyhow::Error> {
111111
Self::enter::<Update, ErasedStorage<State>, _>()
112+
.chain(dptree::filter_async(Self::set_my_commands))
112113
.branch(unauth_command_handler())
113114
.branch(authenticated_command_handler())
114115
}
@@ -153,6 +154,32 @@ impl StableDiffusionBot {
153154
)
154155
}
155156

157+
async fn set_my_commands(bot: Bot, cfg: ConfigParameters, upd: Update) -> bool {
158+
let (chat, user) = match (upd.chat(), upd.user()) {
159+
(Some(c), Some(u)) => (c, u),
160+
_ => return true,
161+
};
162+
let mut commands = UnauthenticatedCommands::bot_commands();
163+
if !cfg.settings.disable_user_settings || cfg.user_is_admin(&user.id.into()) {
164+
commands.extend(SettingsCommands::bot_commands());
165+
}
166+
commands.extend(GenCommands::bot_commands());
167+
let scope = if chat.id == user.id.into() {
168+
teloxide::types::BotCommandScope::Chat {
169+
chat_id: teloxide::types::Recipient::Id(chat.id),
170+
}
171+
} else {
172+
teloxide::types::BotCommandScope::ChatMember {
173+
chat_id: teloxide::types::Recipient::Id(chat.id),
174+
user_id: user.id,
175+
}
176+
};
177+
if let Err(e) = bot.set_my_commands(commands).scope(scope).await {
178+
error!("Failed to set commands: {e:?}");
179+
}
180+
true
181+
}
182+
156183
/// Runs the StableDiffusionBot
157184
pub async fn run(self) -> anyhow::Result<()> {
158185
let StableDiffusionBot {
@@ -162,7 +189,6 @@ impl StableDiffusionBot {
162189
} = self;
163190

164191
let mut commands = UnauthenticatedCommands::bot_commands();
165-
commands.extend(SettingsCommands::bot_commands());
166192
commands.extend(GenCommands::bot_commands());
167193
bot.set_my_commands(commands)
168194
.scope(teloxide::types::BotCommandScope::Default)

0 commit comments

Comments
 (0)