diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 39d7c6c..0b56f3d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,6 @@ // use serenity::async_trait; use serenity::prelude::*; -use serenity::model::channel::Message; +use serenity::model::channel::{Message, Reaction}; // use serenity::model::gateway::Ready; // use serenity::model::prelude::*; @@ -10,6 +10,12 @@ pub mod sm_server; use crate::utils; +pub async fn initialize(ctx: Context) -> Result<(), String> +{ + + Ok(()) +} + pub async fn parse(ctx: Context, msg: Message) { if let Some(server_id) = msg.guild_id @@ -36,4 +42,27 @@ pub async fn parse(ctx: Context, msg: Message) { utils::Logger::log_message(ctx, &format!("DM Received from {}: {}", msg.author.name, msg.content)).await; } +} + +pub async fn handle_reaction_add(ctx: Context, add_reaction: Reaction) +{ + if let Some(server_id) = add_reaction.guild_id + { + if server_id == utils::ADMIN_SERVER_ID && add_reaction.channel_id == utils::ADMIN_COMMAND_CHANNEL_ID + { + //test::handle_reaction_add(ctx, add_reaction, server_id).await; + return; + } + + if server_id == utils::SM_SERVER_ID + { + sm_server::handle_reaction_add(ctx, add_reaction).await; + return; + } + } +} + +pub async fn handle_reaction_remove(ctx: Context, removed_reaction: Reaction) +{ + } \ No newline at end of file diff --git a/src/commands/sm_server.rs b/src/commands/sm_server.rs index db87502..2ce2f24 100644 --- a/src/commands/sm_server.rs +++ b/src/commands/sm_server.rs @@ -3,7 +3,9 @@ use serenity::prelude::*; use serenity::model::prelude::*; use serenity::model::channel::Message; -use crate::utils; +use crate::{utils, Owner}; + +const SM_TEST_CHANNEL_ID: u64 = 280548893135994880; // COLOR ROLE IDS const ROLE_ID_RED: u64 = 279338933748236289; @@ -14,6 +16,9 @@ const ROLE_ID_PINK: u64 = 279339051444862976; const ROLE_ID_ORANGE: u64 = 279339077382438912; const ROLE_ID_PURPLE: u64 = 284310454908747787; +// RACE ROLE IDS + + pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) { @@ -27,17 +32,36 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) let mut command_iter = command_lower.split_whitespace(); let command_start = command_iter.next(); + let data = ctx.data.read().await; + let owner_id = match data.get::() + { + Some(o) => o.load(std::sync::atomic::Ordering::Relaxed), + None => { utils::Logger::log_error(ctx.clone(), &format!("Couldn't get bot owner id")).await; return; } + }; + + if UserId::from(owner_id) == msg.author.id && command_start == Some(".dotestmsg") + { + if let Some(msg) = utils::send_msg(ctx.clone(), ChannelId::from(SM_TEST_CHANNEL_ID), "Test message!").await + { + println!("Test message id is: {:#?}", msg.id) + } + else + { + println!("Failed to get test message id"); + } + } + if command_start == Some(".randomizer") { let item = utils::get_random_item(); - utils::send_msg(ctx, msg.channel_id, &format!("You found {}!", item)).await; + utils::send_msg(ctx.clone(), msg.channel_id, &format!("You found {}!", item)).await; return; } if command_start == Some(".colors") || command_start == Some(".color") { - utils::send_msg(ctx, msg.channel_id, "Valid colors are: green blue red purple pink yellow orange").await; + utils::send_msg(ctx.clone(), msg.channel_id, "Valid colors are: green blue red purple pink yellow orange").await; return; } @@ -48,20 +72,25 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) match arg { // TODO: add color roles - "red" => { set_color(ctx, msg, RoleId(ROLE_ID_RED), server_id).await; return; } - "green" => { set_color(ctx, msg, RoleId(ROLE_ID_GREEN), server_id).await; return; } - "blue" => { set_color(ctx, msg, RoleId(ROLE_ID_BLUE), server_id).await; return; } - "yellow" => { set_color(ctx, msg, RoleId(ROLE_ID_YELLOW), server_id).await; return; } - "orange" => { set_color(ctx, msg, RoleId(ROLE_ID_ORANGE), server_id).await; return; } - "pink" => { set_color(ctx, msg, RoleId(ROLE_ID_PINK), server_id).await; return; } - "purple" => { set_color(ctx, msg, RoleId(ROLE_ID_PURPLE), server_id).await; return; } - _ => utils::Logger::log_error(ctx, &format!("unknown .color argument: {}", arg)).await + "red" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_RED), server_id).await; return; } + "green" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_GREEN), server_id).await; return; } + "blue" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_BLUE), server_id).await; return; } + "yellow" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_YELLOW), server_id).await; return; } + "orange" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_ORANGE), server_id).await; return; } + "pink" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_PINK), server_id).await; return; } + "purple" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_PURPLE), server_id).await; return; } + _ => utils::Logger::log_error(ctx.clone(), &format!("unknown .color argument: {}", arg)).await }; } } } +pub async fn handle_reaction_add(ctx: Context, reaction_add: Reaction) +{ + +} + ///////////////////// HELPERS async fn set_color(ctx: Context, msg: Message, role_id: RoleId, server_id: GuildId) { diff --git a/src/commands/test.rs b/src/commands/test.rs index 3674992..8c6b077 100644 --- a/src/commands/test.rs +++ b/src/commands/test.rs @@ -34,12 +34,14 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) { if let Some(arg) = command_iter.next() { - match arg + if arg == "red" { - "red" => utils::send_msg(ctx, msg.channel_id, "arg was red!").await, - - _ => utils::Logger::log_error(ctx, &format!("unknown .argstest argument: {}", arg)).await - }; + utils::send_msg(ctx, msg.channel_id, "arg was red!").await.unwrap(); + } + else + { + utils::Logger::log_error(ctx, &format!("unknown .argstest argument: {}", arg)).await + } } return; } @@ -49,8 +51,8 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) let test_role = RoleId(1129106677619228702 as u64); match utils::add_role(ctx.clone(), msg.author.id, test_role, server_id).await { - Ok(role) => utils::send_msg(ctx, msg.channel_id, &format!("Added role {:?}", role.name)).await, - Err(why) => utils::Logger::log_error(ctx, &format!("failed to add role to user - {}", why)).await, + Ok(role) => { utils::send_msg(ctx, msg.channel_id, &format!("Added role {:?}", role.name)).await.unwrap(); } + Err(why) => { utils::Logger::log_error(ctx, &format!("failed to add role to user - {}", why)).await; } } return; diff --git a/src/main.rs b/src/main.rs index 81e2b70..76735c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,9 +46,11 @@ impl EventHandler for Handler async fn reaction_add(&self, ctx: Context, add_reaction: Reaction) { - println!("reaction_add event"); - utils::Logger::log_message(ctx, &format!("reaction add event: {:#?}", add_reaction)).await; + // println!("reaction_add event"); + // utils::Logger::log_message(ctx, &format!("reaction add event: {:#?}", add_reaction)).await; // todo!() + + commands::handle_reaction_add(ctx, add_reaction).await; } async fn reaction_remove(&self, ctx: Context, removed_reaction: Reaction) @@ -66,7 +68,16 @@ impl EventHandler for Handler async fn ready(&self, ctx: Context, ready: Ready) { println!("{} is connected!", ready.user.name); - utils::Logger::log_message(ctx, "Start up and connection successful!").await; + utils::Logger::log_message(ctx.clone(), "Start up and connection successful!").await; + + if let Err(why) = commands::initialize(ctx.clone()).await + { + utils::Logger::log_error(ctx.clone(), &format!("Failed to initialize commands module: {}", why)).await; + } + else + { + utils::Logger::log_message(ctx.clone(), "Module initialization successful!").await; + } } // CODE TAKEN FROM EXAMPLE: diff --git a/src/utils.rs b/src/utils.rs index d6a7c25..6b3f7f3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,6 @@ use rand::prelude::*; -use serenity::{client::Context, model::prelude::{Role, ChannelId, GuildId, RoleId, UserId}}; +use serenity::{client::Context, model::{channel::Message, prelude::{ChannelId, GuildId, Role, RoleId, UserId}}}; pub const SM_SERVER_ID: u64 = 98929157894836224; @@ -32,12 +32,26 @@ impl Logger } } -pub async fn send_msg(ctx: Context, channel: ChannelId, msg: &str) +pub async fn send_msg(ctx: Context, channel: ChannelId, msg: &str) -> Option { - if let Err(why) = channel.say(&ctx.http, msg).await + + match channel.say(&ctx.http, msg).await { - Logger::log_error(ctx, &format!("Error sending message: {:?}\nto channel: {:?}", why, channel)).await; + Ok(m) => { return Some(m); }, + Err(why) => + { + Logger::log_error(ctx, &format!("Error sending message: {:?}\nto channel: {:?}", why, channel)).await; + return None; + } } + + // if let Err(why) = channel.say(&ctx.http, msg).await + // { + // Logger::log_error(ctx, &format!("Error sending message: {:?}\nto channel: {:?}", why, channel)).await; + // return None; + // } + + } pub async fn add_role(ctx: Context, user_id: UserId, role_id: RoleId, server_id: GuildId) -> Result @@ -87,7 +101,7 @@ pub fn get_random_item() -> String let major_items = vec!["Missiles", "Super Missiles", "E Tank", "Power Bombs" ]; let minor_items = vec!["Morphing Ball", "Bombs", "Reserve Tank", "Charge Beam", "Spazer", "Ice Beam", "Wave Beam", "Plasma Beam", "Hi-Jump Boots", "Speed Booster", "Grappling Beam", "Stupi... I mean Spring Ball", "Space Jump", "Screw Attack", - "Varia Suit", "Gravity Suit", "X-Ray Scope"]; + "Varia Suit", "Gravity Suit", "X-Ray Scope"]; let major_item_threshold = 75; let not_missile_threshold = 40;