From 2416a28b090c4a7bc94bcda0a9f5a9a5e383390c Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Mon, 14 Jun 2021 12:58:00 +0200 Subject: [PATCH] can almost create message --- Cargo.lock | 28 -------------------- Cargo.toml | 2 -- rustfmt.toml | 1 + src/autorole.rs | 68 ++++++++++++++++++++++++++++++++----------------- src/commands.rs | 7 +---- src/main.rs | 8 +++--- 6 files changed, 51 insertions(+), 63 deletions(-) create mode 100644 rustfmt.toml diff --git a/Cargo.lock b/Cargo.lock index cc065dc..edad3d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,15 +6,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "aho-corasick" -version = "0.7.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" -dependencies = [ - "memchr", -] - [[package]] name = "async-trait" version = "0.1.50" @@ -465,8 +456,6 @@ dependencies = [ name = "killjoy_turret" version = "0.1.0" dependencies = [ - "lazy_static", - "regex", "serde", "serde_json", "serenity", @@ -719,23 +708,6 @@ dependencies = [ "rand_core", ] -[[package]] -name = "regex" -version = "1.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" - [[package]] name = "reqwest" version = "0.11.3" diff --git a/Cargo.toml b/Cargo.toml index 8e298a6..973882c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,6 @@ edition = "2018" tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -regex = "1.5.4" -lazy_static = "1.4.0" [dependencies.serenity] version = "0.10.7" diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..866c756 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +max_width = 120 \ No newline at end of file diff --git a/src/autorole.rs b/src/autorole.rs index 8305dfe..41f0fff 100644 --- a/src/autorole.rs +++ b/src/autorole.rs @@ -5,15 +5,6 @@ use serenity::framework::standard::{ }; use serenity::model::prelude::*; use serenity::prelude::*; -use serenity::{ - async_trait, - collector::MessageCollectorBuilder, - framework::standard::{help_commands, CommandGroup, HelpOptions, StandardFramework}, - futures::stream::StreamExt, - http::Http, - model::prelude::*, - prelude::*, -}; use std::collections::HashMap; use std::time::Duration; @@ -28,13 +19,26 @@ impl TypeMapKey for AutoRoleDataKey { #[description = "Auto role related commands"] struct AutoRole; +const DEFAULT_MESSAGE: &'static str = "Select your role"; + #[command] +#[aliases("a")] #[description = "Add auto assign roles"] #[usage = "sdakhnfj"] -async fn autorole(ctx: &Context, msg: &Message, args: Args) -> CommandResult { +async fn autorole(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let http = &ctx.http; - let mut data = ctx.data.write().await; - let mut auto_roles = data.get_mut::(); + + let target_channel = args.single::(); + args.quoted(); + let message = args.single::().unwrap_or(DEFAULT_MESSAGE.to_string()); + + if let Err(_) = target_channel { + msg.channel_id + .say(http, "You need to mention the channel the message should be sent in") + .await?; + return Ok(()); + } + let target_channel = target_channel.unwrap(); msg.channel_id .send_message(http, |m| { @@ -51,22 +55,40 @@ async fn autorole(ctx: &Context, msg: &Message, args: Args) -> CommandResult { }) .await?; - while let Some(answer) = &msg - .author - .await_reply(&ctx) - .timeout(Duration::from_secs(30)) - .await - { - if let Some(emote) = answer.content.split(' ').next() { - lazy_static! { + let mut roles = vec![]; + while let Some(answer) = &msg.author.await_reply(&ctx).timeout(Duration::from_secs(30)).await { + if answer.content.contains("cancel") { + return Ok(()); + } + if let Some(role) = answer.mention_roles.get(0) { + if let Some(emote) = answer.content.split(' ').next() { + println!("should add emote: {} for role {}", emote, role.0); + answer.react(http, '☑').await?; + + roles.push((emote.to_string(), role.0)); + } else { + break; } - - println!("should add emote: {}", emote); - answer.react(http, '☑').await?; + } else { + break; } } + msg.channel_id.say(http, "done").await?; + + target_channel + .send_message(http, |m| { + m.embed(|e| { + e.title(message); + e.field("neutral", "same", false) + }) + }) + .await?; + + let mut data = ctx.data.write().await; + let mut auto_roles = data.get_mut::().unwrap(); + Ok(()) } diff --git a/src/commands.rs b/src/commands.rs index 466621e..3c9c6cf 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,14 +1,9 @@ use std::collections::HashSet; use serenity::client::Context; -use serenity::framework::standard::{ - help_commands, - macros::{command, group, help}, - Args, CommandGroup, CommandResult, HelpOptions, -}; +use serenity::framework::standard::{help_commands, macros::help, Args, CommandGroup, CommandResult, HelpOptions}; use serenity::model::channel::Message; use serenity::model::id::UserId; -use serenity::utils::{content_safe, ContentSafeOptions}; #[help] #[individual_command_tip = "Killjoy Turret Command Info"] diff --git a/src/main.rs b/src/main.rs index 40bb034..53533fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,16 +2,16 @@ mod autorole; mod commands; mod general; -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use std::fs; +use crate::autorole::{AutoRoleData, AutoRoleDataKey, AUTOROLE_GROUP}; +use crate::commands::MY_HELP; use serenity::client::Context; use serenity::framework::StandardFramework; use serenity::http::Http; -use serenity::model::id::{ChannelId, UserId}; +use serenity::model::id::UserId; use serenity::{async_trait, model::gateway::Ready, prelude::*}; -use crate::autorole::{AUTOROLE_GROUP, AutoRoleDataKey, AutoRoleData}; -use crate::commands::MY_HELP; struct Handler;