Integrated custom magick-rust, added basic db usage
This commit is contained in:
106
src/main.rs
106
src/main.rs
@ -4,6 +4,7 @@ extern crate lastfm;
|
||||
use serenity::async_trait;
|
||||
use serenity::builder::{CreateAttachment, CreateMessage};
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::model::id::UserId;
|
||||
use serenity::prelude::*;
|
||||
|
||||
extern crate magick_rust;
|
||||
@ -12,6 +13,9 @@ use magick_rust::{ColorspaceType, CompositeOperator, MagickWand, PixelWand, magi
|
||||
use std::env;
|
||||
use std::sync::Once;
|
||||
|
||||
extern crate sqlx;
|
||||
use sqlx::sqlite::SqlitePool;
|
||||
|
||||
static START: Once = Once::new();
|
||||
|
||||
struct Handler;
|
||||
@ -40,8 +44,15 @@ macro_rules! handle_magick_option {
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
async fn fmi(_arg: &str) -> Reply {
|
||||
let client = lastfm::Client::<String, &str>::from_env("seoxi");
|
||||
async fn fmi(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
||||
let lastfm_user = match arg {
|
||||
"" => get_lastfm_username(ctx, id).await,
|
||||
_ => Some(arg.to_string()),
|
||||
};
|
||||
let client = match lastfm_user {
|
||||
Some(s) => lastfm::Client::<String, String>::from_env(s),
|
||||
None => return Reply::Text("No last.fm username set.".to_string()),
|
||||
};
|
||||
let now_playing = match client.now_playing().await {
|
||||
Ok(np) => np,
|
||||
Err(e) => return Reply::Text(format!("Error: grabbing last.fm user data failed {e}")),
|
||||
@ -129,7 +140,16 @@ async fn fmi(_arg: &str) -> Reply {
|
||||
main_wand.compose_images(&art_wand, CompositeOperator::SrcOver, false, 12, 12),
|
||||
"Failed to combine images"
|
||||
);
|
||||
handle_magick_result!(main_wand.compose_images(&art_wand_cluster, CompositeOperator::SrcOver, false, 160, 12), "Failed to combine debug images");
|
||||
handle_magick_result!(
|
||||
main_wand.compose_images(
|
||||
&art_wand_cluster,
|
||||
CompositeOperator::SrcOver,
|
||||
false,
|
||||
160,
|
||||
12
|
||||
),
|
||||
"Failed to combine debug images"
|
||||
);
|
||||
handle_magick_result!(
|
||||
main_wand.compose_images(&mask_wand, CompositeOperator::SrcOver, false, 401, 0),
|
||||
"Failed to combine mask"
|
||||
@ -137,8 +157,9 @@ async fn fmi(_arg: &str) -> Reply {
|
||||
Reply::Image(main_wand.write_image_blob("png").unwrap())
|
||||
}
|
||||
|
||||
async fn set(arg: &str) -> Reply {
|
||||
Reply::Text(format!("set user {}", arg))
|
||||
async fn set(ctx: &Context, arg: &str, id: UserId) -> Reply {
|
||||
set_lastfm_username(ctx, id, arg.to_string()).await;
|
||||
Reply::Text(format!("set user {}", arg))
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@ -148,8 +169,8 @@ impl EventHandler for Handler {
|
||||
let cmd = cmd_iter.next().unwrap_or("");
|
||||
let arg = cmd_iter.next().unwrap_or("");
|
||||
let resp = match cmd {
|
||||
".fmi" => Some(fmi(arg).await),
|
||||
".set" => Some(set(arg).await),
|
||||
".fmi" => Some(fmi(&ctx, arg, msg.author.id).await),
|
||||
".set" => Some(set(&ctx, arg, msg.author.id).await),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(reply) = resp {
|
||||
@ -169,6 +190,56 @@ impl EventHandler for Handler {
|
||||
}
|
||||
}
|
||||
|
||||
struct PoolContainer;
|
||||
|
||||
impl TypeMapKey for PoolContainer {
|
||||
type Value = SqlitePool;
|
||||
}
|
||||
|
||||
struct DBResponse {
|
||||
lastfm_username: String,
|
||||
}
|
||||
|
||||
async fn get_lastfm_username(ctx: &Context, id: UserId) -> Option<String> {
|
||||
let data = ctx.data.write().await;
|
||||
let pool = data
|
||||
.get::<PoolContainer>()
|
||||
.expect("Failed to get pool container");
|
||||
let id = i64::from(id);
|
||||
let resp = sqlx::query_as!(
|
||||
DBResponse,
|
||||
r#"
|
||||
SELECT lastfm_username
|
||||
FROM users
|
||||
WHERE discord_userid = ?1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
match resp {
|
||||
Ok(r) => Some(r.lastfm_username),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_lastfm_username(ctx: &Context, id: UserId, user: String) {
|
||||
let data = ctx.data.write().await;
|
||||
let pool = data
|
||||
.get::<PoolContainer>()
|
||||
.expect("Failed to get pool container");
|
||||
let id = i64::from(id);
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO users
|
||||
VALUES (?1, ?2)
|
||||
"#,
|
||||
id, user
|
||||
)
|
||||
.execute(pool)
|
||||
.await.expect("Failed to insert");
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
START.call_once(|| {
|
||||
@ -176,23 +247,30 @@ async fn main() {
|
||||
magick_wand_genesis();
|
||||
});
|
||||
|
||||
// Login with a bot token from the environment
|
||||
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||
let db_url = env::var("DATABASE_URL").expect("Failed to load DATABASE_URL");
|
||||
let pool = SqlitePool::connect(&db_url)
|
||||
.await
|
||||
.expect("Failed to load db");
|
||||
|
||||
let token = env::var("DISCORD_TOKEN").expect("Failed to load DISCORD_TOKEN");
|
||||
// Set gateway intents, which decides what events the bot will be notified about
|
||||
let intents = GatewayIntents::GUILD_MESSAGES
|
||||
| GatewayIntents::DIRECT_MESSAGES
|
||||
| GatewayIntents::MESSAGE_CONTENT;
|
||||
|
||||
// Create a new instance of the Client, logging in as a bot.
|
||||
let mut client = Client::builder(&token, intents)
|
||||
.event_handler(Handler)
|
||||
.await
|
||||
.expect("Err creating client");
|
||||
|
||||
// Start listening for events by starting a single shard
|
||||
if let Err(why) = client.start().await {
|
||||
println!("Client error: {why:?}");
|
||||
{
|
||||
let mut data = client.data.write().await;
|
||||
data.insert::<PoolContainer>(pool);
|
||||
}
|
||||
|
||||
println!("watcat started");
|
||||
if let Err(why) = client.start().await {
|
||||
println!("Client error: {why:?}");
|
||||
} else {
|
||||
println!("watcat started");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user