forked from karx/dircord
don't respond to messages sent by us
This commit is contained in:
parent
e8d4b9adac
commit
3e3e3540f1
48
src/main.rs
48
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::<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?)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue