don't respond to messages sent by us

This commit is contained in:
Yash Karandikar 2021-12-26 16:05:53 -06:00
parent e8d4b9adac
commit 3e3e3540f1
Signed by untrusted user: karx
GPG key ID: A794DA2529474BA5

View file

@ -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::<HttpKey>().unwrap().to_owned();
let channel_id = data.get::<ChannelIdKey>().unwrap().to_owned();
let user_id = data.get::<UserIdKey>().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::<UserIdKey>(id);
}
}
struct HttpKey;
struct ChannelIdKey;
struct UserIdKey;
impl TypeMapKey for HttpKey {
type Value = Arc<Http>;
@ -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<Message> {
async fn send_message(
http: &Http,
channel_id: &ChannelId,
content: &str,
) -> anyhow::Result<Message> {
Ok(channel_id.say(http, content).await?)
}
}