Skip to content

Commit

Permalink
Actually implement Kiai API stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
tazz4843 committed Nov 16, 2024
1 parent ce79d7f commit dfa9691
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripty_bot_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ scripty_premium = { path = "../scripty_premium" }
scripty_botlists = { path = "../scripty_botlists" }
scripty_data_type = { path = "../scripty_data_type" }
scripty_data_storage = { path = "../scripty_data_storage" }
scripty_integrations = { path = "../scripty_integrations" }
scripty_audio_handler = { path = "../scripty_audio_handler" }
tokio = { version = "1", features = ["parking_lot", "signal"] }
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
Expand Down
12 changes: 12 additions & 0 deletions scripty_bot_utils/src/error/error_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum ErrorEnum {
ExpectedPremiumValue,
AudioTranscription(GenericMessageError),
CallAlreadyExists,
KiaiError(scripty_integrations::kiai::KiaiApiError),
Custom(String),
}

Expand Down Expand Up @@ -230,6 +231,7 @@ impl Display for Error {
}
Custom(e) => format!("Custom error: {}", e).into(),
AudioTranscription(e) => format!("Failed to transcribe audio message: {}", e).into(),
KiaiError(e) => format!("Kiai API error: {}", e).into(),
CallAlreadyExists => "a call for this channel already exists - not trying to rejoin \
the same channel"
.into(),
Expand All @@ -254,6 +256,7 @@ impl StdError for Error {
Transcription(e) => Some(e),
ExpectedPremiumValue => None,
AudioTranscription(e) => Some(e),
KiaiError(e) => Some(e),
CallAlreadyExists => None,
Custom(_) => None,
}
Expand Down Expand Up @@ -382,3 +385,12 @@ impl From<scripty_redis::TransactionError> for Error {
}
}
}

impl From<scripty_integrations::kiai::KiaiApiError> for Error {
fn from(e: scripty_integrations::kiai::KiaiApiError) -> Self {
Self {
err: ErrorEnum::KiaiError(e),
bt: Backtrace::new(),
}
}
}
1 change: 1 addition & 0 deletions scripty_commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ scripty_automod = { path = "../scripty_automod" }
scripty_premium = { path = "../scripty_premium" }
scripty_bot_utils = { path = "../scripty_bot_utils" }
scripty_data_storage = { path = "../scripty_data_storage" }
scripty_integrations = { path = "../scripty_integrations" }
scripty_audio_handler = { path = "../scripty_audio_handler" }
tokio = { version = "1", features = ["parking_lot", "signal"] }
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "next", features = [
Expand Down
49 changes: 49 additions & 0 deletions scripty_commands/src/cmds/config/kiai_enabled.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use scripty_bot_utils::{checks::is_guild, Context, Error};
use scripty_integrations::kiai::{get_kiai_api_client, Permissions as KiaiPermissions};

/// Enable Scripty's Kiai integration. You should disable Kiai's voice XP levelling if you use this.
#[poise::command(
prefix_command,
slash_command,
check = "is_guild",
required_permissions = "MANAGE_GUILD",
rename = "enable_kiai"
)]
pub async fn config_enable_kiai(ctx: Context<'_>, enable_kiai: Option<bool>) -> Result<(), Error> {
let guild_id = ctx
.guild_id()
.map(|g| g.get())
.ok_or_else(Error::expected_guild)?;
let resolved_language =
scripty_i18n::get_resolved_language(ctx.author().id.get(), Some(guild_id)).await;

let i18n_string = if let Some(enable_kiai) = enable_kiai {
let kc = get_kiai_api_client();
let perms = kc.get_permissions(guild_id).await?;
if perms.contains(KiaiPermissions::LEVELS) {
sqlx::query!(
"INSERT INTO guilds (guild_id, kiai_enabled) VALUES ($1, $2) ON CONFLICT \
(guild_id) DO UPDATE SET kiai_enabled = $2",
guild_id as i64,
enable_kiai
)
.execute(scripty_db::get_db())
.await?;

if enable_kiai {
"config-kiai-enabled"
} else {
"config-kiai-disabled"
}
} else {
"config-kiai-missing-perms"
}
} else {
"config-kiai-info"
};

ctx.say(format_message!(resolved_language, i18n_string))
.await?;

Ok(())
}
2 changes: 2 additions & 0 deletions scripty_commands/src/cmds/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod auto_detect_lang;
mod kiai_enabled;
mod language;
mod transcribe_audio;
mod transcribe_only_role;
Expand All @@ -8,6 +9,7 @@ mod translate;
mod verbose;

