dircord/src/main.rs

140 lines
3.4 KiB
Rust
Raw Normal View History

2021-12-26 15:35:49 -06:00
use std::{env, sync::Arc};
2021-12-26 15:13:09 -06:00
2021-12-26 16:05:53 -06:00
use serenity::{
async_trait,
2021-12-27 11:24:31 -06:00
futures::StreamExt,
2021-12-26 16:05:53 -06:00
http::Http,
model::{
channel::Message,
id::{ChannelId, UserId},
prelude::Ready,
},
prelude::*,
2021-12-27 11:24:31 -06:00
Client as DiscordClient,
};
use irc::{
client::{data::Config, Client as IrcClient, Sender},
proto::Command,
2021-12-26 16:05:53 -06:00
};
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,
2021-12-26 16:05:53 -06:00
None => msg.author.name,
2021-12-26 15:13:09 -06:00
}
} else {
msg.author.name
}
};
2021-12-26 16:05:53 -06:00
let (http, channel_id, user_id) = {
2021-12-26 15:35:49 -06:00
let data = ctx.data.read().await;
let http = data.get::<HttpKey>().unwrap().to_owned();
let channel_id = data.get::<ChannelIdKey>().unwrap().to_owned();
2021-12-26 16:05:53 -06:00
let user_id = data.get::<UserIdKey>().unwrap().to_owned();
2021-12-26 15:35:49 -06:00
2021-12-26 16:05:53 -06:00
(http, channel_id, user_id)
2021-12-26 15:35:49 -06:00
};
2021-12-27 11:24:31 -06:00
// if user_id != msg.author.id && !msg.author.bot {
// send_message(&http, &channel_id, &format!("{}: {}", nick, msg.content))
// .await
// .unwrap();
// }
2021-12-26 16:05:53 -06:00
}
async fn ready(&self, ctx: Context, info: Ready) {
let id = info.user.id;
let mut data = ctx.data.write().await;
data.insert::<UserIdKey>(id);
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;
2021-12-26 16:05:53 -06:00
struct UserIdKey;
2021-12-27 11:24:31 -06:00
struct SenderKey;
2021-12-26 15:35:49 -06:00
impl TypeMapKey for HttpKey {
type Value = Arc<Http>;
}
impl TypeMapKey for ChannelIdKey {
type Value = ChannelId;
}
2021-12-26 16:05:53 -06:00
impl TypeMapKey for UserIdKey {
type Value = UserId;
}
2021-12-27 11:24:31 -06:00
impl TypeMapKey for SenderKey {
type Value = Sender;
}
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");
2021-12-27 11:24:31 -06:00
let mut discord_client = DiscordClient::builder(&token)
.event_handler(Handler)
.await?;
2021-12-26 15:13:09 -06:00
let channel_id = ChannelId(831255708875751477);
2021-12-27 11:24:31 -06:00
let config = Config {
nickname: Some("dircord".to_string()),
server: Some("192.168.1.28".to_owned()),
port: Some(6667),
channels: vec!["#no-normies".to_string()],
use_tls: Some(false),
umodes: Some("+B".to_string()),
..Config::default()
};
let mut irc_client = IrcClient::from_config(config).await?;
let http = discord_client.cache_and_http.http.clone();
2021-12-26 15:35:49 -06:00
{
2021-12-27 11:24:31 -06:00
let mut data = discord_client.data.write().await;
data.insert::<HttpKey>(http.clone());
2021-12-26 15:35:49 -06:00
data.insert::<ChannelIdKey>(channel_id);
2021-12-27 11:24:31 -06:00
data.insert::<SenderKey>(irc_client.sender());
2021-12-26 15:35:49 -06:00
}
2021-12-27 11:24:31 -06:00
tokio::spawn(async move {
irc_loop(irc_client, http, channel_id).await.unwrap();
});
discord_client.start().await?;
2021-12-26 15:13:09 -06:00
Ok(())
}
2021-12-27 11:24:31 -06:00
async fn irc_loop(
mut client: IrcClient,
http: Arc<Http>,
channel_id: ChannelId,
) -> anyhow::Result<()> {
client.identify()?;
let mut stream = client.stream()?;
while let Some(orig_message) = stream.next().await.transpose()? {
print!("{}", orig_message);
if let Command::PRIVMSG(ref channel, ref message) = orig_message.command {
let nickname = orig_message.source_nickname().unwrap();
channel_id
.say(&http, format!("{}: {}", nickname, message))
.await?;
}
}
Ok(())
2021-12-26 16:05:53 -06:00
}