diff --git a/src/main.rs b/src/main.rs index 6ae8fba..9215989 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,15 @@ use std::{env, sync::Arc}; -use serenity::{prelude::*, async_trait, model::{channel::Message, id::ChannelId}, http::Http}; +use serenity::{ + async_trait, + http::Http, + model::{ + channel::Message, + id::{ChannelId, UserId}, + prelude::Ready, + }, + prelude::*, +}; struct Handler; @@ -11,28 +20,41 @@ impl EventHandler for Handler { if let Some(member) = msg.member { match member.nick { Some(n) => n, - None => msg.author.name + None => msg.author.name, } } else { msg.author.name } }; - let (http, channel_id) = { + let (http, channel_id, user_id) = { let data = ctx.data.read().await; let http = data.get::().unwrap().to_owned(); let channel_id = data.get::().unwrap().to_owned(); + let user_id = data.get::().unwrap().to_owned(); - (http, channel_id) + (http, channel_id, user_id) }; - send_message(&http, &channel_id, &format!("{}: {}", nick, msg.content)).await.unwrap(); + if user_id != msg.author.id { + send_message(&http, &channel_id, &format!("{}: {}", nick, msg.content)) + .await + .unwrap(); + } + } + + async fn ready(&self, ctx: Context, info: Ready) { + let id = info.user.id; + + let mut data = ctx.data.write().await; + data.insert::(id); } } struct HttpKey; struct ChannelIdKey; +struct UserIdKey; impl TypeMapKey for HttpKey { type Value = Arc; @@ -42,13 +64,15 @@ impl TypeMapKey for ChannelIdKey { type Value = ChannelId; } +impl TypeMapKey for UserIdKey { + type Value = UserId; +} + #[tokio::main] async fn main() -> anyhow::Result<()> { let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN not set"); - let mut client = Client::builder(&token) - .event_handler(Handler) - .await?; + let mut client = Client::builder(&token).event_handler(Handler).await?; let channel_id = ChannelId(831255708875751477); @@ -63,6 +87,10 @@ async fn main() -> anyhow::Result<()> { Ok(()) } -async fn send_message(http: &Http, channel_id: &ChannelId, content: &str) -> anyhow::Result { +async fn send_message( + http: &Http, + channel_id: &ChannelId, + content: &str, +) -> anyhow::Result { Ok(channel_id.say(http, content).await?) -} \ No newline at end of file +}