feat: add commands & refactor
refactored how helix api is handled, + other stuff rename fill -> massping
This commit is contained in:
parent
b510e65052
commit
17a6f1cc45
7 changed files with 131 additions and 44 deletions
35
src/api.rs
35
src/api.rs
|
@ -4,22 +4,47 @@ use reqwest::{
|
||||||
};
|
};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
pub fn helix_client() -> Client {
|
pub struct HelixClient {
|
||||||
|
pub base_url: String,
|
||||||
|
pub headers: HeaderMap<HeaderValue>,
|
||||||
|
pub client: Client,
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub struct GqlClient {
|
||||||
|
// pub base_url: String,
|
||||||
|
// pub headers: HeaderMap<HeaderValue>,
|
||||||
|
// pub client: Client,
|
||||||
|
// }
|
||||||
|
|
||||||
|
impl HelixClient {
|
||||||
|
pub fn new(base_url: &str) -> HelixClient {
|
||||||
let twitch_client_id = env::var("TWITCH_CLIENT_ID").expect("a");
|
let twitch_client_id = env::var("TWITCH_CLIENT_ID").expect("a");
|
||||||
let twitch_bearer = env::var("TWITCH_BEARER").expect("a");
|
let twitch_auth_token = env::var("TWITCH_AUTH").expect("a");
|
||||||
|
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
|
|
||||||
headers.insert(
|
headers.insert(
|
||||||
"Authorization",
|
"Authorization",
|
||||||
HeaderValue::from_str(&twitch_bearer).expect("a"),
|
HeaderValue::from_str(&twitch_auth_token).expect("a"),
|
||||||
);
|
);
|
||||||
headers.insert(
|
headers.insert(
|
||||||
"Client-Id",
|
"Client-Id",
|
||||||
HeaderValue::from_str(&twitch_client_id).expect("a"),
|
HeaderValue::from_str(&twitch_client_id).expect("a"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let client = Client::builder().default_headers(headers).build().unwrap();
|
let client = Client::builder()
|
||||||
|
.default_headers(headers.clone())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
return client;
|
HelixClient {
|
||||||
|
base_url: base_url.to_string(),
|
||||||
|
headers,
|
||||||
|
client,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// impl GqlClient {
|
||||||
|
// pub fn new() {}
|
||||||
|
// }
|
||||||
|
|
|
@ -2,6 +2,7 @@ use crate::client::TwitchClient;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
use twitch_irc::message::PrivmsgMessage;
|
use twitch_irc::message::PrivmsgMessage;
|
||||||
|
|
||||||
|
@ -75,7 +76,7 @@ pub enum Size {
|
||||||
Small,
|
Small,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn lastfm_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
pub async fn lastfm_command(m: &PrivmsgMessage, c: &TwitchClient) -> Result<(), Box<dyn Error>> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
let lastfm_api_key = env::var("LASTFM_API_KEY").expect("Failed to load lastfm api key.");
|
let lastfm_api_key = env::var("LASTFM_API_KEY").expect("Failed to load lastfm api key.");
|
||||||
|
@ -91,7 +92,7 @@ pub async fn lastfm_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
||||||
match client.get(recent_tracks_url).send().await {
|
match client.get(recent_tracks_url).send().await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
if response.status().is_success() {
|
if response.status().is_success() {
|
||||||
let body = response.text().await.unwrap_or_default();
|
let body = response.text().await?;
|
||||||
match serde_json::from_str::<Data>(&body) {
|
match serde_json::from_str::<Data>(&body) {
|
||||||
Ok(payload) => {
|
Ok(payload) => {
|
||||||
if let Some(tracks) = payload.recenttracks.track.first() {
|
if let Some(tracks) = payload.recenttracks.track.first() {
|
||||||
|
@ -101,8 +102,7 @@ pub async fn lastfm_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
||||||
);
|
);
|
||||||
c.twitch_client
|
c.twitch_client
|
||||||
.say(m.channel_login.to_owned(), s.to_owned())
|
.say(m.channel_login.to_owned(), s.to_owned())
|
||||||
.await
|
.await?
|
||||||
.expect("Error sending message to twitch");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => error!("{}", e),
|
Err(e) => error!("{}", e),
|
||||||
|
@ -113,4 +113,5 @@ pub async fn lastfm_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
||||||
}
|
}
|
||||||
Err(e) => error!("Error sending request: {}", e),
|
Err(e) => error!("Error sending request: {}", e),
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
54
src/commands/massping.rs
Normal file
54
src/commands/massping.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
use dotenv::dotenv;
|
||||||
|
use std::error::Error;
|
||||||
|
use twitch_irc::message::PrivmsgMessage;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::api::HelixClient;
|
||||||
|
use crate::client::TwitchClient;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct Data {
|
||||||
|
data: Vec<UserData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct UserData {
|
||||||
|
user_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn massping_command(m: &PrivmsgMessage, c: &TwitchClient) -> Result<(), Box<dyn Error>> {
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
|
let base_url =
|
||||||
|
"https://api.twitch.tv/helix/chat/chatters?broadcaster_id=69768189&moderator_id=69768189";
|
||||||
|
|
||||||
|
let helix_client = HelixClient::new(base_url).client;
|
||||||
|
|
||||||
|
let twitch_client = c.twitch_client.clone();
|
||||||
|
|
||||||
|
match helix_client.get(base_url).send().await {
|
||||||
|
Ok(response) => {
|
||||||
|
if response.status().is_success() {
|
||||||
|
let body = response.text().await?;
|
||||||
|
match serde_json::from_str::<Data>(&body) {
|
||||||
|
Ok(payload) => {
|
||||||
|
for items in payload.data {
|
||||||
|
let s = items.user_name.to_string();
|
||||||
|
twitch_client
|
||||||
|
.say(m.channel_login.to_owned(), s)
|
||||||
|
.await
|
||||||
|
.expect("Failed to send message");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => error!("Failed: {}", e),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let error = format!("Error with response: {}", response.status());
|
||||||
|
twitch_client.say(m.channel_login.to_owned(), error).await?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => error!("Error sending request: {}", e),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod lastfm;
|
pub mod lastfm;
|
||||||
pub mod logs;
|
pub mod logs;
|
||||||
|
pub mod massping;
|
||||||
pub mod ping;
|
pub mod ping;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use crate::client::TwitchClient;
|
use crate::client::TwitchClient;
|
||||||
use sysinfo::System;
|
use sysinfo::System;
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
use twitch_irc::message::PrivmsgMessage;
|
use twitch_irc::message::PrivmsgMessage;
|
||||||
|
|
||||||
pub async fn ping_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
pub async fn ping_command(m: &PrivmsgMessage, c: &TwitchClient) -> Result<(), Box<dyn Error>> {
|
||||||
let mut sys = System::new_all();
|
let mut sys = System::new_all();
|
||||||
|
|
||||||
sys.refresh_all();
|
sys.refresh_all();
|
||||||
|
@ -24,6 +25,7 @@ pub async fn ping_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
||||||
"🚀Pong! | ↑: {}m {}s | Host: {} | Mem: {:.2} MB",
|
"🚀Pong! | ↑: {}m {}s | Host: {} | Mem: {:.2} MB",
|
||||||
uptime_minute, remaining_seconds, host, mem
|
uptime_minute, remaining_seconds, host, mem
|
||||||
);
|
);
|
||||||
let _message = c.twitch_client.say(m.channel_login.to_owned(), s).await;
|
c.twitch_client.say(m.channel_login.to_owned(), s).await?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,9 @@ use twitch_irc::message::PrivmsgMessage;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::api::helix_client;
|
use crate::api::HelixClient;
|
||||||
use crate::client::TwitchClient;
|
use crate::client::TwitchClient;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
@ -26,19 +27,21 @@ struct UserData {
|
||||||
view_count: i64,
|
view_count: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_user_command(m: &PrivmsgMessage, c: &TwitchClient, a: &[&str]) {
|
pub async fn get_user_command(
|
||||||
|
m: &PrivmsgMessage,
|
||||||
|
c: &TwitchClient,
|
||||||
|
a: &[&str],
|
||||||
|
) -> Result<(), Box<dyn Error>> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
let url = format!("https://api.twitch.tv/helix/users?login={}", a.join(" "));
|
let base_url = format!("https://api.twitch.tv/helix/users?login={}", a.join(" "));
|
||||||
|
let helix_client = HelixClient::new(&base_url).client;
|
||||||
let helix_client = helix_client();
|
|
||||||
|
|
||||||
let twitch_client = c.twitch_client.clone();
|
let twitch_client = c.twitch_client.clone();
|
||||||
|
|
||||||
match helix_client.get(url).send().await {
|
match helix_client.get(base_url).send().await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
if response.status().is_success() {
|
if response.status().is_success() {
|
||||||
let body = response.text().await.unwrap_or_default();
|
let body = response.text().await?;
|
||||||
match serde_json::from_str::<Data>(&body) {
|
match serde_json::from_str::<Data>(&body) {
|
||||||
Ok(payload) => {
|
Ok(payload) => {
|
||||||
for items in payload.data {
|
for items in payload.data {
|
||||||
|
@ -50,22 +53,19 @@ pub async fn get_user_command(m: &PrivmsgMessage, c: &TwitchClient, a: &[&str])
|
||||||
items.broadcaster_type.to_uppercase(),
|
items.broadcaster_type.to_uppercase(),
|
||||||
items.profile_image_url
|
items.profile_image_url
|
||||||
);
|
);
|
||||||
twitch_client
|
twitch_client.say(m.channel_login.to_owned(), s).await?
|
||||||
.say(m.channel_login.to_owned(), s)
|
|
||||||
.await
|
|
||||||
.expect("Failed to send message");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => error!("Failed: {}", e),
|
Err(e) => error!("Failed: {}", e),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let error = format!("Error with response: {}", response.status());
|
|
||||||
twitch_client
|
twitch_client
|
||||||
.say(m.channel_login.to_owned(), error)
|
.say(m.channel_login.to_owned(), response.status().to_string())
|
||||||
.await
|
.await?
|
||||||
.expect("Error sending message to twitch.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => error!("Error sending request: {}", e),
|
Err(e) => error!("Error sending request: {}", e),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -1,5 +1,6 @@
|
||||||
use commands::lastfm::lastfm_command;
|
use commands::lastfm::lastfm_command;
|
||||||
use commands::logs::logs_command;
|
use commands::logs::logs_command;
|
||||||
|
use commands::massping::massping_command;
|
||||||
use commands::ping::ping_command;
|
use commands::ping::ping_command;
|
||||||
use commands::user::get_user_command;
|
use commands::user::get_user_command;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -18,8 +19,8 @@ extern crate pretty_env_logger;
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn main() {
|
pub async fn main() {
|
||||||
pretty_env_logger::try_init().expect("Failed to load logger");
|
pretty_env_logger::try_init().expect("Failed to load logger");
|
||||||
let mut initial_channels = HashMap::new();
|
|
||||||
|
|
||||||
|
let mut initial_channels = HashMap::new();
|
||||||
let mut client = client();
|
let mut client = client();
|
||||||
|
|
||||||
initial_channels.insert("notnotoh", ());
|
initial_channels.insert("notnotoh", ());
|
||||||
|
@ -50,10 +51,13 @@ pub async fn main() {
|
||||||
let arguments: Vec<&str> = parts.collect();
|
let arguments: Vec<&str> = parts.collect();
|
||||||
|
|
||||||
match command {
|
match command {
|
||||||
"ping" => ping_command(&msg, &client).await,
|
"ping" => ping_command(&msg, &client).await.unwrap_or_default(),
|
||||||
"song" => lastfm_command(&msg, &client).await,
|
"song" => lastfm_command(&msg, &client).await.unwrap_or_default(),
|
||||||
"user" => get_user_command(&msg, &client, &arguments).await,
|
"user" => get_user_command(&msg, &client, &arguments)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default(),
|
||||||
"logs" => logs_command(&msg, &client, &arguments).await,
|
"logs" => logs_command(&msg, &client, &arguments).await,
|
||||||
|
"massping" => massping_command(&msg, &client).await.unwrap_or_default(),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue