discord-kurante/src/util/mod.rs

47 lines
1.3 KiB
Rust

use lazy_static::lazy_static;
use serenity::{
http::Http,
model::{channel::Message, id::ChannelId},
};
use std::env::var;
pub mod db;
pub mod error;
lazy_static! {
static ref BOT_OWNER_ID: u64 = var("BOT_OWNER_ID")
.unwrap_or_else(|_| {
log::warn!("`BOT_OWNER_ID` env var missing, bot will never respond in debug mode!");
String::new()
})
.parse()
.unwrap_or_else(|_| {
log::error!(
"`BOT_OWNER_ID` env var is not a valid u64! Bot will never respond in debug mode!"
);
0
});
}
pub async fn debug_say(
msg: &Message,
ctx: impl AsRef<Http>,
resp: impl std::fmt::Display,
) -> Result<Option<Message>, serenity::Error> {
debug_channel_say(&msg.channel_id, *msg.author.id.as_u64(), ctx, resp).await
}
pub async fn debug_channel_say(
channel: &ChannelId,
author_id: u64,
ctx: impl AsRef<Http>,
resp: impl std::fmt::Display,
) -> Result<Option<Message>, serenity::Error> {
if cfg!(debug_assertions) && author_id == *BOT_OWNER_ID {
Ok(Some(channel.say(ctx, format!("DEBUG: {}", resp)).await?))
} else if !cfg!(debug_assertions) {
Ok(Some(channel.say(ctx, resp).await?))
} else {
Ok(None)
}
}