almost there with the redis client...

Thanks random Chinese blog
https://ask.csdn.net/questions/1964104
This commit is contained in:
Brandon Thomas committed 2021-02-25 05:16:24 -05:00
1 parent 173446ccce
commit 856d2145cf
5 files changed
+768 -10

No files matched your search

+11
View File
@@ -11,6 +11,12 @@ pub struct Secrets {
pub application_client_secret: String,
/// OAuth access token generated with URL handshake
pub oauth_access_token: String,
/// Redis
pub redis_username: String,
pub redis_password: String,
pub redis_host: String,
pub redis_port: u32,
}
impl Secrets {
@@ -21,4 +27,9 @@ impl Secrets {
let secrets = toml::from_str(&buffer)?;
Ok(secrets)
}
pub fn redis_url(&self) -> String {
format!("rediss://{}:{}@{}:{}",
self.redis_username, self.redis_password, self.redis_host, self.redis_port)
}
}
+17 -4
View File
@@ -2,6 +2,8 @@
use anyhow::Context as _;
use twitchchat::{messages, AsyncRunner, Status, UserConfig};
use crate::filesystem::secrets::Secrets;
use redis::aio::Connection;
use redis::AsyncCommands;
// some helpers for the demo
// fn get_env_var(key: &str) -> anyhow::Result<String> {
@@ -35,13 +37,20 @@ pub fn channels_to_join() -> anyhow::Result<Vec<String>> {
}
// a 'main loop'
pub async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
pub async fn main_loop(mut runner: AsyncRunner, secrets: &Secrets) -> anyhow::Result<()> {
println!("Connect to redis");
let client = redis::Client::open(secrets.redis_url())?;
let mut connection = client.get_async_connection().await?;
//let mut con = client.get_connection()?;
//let mut pubsub = con.as_pubsub();
loop {
match runner.next_message().await? {
// this is the parsed message -- across all channels (and notifications from Twitch)
Status::Message(msg) => {
println!("Handle message time!");
handle_message(msg).await;
handle_message(msg , &mut connection ).await;
}
// you signaled a quit
@@ -61,12 +70,16 @@ pub async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
}
// you can generally ignore the lifetime for these types.
async fn handle_message(msg: messages::Commands<'_>) {
async fn handle_message(msg: messages::Commands<'_> , connection: &mut Connection ) {
use messages::Commands::*;
// All sorts of messages
match msg {
// This is the one users send to channels
Privmsg(msg) => println!("[{}] {}: {}", msg.channel(), msg.name(), msg.data()),
Privmsg(msg) => {
println!("[{}] {}: {}", msg.channel(), msg.name(), msg.data());
//connection.publish("vocode", msg.data()).await;
},
// This one is special, if twitch adds any new message
// types, this will catch it until future releases of
+13 -4
View File
@@ -1,3 +1,6 @@
#[macro_use]
extern crate serde_derive;
// NOTE: this demo requires `--features="tokio/full tokio-util"`.
use twitchchat::{
commands, connector, messages,
@@ -9,8 +12,12 @@ use twitchchat::{
mod include;
use crate::include::{channels_to_join, get_user_config, main_loop};
pub type AnyhowResult<T> = anyhow::Result<T>;
mod filesystem;
use crate::filesystem::secrets::Secrets;
async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Result<AsyncRunner> {
// create a connector using ``tokio``, this connects to Twitch.
// you can provide a different address with `custom`
@@ -39,8 +46,10 @@ async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Resul
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let secrets = Secrets::from_file("secrets.toml")?;
// create a user configuration
let user_config = get_user_config()?;
let user_config = get_user_config(&secrets)?;
// get some channels to join from the environment
let channels = channels_to_join()?;
@@ -70,8 +79,8 @@ async fn main() -> anyhow::Result<()> {
}
println!("in 100 seconds we'll exit");
tokio::time::delay_for(std::time::Duration::from_secs(100)).await;
println!("in 1000 seconds we'll exit");
tokio::time::delay_for(std::time::Duration::from_secs(1000)).await;
// send one final message to all channels
for channel in &channels {
@@ -101,5 +110,5 @@ async fn main() -> anyhow::Result<()> {
println!("starting main loop");
// your 'main loop'. you'll just call next_message() until you're done
main_loop(runner).await
main_loop(runner, &secrets).await
}