2021-12-26 15:35:49 -06:00
|
|
|
use std::{env, sync::Arc};
|
2021-12-26 15:13:09 -06:00
|
|
|
|
2021-12-26 15:35:49 -06:00
|
|
|
use serenity::{prelude::*, async_trait, model::{channel::Message, id::ChannelId}, http::Http};
|
2021-12-26 15:13:09 -06:00
|
|
|
|
|
|
|
struct Handler;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
|
|
|
async fn message(&self, ctx: Context, msg: Message) {
|
|
|
|
let nick = {
|
|
|
|
if let Some(member) = msg.member {
|
|
|
|
match member.nick {
|
|
|
|
Some(n) => n,
|
|
|
|
None => msg.author.name
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg.author.name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-26 15:35:49 -06:00
|
|
|
let (http, channel_id) = {
|
|
|
|
let data = ctx.data.read().await;
|
|
|
|
|
|
|
|
let http = data.get::<HttpKey>().unwrap().to_owned();
|
|
|
|
let channel_id = data.get::<ChannelIdKey>().unwrap().to_owned();
|
|
|
|
|
|
|
|
(http, channel_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
send_message(&http, &channel_id, &format!("{}: {}", nick, msg.content)).await.unwrap();
|
2021-12-26 15:13:09 -06:00
|
|
|
}
|
2021-12-26 14:04:47 -06:00
|
|
|
}
|
2021-12-26 15:13:09 -06:00
|
|
|
|
2021-12-26 15:35:49 -06:00
|
|
|
struct HttpKey;
|
|
|
|
struct ChannelIdKey;
|
|
|
|
|
|
|
|
impl TypeMapKey for HttpKey {
|
|
|
|
type Value = Arc<Http>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeMapKey for ChannelIdKey {
|
|
|
|
type Value = ChannelId;
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:13:09 -06:00
|
|
|
#[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 channel_id = ChannelId(831255708875751477);
|
|
|
|
|
2021-12-26 15:35:49 -06:00
|
|
|
{
|
|
|
|
let mut data = client.data.write().await;
|
|
|
|
data.insert::<HttpKey>(client.cache_and_http.http.clone());
|
|
|
|
data.insert::<ChannelIdKey>(channel_id);
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:13:09 -06:00
|
|
|
client.start().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:35:49 -06:00
|
|
|
async fn send_message(http: &Http, channel_id: &ChannelId, content: &str) -> anyhow::Result<Message> {
|
2021-12-26 15:13:09 -06:00
|
|
|
Ok(channel_id.say(http, content).await?)
|
|
|
|
}
|