pub use auto_detect_lang::config_auto_detect_lang;
pub use kiai_enabled::config_enable_kiai;
pub use language::config_server_language;
use poise::CreateReply;
use scripty_bot_utils::{checks::is_guild, Context, Error};
Expand Down
1 change: 1 addition & 0 deletions scripty_commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn build_commands() -> Vec<poise::Command<Data, Error>> {
cmds::config::config_auto_detect_lang(),
cmds::config::config_transcribe_only_role(),
cmds::config::config_translate(),
cmds::config::config_enable_kiai(),
],
subcommand_required: true,
..cmds::config::config_root()
Expand Down
12 changes: 12 additions & 0 deletions scripty_i18n/locales/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ config-translate-enabled = Scripty will now translate transcriptions to English.
config-translate-disabled = Scripty will now attempt to match the phrases being spoken to English words, but will not translate.
config-translate-not-english = You must set your language to English to enable translation. Do so with `{ $contextPrefix }config language en`.
## config - enable_kiai command
config_enable_kiai = enable_kiai
.description = Enable Scripty's Kiai integration. Run this command with no arguments to get info on Kiai.
.enable_kiai = enable_kiai
.enable_kiai-description = Defaults to false
config-kiai-enabled = Scripty will now send any voice XP gained to Kiai. Disable Kiai's voice XP leveling to prevent users getting double XP.
config-kiai-disabled = Scripty will no longer send any voice XP gained to Kiai's API.
config-kiai-info = You can find more info about Kiai at [https://www.kiai.app/](https://www.kiai.app/?utm_source=scripty_info).
{""}
If you use this integration, be sure to disable Kiai's voice XP module as they will conflict.
config-kiai-missing-perms = Scripty is missing permissions to work in this server. Authorize it with the `/application authorize` command, using an application ID of `811652199100317726`, and giving Scripty the "view and edit all levels and XP" permission.
## Help menu translation strings

command-not-found = No command with name `{ $commandName }` found.
Expand Down
12 changes: 11 additions & 1 deletion scripty_integrations/src/kiai/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{fmt, num::ParseIntError};

use serde::{Deserialize, Serialize};

Expand All @@ -15,8 +15,11 @@ pub enum KiaiApiError {
status: reqwest::StatusCode,
body: String,
},
BadInteger(ParseIntError),
}

impl std::error::Error for KiaiApiError {}

#[derive(Debug, Serialize, Deserialize)]
pub struct BadRequestPayload {
pub error: String,
Expand Down Expand Up @@ -56,7 +59,14 @@ impl fmt::Display for KiaiApiError {
Self::Unauthorized(msg) => write!(f, "Unauthorized: {}", msg),
Self::TooManyRequests(payload) => write!(f, "TooManyRequests: {}", payload.message),
Self::Unknown { status, body } => write!(f, "Unknown: {} {}", status, body),
Self::BadInteger(e) => write!(f, "Invalid integer received: {}", e),
}?;
f.write_str(" }")
}
}

impl From<ParseIntError> for KiaiApiError {
fn from(value: ParseIntError) -> Self {
Self::BadInteger(value)
}
}
17 changes: 16 additions & 1 deletion scripty_integrations/src/kiai/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use reqwest::{Client as ReqwestClient, StatusCode};

use crate::kiai::{KiaiApiError, KiaiApiResult, KiaiPostVirtualMessage};
use crate::kiai::{KiaiApiError, KiaiApiResult, KiaiPostVirtualMessage, Permissions};

#[derive(Debug)]
pub struct KiaiHttpClient {
Expand Down Expand Up @@ -33,6 +33,21 @@ impl KiaiHttpClient {
self.decode_response(res).await
}

pub async fn get_permissions(&self, guild_id: u64) -> KiaiApiResult<Permissions> {
let url = format!("https://api.kiaibot.com/v1/guild/{}/permissions", guild_id);

let res = self
.client
.get(&url)
.header(reqwest::header::AUTHORIZATION, &self.token)
.send()
.await?;

Ok(Permissions::from_bits_retain(
res.text().await?.trim().parse()?,
))
}

async fn decode_response<T>(&self, res: reqwest::Response) -> KiaiApiResult<T>
where
for<'de> T: serde::Deserialize<'de>,
Expand Down

0 comments on commit dfa9691

Please sign in to comment.