Add context types to other commands

This commit is contained in:
toast 2024-12-07 13:29:26 +11:00
parent 5417dcaa1d
commit a7f67c1ec8
4 changed files with 26 additions and 18 deletions

View File

@ -1,3 +1,8 @@
mod ilo;
mod midi;
mod status;
mod uptime;
use kon_libs::{ use kon_libs::{
KonData, KonData,
KonError, KonError,
@ -5,13 +10,6 @@ use kon_libs::{
PoiseCtx PoiseCtx
}; };
use poise::Command;
mod ilo;
mod midi;
mod status;
mod uptime;
use { use {
ilo::ilo, ilo::ilo,
midi::midi_to_wav, midi::midi_to_wav,
@ -25,7 +23,7 @@ macro_rules! commands {
} }
} }
pub fn register_cmds() -> Vec<Command<KonData, KonError>> { commands!(deploy, ping, midi_to_wav, status, ilo, uptime) } pub fn register_cmds() -> Vec<poise::Command<KonData, KonError>> { commands!(deploy, ping, ilo, midi_to_wav, status, uptime) }
/// Deploy the commands globally or in a guild /// Deploy the commands globally or in a guild
#[poise::command(prefix_command, owners_only, guild_only)] #[poise::command(prefix_command, owners_only, guild_only)]
@ -35,7 +33,7 @@ pub async fn deploy(ctx: PoiseCtx<'_>) -> KonResult<()> {
} }
/// Check if the bot is alive /// Check if the bot is alive
#[poise::command(slash_command)] #[poise::command(slash_command, install_context = "Guild|User", interaction_context = "Guild|BotDm|PrivateChannel")]
pub async fn ping(ctx: PoiseCtx<'_>) -> KonResult<()> { pub async fn ping(ctx: PoiseCtx<'_>) -> KonResult<()> {
ctx.reply(format!("Powong! `{:.2?}`", ctx.ping().await)).await?; ctx.reply(format!("Powong! `{:.2?}`", ctx.ping().await)).await?;
Ok(()) Ok(())

View File

@ -7,7 +7,10 @@ use {
}, },
poise::{ poise::{
CreateReply, CreateReply,
serenity_prelude::CreateAttachment serenity_prelude::{
CreateAttachment,
Message
}
}, },
regex::Regex, regex::Regex,
std::{ std::{
@ -23,12 +26,12 @@ use {
/// Convert MIDI file to WAV /// Convert MIDI file to WAV
#[poise::command( #[poise::command(
context_menu_command = "MIDI -> WAV", context_menu_command = "MIDI -> WAV",
install_context = "User", install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel" interaction_context = "Guild|BotDm|PrivateChannel"
)] )]
pub async fn midi_to_wav( pub async fn midi_to_wav(
ctx: super::PoiseCtx<'_>, ctx: super::PoiseCtx<'_>,
#[description = "MIDI file to be converted"] message: poise::serenity_prelude::Message #[description = "MIDI file to be converted"] message: Message
) -> KonResult<()> { ) -> KonResult<()> {
let re = Regex::new(r"(?i)\.mid$").unwrap(); let re = Regex::new(r"(?i)\.mid$").unwrap();
@ -87,9 +90,12 @@ pub async fn midi_to_wav(
&*wav_path, &*wav_path,
format_bytes(metadata(&*wav_path).unwrap().size()) format_bytes(metadata(&*wav_path).unwrap().size())
))) )))
.await .await?;
.unwrap();
} else if reply.is_ok() { } else if reply.is_ok() {
println!(
"Discord[{}]: Processed file uploaded back to Discord channel",
ctx.command().qualified_name
);
remove_file(midi_path)?; remove_file(midi_path)?;
remove_file(&*wav_path)?; remove_file(&*wav_path)?;
} }
@ -97,8 +103,7 @@ pub async fn midi_to_wav(
Err(y) => { Err(y) => {
ctx ctx
.send(CreateReply::default().content("Command didn't execute successfully, check console for more information!")) .send(CreateReply::default().content("Command didn't execute successfully, check console for more information!"))
.await .await?;
.unwrap();
return Err(KonError::from(format!("Midi conversion failed: {y}"))) return Err(KonError::from(format!("Midi conversion failed: {y}")))
} }

View File

@ -73,7 +73,12 @@ fn process_pms_statuses(servers: Vec<(String, Vec<Value>)>) -> Vec<(String, Stri
} }
/// Query the server statuses /// Query the server statuses
#[poise::command(slash_command, subcommands("wg"))] #[poise::command(
slash_command,
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
subcommands("wg")
)]
pub async fn status(_: super::PoiseCtx<'_>) -> KonResult<()> { Ok(()) } pub async fn status(_: super::PoiseCtx<'_>) -> KonResult<()> { Ok(()) }
/// Retrieve the server statuses from Wargaming /// Retrieve the server statuses from Wargaming

View File

@ -46,7 +46,7 @@ fn get_os_info() -> String {
} }
/// Retrieve host and bot uptimes /// Retrieve host and bot uptimes
#[poise::command(slash_command)] #[poise::command(slash_command, install_context = "Guild|User", interaction_context = "Guild|BotDm|PrivateChannel")]
pub async fn uptime(ctx: super::PoiseCtx<'_>) -> KonResult<()> { pub async fn uptime(ctx: super::PoiseCtx<'_>) -> KonResult<()> {
let bot = ctx.http().get_current_user().await.unwrap(); let bot = ctx.http().get_current_user().await.unwrap();
let mut sys = System::new_all(); let mut sys = System::new_all();