From cd8ed8bc5793985df2c1692072a932ceee517b95 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 8 Jul 2026 02:13:35 +0200 Subject: OPEN THE RUST GATES Signed-off-by: salmonik --- src/commands/ai.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/commands/ai.rs (limited to 'src/commands/ai.rs') diff --git a/src/commands/ai.rs b/src/commands/ai.rs new file mode 100644 index 0000000..a75f164 --- /dev/null +++ b/src/commands/ai.rs @@ -0,0 +1,76 @@ +use crate::Data; +use crate::error::Error; +use crate::lang; + +type Context<'a> = poise::Context<'a, Data, Error>; + +/// Query the AI senator via OpenRouter +#[poise::command(slash_command)] +pub async fn senator( + ctx: Context<'_>, + #[description = "Wiadomość do wysłania"] text: String, + #[description = "Przemówić w kanale głosowym?"] tts_vc: Option, +) -> crate::error::Result<()> { + ctx.defer().await?; + + let api_key = ctx + .data() + .config + .openrouter_api_key + .as_ref() + .ok_or_else(|| Error::Custom(lang::L_OPENROUTER_NO_KEY.into()))?; + let model = ctx + .data() + .config + .openrouter_model + .as_deref() + .unwrap_or("cognitivecomputations/dolphin-mistral-24b-venice-edition:free"); + + let client = crate::openrouter::OpenRouterClient::new(api_key.clone(), model.to_string()); + let response = client.chat(lang::SENATOR_SYSTEM_PROMPT, &text).await?; + + if tts_vc.unwrap_or(false) { + let guild_id = ctx.guild_id().ok_or_else(|| lang::err_not_in_guild())?; + let channel_id = ctx + .serenity_context() + .cache + .guild(guild_id) + .and_then(|g| { + g.voice_states + .get(&ctx.author().id) + .and_then(|vs| vs.channel_id) + }) + .ok_or_else(|| lang::err_not_in_voice())?; + + let tts_url = format!( + "https://translate.google.com/translate_tts?ie=UTF-8&q={}&tl=pl&client=tw-ob", + crate::commands::tts::urlencoding(&response) + ); + let audio_data = ctx + .data() + .http_client + .get(&tts_url) + .send() + .await? + .bytes() + .await?; + + let call = ctx + .data() + .voice_manager + .connect(guild_id, channel_id) + .await?; + let input = crate::voice::decode_audio(&audio_data)?; + let mut call_lock = call.lock().await; + if call_lock.current_channel().is_some() { + call_lock.play_input(input); + } + } + + ctx.say(response).await?; + Ok(()) +} + +pub fn commands() -> Vec> { + vec![senator()] +} -- cgit v1.2.3