use updated irc api

master
lalanza808 4 years ago
parent 85ae652690
commit ceeb329fd1

@ -1,47 +1,36 @@
extern crate irc; extern crate irc;
extern crate tokio_postgres; extern crate tokio_postgres;
use irc::client::prelude::{Client, ClientExt, Command, IrcClient}; use irc::client::prelude::{ClientExt, Command, Config, IrcReactor};
use tokio_postgres::{NoTls, Error}; use tokio_postgres::{NoTls, Error, Client};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Error> { async fn main() -> Result<(), Error> {
println!("[DEBUG] Connecting to IRC server..."); println!("[DEBUG] Connecting to IRC server...");
let client = IrcClient::new("config.toml").unwrap(); let irc_config = Config::load("config.toml").unwrap();
// Connect to the database.
let (pg_client, connection) = tokio_postgres::connect("host=localhost user=postgres password=postgres", NoTls) let (pg_client, connection) = tokio_postgres::connect("host=localhost user=postgres password=postgres", NoTls)
.await?; .await?;
let mut reactor = IrcReactor::new().unwrap();
let irc_client = reactor.prepare_client_and_connect(&irc_config).unwrap();
irc_client.identify().unwrap();
// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = connection.await { if let Err(e) = connection.await {
eprintln!("connection error: {}", e); eprintln!("connection error: {}", e);
} }
}); });
// Now we can execute a simple statement that just returns its parameter. reactor.register_client_with_handler(irc_client, |c, m| {
let rows = pg_client if let Command::NOTICE(channel, message) = &m.command {
.query("SELECT $1::TEXT", &[&"hello world"]) println!("[{:?}][{}]: {}", &m.response_target(), &channel, &message);
.await?;
// And then check that we got back the same string we sent over.
let value: &str = rows[0].get(0);
assert_eq!(value, "hello world");
client.identify().unwrap();
client.for_each_incoming(|im| {
if let Command::NOTICE(channel, message) = &im.command {
println!("[{:?}][{}]: {}", &im.response_target(), &channel, &message)
} }
if let Command::PRIVMSG(channel, message) = &im.command { if let Command::PRIVMSG(channel, message) = &m.command {
let src_nick = &im.source_nickname().unwrap(); let src_nick = &m.source_nickname().unwrap();
let res_target = &m.response_target().unwrap();
let help_msg = vec![ let help_msg = vec![
format!("Hey there {}, here's what you can do.", &im.source_nickname().unwrap()), format!("Hey there {}, here's what you can do.", &m.source_nickname().unwrap()),
"Say '!create' followed by the title of your listing to create a new one.".to_string(), "Say '!create' followed by the title of your listing to create a new one.".to_string(),
"Example: '!create I am offering virtual guitar lessons/sessions'.".to_string(), "Example: '!create I am offering virtual guitar lessons/sessions'.".to_string(),
"Say '!delete' followed by the name or ID of an existing listing.".to_string(), "Say '!delete' followed by the name or ID of an existing listing.".to_string(),
@ -49,33 +38,34 @@ async fn main() -> Result<(), Error> {
]; ];
let help_msg = help_msg.join(" "); let help_msg = help_msg.join(" ");
// Print all messages // Print user messages to channel
println!( println!("[{}][{}]: {}", res_target, src_nick, message);
"[{}][{}]: {}",
&im.response_target().unwrap(),
src_nick,
message
);
if message.starts_with("!create") { if message.starts_with("!create") {
let user_msg = message.split(" "); let user_msg = message.split(" ");
let mut user_msg: Vec<&str> = user_msg.collect(); let mut user_msg: Vec<&str> = user_msg.collect();
user_msg.remove(0); user_msg.remove(0);
let user_msg = user_msg.join(" "); let user_msg = user_msg.join(" ");
println!("it looks like you said: '{:?}' - Trying to save to the DB", user_msg); println!("it looks like you said: {:?} - Trying to save to the DB", user_msg);
// pg_client.query( // save to db
// "INSERT INTO posts (nick, post_title) VALUES ('$1', '$2')",
// &[src_nick, &user_msg]
// );
// let value: &str = rows2[0].get(0);
// println!("{}", value);
} else if message.starts_with("!delete") { } else if message.starts_with("!delete") {
let _ = client.send_privmsg(&channel, "This feature is still being worked on."); let _ = c.send_privmsg(&channel, "This feature is still being worked on.");
} else if message.starts_with("!help") { } else if message.starts_with("!help") {
let _ = client.send_privmsg(&channel, &help_msg); let _ = c.send_privmsg(&channel, &help_msg);
} }
} }
}).unwrap(); Ok(())
});
reactor.run().unwrap();
Ok(()) Ok(())
} }
// async fn insert_post(client: &Client, nick: &str, user_msg: &str) -> String {
// let rows2 = pg_client.query(
// "INSERT INTO posts (nick, post_title) VALUES ('$1', '$2')",
// &[&src_nick, &user_msg]
// ).await?;
// let value: &str = rows2[0].get(0);
// println!("{}", value);
// }

Loading…
Cancel
Save