From 8fcdcef2cb6910f26d159ae9ad3858d0c9d1b890 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Tue, 18 Jul 2023 16:34:01 -0400 Subject: [PATCH] In the process of adding SM Server commands --- src/commands/admin.rs | 10 +++++++++- src/commands/mod.rs | 15 ++++++++++++++- src/commands/sm_server.rs | 36 ++++++++++++++++++++++++++++++++++++ src/commands/test.rs | 10 ---------- src/main.rs | 24 ++++++++++++++++++++---- src/utils.rs | 1 + 6 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 src/commands/sm_server.rs diff --git a/src/commands/admin.rs b/src/commands/admin.rs index 9802521..9bd8a40 100644 --- a/src/commands/admin.rs +++ b/src/commands/admin.rs @@ -1,4 +1,12 @@ use serenity::prelude::*; use serenity::model::prelude::*; -use serenity::model::channel::Message; \ No newline at end of file +use serenity::model::channel::Message; + + +use crate::utils; + +pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) +{ + +} \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 59201cb..93aa264 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -5,17 +5,30 @@ use serenity::model::channel::Message; // use serenity::model::prelude::*; pub mod test; +pub mod admin; +pub mod sm_server; use crate::utils; pub async fn parse(ctx: Context, msg: Message) { if let Some(server_id) = msg.guild_id - { + { + // This is not a command + if msg.content.len() == 0 || msg.content.chars().nth(0) != Some('.') + { + return; + } + if server_id == utils::ADMIN_SERVER_ID && msg.channel_id == utils::ADMIN_COMMAND_CHANNEL_ID { test::parse_command(ctx, msg, server_id).await; } + + if server_id == utils::SM_SERVER_ID + { + + } } else { diff --git a/src/commands/sm_server.rs b/src/commands/sm_server.rs new file mode 100644 index 0000000..7c135c3 --- /dev/null +++ b/src/commands/sm_server.rs @@ -0,0 +1,36 @@ + +use serenity::prelude::*; +use serenity::model::prelude::*; +use serenity::model::channel::Message; + + +use crate::utils; + +pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) +{ + if msg.content == ".randomizer" + { + let item = utils::get_random_item(); + if let Err(why) = msg.channel_id.say(&ctx.http, format!("You found {}!", item)).await + { + println!("Error sending message: {:?}", why); + } + } + + let command_lower = msg.content.to_lowercase(); + let mut command_iter = command_lower.split_whitespace(); + let command_start = command_iter.next(); + + if command_start == Some(".color") + { + if let Some(arg) = command_iter.next() + { + match arg + { + // TODO: add color roles + _ => utils::Logger::log_error(ctx, &format!("unknown .color argument: {}", arg)).await + }; + } + } + +} \ No newline at end of file diff --git a/src/commands/test.rs b/src/commands/test.rs index efa02a8..35ba5e7 100644 --- a/src/commands/test.rs +++ b/src/commands/test.rs @@ -46,14 +46,4 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) } return; } - - - if msg.content == ".randomizer" - { - let item = utils::get_random_item(); - if let Err(why) = msg.channel_id.say(&ctx.http, format!("You found {}!", item)).await - { - println!("Error sending message: {:?}", why); - } - } } diff --git a/src/main.rs b/src/main.rs index f12fef4..0e3efd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,24 @@ mod commands; mod data_loader; mod utils; -use std::collections::HashSet; +// use std::collections::HashSet; +use std::sync::Arc; +use std::sync::atomic::AtomicU64; use serenity::async_trait; use serenity::prelude::*; -use serenity::model::prelude::*; +// use serenity::model::prelude::*; use serenity::model::channel::Message; use serenity::model::gateway::Ready; use serenity::http::Http; +struct Globals; + +impl TypeMapKey for Globals +{ + type Value = Arc; +} + struct Handler; #[async_trait] @@ -59,6 +68,7 @@ async fn main() + println!("Connecting..."); let intents = GatewayIntents::GUILD_MESSAGES @@ -70,9 +80,15 @@ async fn main() // by Discord for bot users. let mut client = Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client"); - // TODO: See link for how to add global data to the client: + + // Set the global data // https://github.com/serenity-rs/serenity/blob/current/examples/e12_global_data/src/main.rs - // client.data.write().await.insert::(owner); + { + // Open the data lock in write mode, so keys can be inserted to it. + let mut data = client.data.write().await; + + data.insert::(Arc::new(AtomicU64::new(owner.into()))); + } // Finally, start a single shard, and start listening to events. // diff --git a/src/utils.rs b/src/utils.rs index 1b2b6b4..0830934 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,6 +3,7 @@ use rand::prelude::*; use serenity::{client::Context, model::prelude::ChannelId}; +pub const SM_SERVER_ID: u64 = 98929157894836224; pub const ADMIN_SERVER_ID: u64 = 483735475283165195; pub const ADMIN_COMMAND_CHANNEL_ID: u64 = 1130607621699809443; pub const MAIN_LOG_CHANNEL_ID: u64 = 1130607583007363074;