+ Right now can just play voices + Added commands to add api keys + clippy + fmt
130 lines
3.5 KiB
Rust
130 lines
3.5 KiB
Rust
use crate::config::BotConfig;
|
|
use crate::models::api_key::Apikey;
|
|
use crate::{command, group, GlobalData};
|
|
use json::JsonValue;
|
|
use serenity::client::Context;
|
|
use serenity::framework::standard::{Args, CommandResult};
|
|
use serenity::model::channel::{AttachmentType, Message};
|
|
use serenity::model::prelude::UserId;
|
|
use std::borrow::Cow;
|
|
|
|
#[group]
|
|
#[commands(reload, dump_db, load_db, add_key)]
|
|
pub struct ADMIN;
|
|
|
|
pub fn is_admin(user_id: &UserId, cfg: &BotConfig) -> bool {
|
|
cfg.admins.contains(user_id)
|
|
}
|
|
|
|
#[command]
|
|
async fn reload(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
|
let mut data = ctx.data.write().await;
|
|
let global_data = data.get_mut::<GlobalData>().unwrap();
|
|
|
|
if !is_admin(&msg.author.id, &global_data.cfg) {
|
|
return Ok(());
|
|
}
|
|
|
|
global_data.reload().await?;
|
|
msg.reply(&ctx.http, "Reload done ;)").await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[command]
|
|
async fn dump_db(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
|
let mut data = ctx.data.write().await;
|
|
let global_data = data.get_mut::<GlobalData>().unwrap();
|
|
|
|
if !is_admin(&msg.author.id, &global_data.cfg) {
|
|
return Ok(());
|
|
}
|
|
|
|
let db_dump = global_data.db.dump_db()?;
|
|
|
|
let output = db_dump.pretty(4);
|
|
|
|
msg.author
|
|
.id
|
|
.create_dm_channel(&ctx.http)
|
|
.await?
|
|
.send_message(&ctx.http, |m| {
|
|
m.content("The current DB state")
|
|
.add_file(AttachmentType::Bytes {
|
|
data: Cow::from(output.as_bytes()),
|
|
filename: "db.json".to_string(),
|
|
})
|
|
})
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[command]
|
|
async fn load_db(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
|
let mut data = ctx.data.write().await;
|
|
let global_data = data.get_mut::<GlobalData>().unwrap();
|
|
|
|
if !is_admin(&msg.author.id, &global_data.cfg) {
|
|
return Ok(());
|
|
}
|
|
|
|
if let Some(attachment) = msg.attachments.first() {
|
|
let db_bytes = attachment.download().await?;
|
|
let db_string = String::from_utf8(db_bytes)?;
|
|
|
|
let json_value: JsonValue = match json::parse(&db_string) {
|
|
Ok(v) => v,
|
|
Err(e) => {
|
|
msg.reply(&ctx.http, format!("Error parsing json: {}", e))
|
|
.await?;
|
|
return Ok(());
|
|
}
|
|
};
|
|
|
|
match global_data.db.import_db(json_value) {
|
|
Ok(_) => {
|
|
msg.reply(&ctx.http, "Database imported successfully")
|
|
.await?
|
|
}
|
|
Err(err) => {
|
|
msg.reply(&ctx.http, format!("Error importing db: {}", err))
|
|
.await?;
|
|
return Ok(());
|
|
}
|
|
};
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[command]
|
|
async fn add_key(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
|
let mut data = ctx.data.write().await;
|
|
let global_data = data.get_mut::<GlobalData>().unwrap();
|
|
|
|
if !is_admin(&msg.author.id, &global_data.cfg) {
|
|
return Ok(());
|
|
}
|
|
|
|
let (api_key, key) = if args.len() == 1 {
|
|
let user_id = args.parse::<UserId>()?;
|
|
let user = user_id.to_user(&ctx.http).await?;
|
|
Apikey::new(&format!("{}'s Key", user.name), Some(user_id))
|
|
} else {
|
|
Apikey::new(&format!("{}'s Key", msg.author.name), Some(msg.author.id))
|
|
};
|
|
|
|
global_data.db.insert::<Apikey>(api_key.clone())?;
|
|
|
|
let dm = msg.author.create_dm_channel(&ctx.http).await?;
|
|
|
|
dm.say(
|
|
&ctx.http,
|
|
format!("Key '{}' added. Api Key: {}", api_key.name, key),
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|