Initial commit
+ Working color and album commands + Clippy + fmt
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
use crate::config::AlbumConfig;
|
||||
use crate::{command, group, GlobalData};
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::{Args, CommandResult};
|
||||
use serenity::model::channel::Message;
|
||||
|
||||
#[group]
|
||||
#[commands(add_album, remove_album, list_albums)]
|
||||
pub struct Album;
|
||||
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
#[min_args(2)]
|
||||
#[max_args(2)]
|
||||
async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
let album_name = args.parse::<String>()?;
|
||||
args.advance();
|
||||
let album_id = args.parse::<String>()?;
|
||||
|
||||
let mut data = ctx.data.write().await;
|
||||
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
global_data.cfg.albums.push(AlbumConfig {
|
||||
album_id,
|
||||
name: album_name.clone(),
|
||||
});
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
.save(&global_data.args.cfg_path)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
msg.reply(&ctx.http, format!("{} album added!", album_name))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
#[max_args(1)]
|
||||
async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
let album_name = args.parse::<String>()?;
|
||||
|
||||
let mut data = ctx.data.write().await;
|
||||
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
.albums
|
||||
.retain(|album| !album.name.eq_ignore_ascii_case(&album_name));
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
.save(&global_data.args.cfg_path)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
msg.reply(&ctx.http, format!("{} album removed!", album_name))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
#[aliases("albums")]
|
||||
#[only_in(guilds)]
|
||||
async fn list_albums(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let data = ctx.data.read().await;
|
||||
|
||||
let global_data = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let album_names: Vec<String> = global_data
|
||||
.cfg
|
||||
.albums
|
||||
.iter()
|
||||
.map(|album| album.name.clone())
|
||||
.collect();
|
||||
|
||||
if album_names.is_empty() {
|
||||
msg.reply(&ctx.http, "There are no albums configured!")
|
||||
.await?;
|
||||
} else {
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
format!("**Albums**:\n{}", album_names.join("\n")),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
use crate::{command, group};
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::{Args, CommandResult};
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::utils::Colour;
|
||||
|
||||
#[group]
|
||||
#[commands(set_color, remove_color)]
|
||||
pub struct Color;
|
||||
|
||||
#[command]
|
||||
#[example("#35BB1D")]
|
||||
#[example("0x35BB1D")]
|
||||
#[example("53 187 29")]
|
||||
#[min_args(1)]
|
||||
#[max_args(3)]
|
||||
#[only_in(guilds)]
|
||||
async fn set_color(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
let color = if args.len() == 3 {
|
||||
Colour::from_rgb(
|
||||
args.parse::<u8>()?,
|
||||
args.parse::<u8>()?,
|
||||
args.parse::<u8>()?,
|
||||
)
|
||||
} else {
|
||||
let color_str = args.rest();
|
||||
|
||||
if color_str.starts_with("0x") || color_str.starts_with('#') {
|
||||
let color_str = color_str.replace("0x", "").replace('#', "");
|
||||
let val = u32::from_str_radix(&color_str, 16)?;
|
||||
|
||||
Colour::new(val)
|
||||
} else {
|
||||
Colour::new(color_str.parse()?)
|
||||
}
|
||||
};
|
||||
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
|
||||
let (_, member) = guild
|
||||
.members
|
||||
.iter()
|
||||
.find(|(id, _)| id == &&msg.author.id)
|
||||
.unwrap();
|
||||
|
||||
let role = if let Some(role) = member
|
||||
.roles(&ctx.cache)
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.find(|r| r.name.contains("COwOlor"))
|
||||
{
|
||||
role.clone()
|
||||
} else {
|
||||
guild
|
||||
.create_role(&ctx.http, |r| {
|
||||
r.name(&format!("{} COwOlor", member.user.name))
|
||||
})
|
||||
.await?
|
||||
};
|
||||
|
||||
guild
|
||||
.edit_role(&ctx.http, role.id, |r| {
|
||||
r.position(255).hoist(true).colour(color.0 as u64)
|
||||
})
|
||||
.await?;
|
||||
|
||||
let len = guild.roles.len() - 1;
|
||||
guild
|
||||
.edit_role_position(&ctx.http, role.id, len as u64)
|
||||
.await?;
|
||||
|
||||
member.clone().add_role(&ctx.http, role).await?;
|
||||
|
||||
msg.reply(&ctx.http, "Color set!").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
#[max_args(0)]
|
||||
#[only_in(guilds)]
|
||||
async fn remove_color(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
|
||||
let (_, member) = guild
|
||||
.members
|
||||
.iter()
|
||||
.find(|(id, _)| id == &&msg.author.id)
|
||||
.unwrap();
|
||||
|
||||
if let Some(role) = member
|
||||
.roles(&ctx.cache)
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.find(|r| r.name.contains("COwOlor"))
|
||||
{
|
||||
role.clone().delete(&ctx.http).await?;
|
||||
}
|
||||
|
||||
msg.reply(&ctx.http, "Color removed!").await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
pub mod album;
|
||||
pub mod color;
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::{help, hook, imgur, GlobalData};
|
||||
use rand::prelude::SliceRandom;
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::{
|
||||
help_commands, Args, CommandGroup, CommandResult, HelpOptions,
|
||||
};
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::model::id::UserId;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[hook]
|
||||
pub async fn after(
|
||||
_ctx: &Context,
|
||||
_msg: &Message,
|
||||
command_name: &str,
|
||||
command_result: CommandResult,
|
||||
) {
|
||||
match command_result {
|
||||
Ok(()) => println!("Processed command '{}'", command_name),
|
||||
Err(why) => println!("Command '{}' returned error {:?}", command_name, why),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn parse_album(ctx: &Context, msg: &Message, album_name: &str) -> Result<(), Error> {
|
||||
let data = ctx.data.read().await;
|
||||
let global_data = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let album = global_data
|
||||
.cfg
|
||||
.albums
|
||||
.iter()
|
||||
.find(|album| album.name.to_lowercase() == album_name);
|
||||
|
||||
if let Some(album) = album {
|
||||
match imgur::get_album_images(&global_data.cfg.imgur_client_id, &album.album_id).await {
|
||||
Ok(album) => {
|
||||
let image = {
|
||||
let mut rng = rand::thread_rng();
|
||||
album.choose(&mut rng)
|
||||
};
|
||||
|
||||
if let Some(image) = image {
|
||||
msg.reply(&ctx.http, &image.link).await?;
|
||||
} else {
|
||||
msg.reply(&ctx.http, "Album empty, try and add some images.")
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
msg.reply(&ctx.http, "Unable to get album, try again later.")
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[hook]
|
||||
pub async fn unrecognised_command_hook(
|
||||
ctx: &Context,
|
||||
msg: &Message,
|
||||
unrecognised_command_name: &str,
|
||||
) {
|
||||
if let Err(e) = parse_album(ctx, msg, unrecognised_command_name).await {
|
||||
println!("Error processing album command: {}", e)
|
||||
}
|
||||
}
|
||||
|
||||
#[help]
|
||||
pub async fn my_help(
|
||||
context: &Context,
|
||||
msg: &Message,
|
||||
args: Args,
|
||||
help_options: &'static HelpOptions,
|
||||
groups: &[&'static CommandGroup],
|
||||
owners: HashSet<UserId>,
|
||||
) -> CommandResult {
|
||||
let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user