discord-kurante/src/util/mod.rs

97 lines
2.7 KiB
Rust

use lazy_static::lazy_static;
use serenity::{
http::Http,
model::{channel::Message, id::ChannelId},
};
use std::{borrow::Cow, env::var};
pub mod db;
pub mod error;
pub mod operators;
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_say_cow(Cow::Borrowed(msg), ctx, resp).await
}
pub async fn debug_say_owned(
msg: Message,
ctx: impl AsRef<Http>,
resp: impl std::fmt::Display,
) -> Result<Option<Message>, serenity::Error> {
debug_say_cow(Cow::Owned(msg), ctx, resp).await
}
async fn debug_say_cow(
msg: Cow<'_, 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: impl Into<ChannelId>,
author_id: impl Into<u64>,
ctx: impl AsRef<Http>,
resp: impl std::fmt::Display,
) -> Result<Option<Message>, serenity::Error> {
let channel: ChannelId = channel.into();
if cfg!(debug_assertions) && author_id.into() == *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)
}
}
// pub async fn debug_send_message<'a, F>(
// channel: impl Into<ChannelId>,
// author_id: impl Into<u64>,
// http: impl AsRef<Http>,
// f: F,
// ) -> Result<Option<Message>, serenity::Error>
// where
// for<'b> F: FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a>,
// {
// let channel: ChannelId = channel.into();
// if cfg!(debug_assertions) && author_id.into() == *BOT_OWNER_ID {
// Ok(Some(
// channel
// .send_message(http, |mut m| {
// let resp = f(m);
// // let cur_content = resp.0.get("content");
// f(m)
// })
// .await?,
// ))
// } else if !cfg!(debug_assertions) {
// Ok(Some(channel.send_message(http, f).await?))
// } else {
// Ok(None)
// }
// }
pub fn is_bot_owner(id: impl Into<u64>) -> bool {
id.into() == *BOT_OWNER_ID
}