Skip to content

Commit

Permalink
Merge pull request #34 from ChristianPayne/main
Browse files Browse the repository at this point in the history
v1.4.1 - auth fixes for windows and new migration script
  • Loading branch information
ChristianPayne authored Jan 23, 2025
2 parents 78760ca + 142167b commit 76ab28d
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 169 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ennesults-rs",
"private": true,
"version": "1.4.0",
"version": "1.4.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ennesults-rs"
version = "1.4.0"
version = "1.4.1"
description = "A Twitch bot created for Ennegineer to kindly insult her chat."
authors = ["Christian Payne"]
license = ""
Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/bot/announcements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ pub async fn announcement_thread_loop(app_handle: AppHandle, rx: Receiver<()>) {
loop {
let state = app_handle.state::<Bot>();
let (enable_announcements, randomize_announcements, time_between_announcements) = {
let bot_info = state
.bot_info
let settings = state
.settings
.lock()
.expect("Failed to get lock for bot_info");
.expect("Failed to get lock for settings");

(
bot_info.enable_announcements,
bot_info.randomize_announcements,
bot_info.time_between_announcements,
settings.enable_announcements,
settings.randomize_announcements,
settings.time_between_announcements,
)
};

Expand Down Expand Up @@ -190,7 +190,7 @@ pub mod api {
.bot_data
.announcements
.lock()
.expect("Failed to get lock for bot info") = announcements.clone();
.expect("Failed to get lock for settings") = announcements.clone();

let write_result =
write_file::<Announcements>(&app_handle, "announcements.json", announcements.clone());
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/bot/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ pub mod api {
use super::{validate_auth, Authentication, AuthenticationBuilder, CLIENT_ID};

/// Opens a new window from Ennesults to log in the user.
/// Note the async function. Tauri has a bug where Windows will deadlock when creating windows in a sync function.
#[tauri::command]
pub fn open_auth_window(app_handle: AppHandle) -> Result<(), String> {
pub async fn open_auth_window(app_handle: AppHandle) -> Result<(), String> {
if !app_handle.manage(AuthenticationBuilder::new()) {
return Err("Authentication Builder state is already being managed.".to_string());
}
Expand Down
12 changes: 6 additions & 6 deletions src-tauri/src/bot/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::{Deref, DerefMut};

use crate::bot::{announcement_thread_loop, insult_thread_loop, AnnouncementThread, InsultThread};

use super::{api::get_bot_info, handle_incoming_chat, BotData, BotInfo, Client};
use super::{api::get_settings, handle_incoming_chat, BotData, Client, Settings};
use super::{validate_auth, Authentication, AuthenticationDetails};

#[derive(serde::Serialize, Clone, Debug, TS)]
Expand Down Expand Up @@ -38,17 +38,17 @@ pub enum Alert {
// BOT
#[derive(Debug)]
pub struct Bot {
pub bot_info: Mutex<BotInfo>,
pub settings: Mutex<Settings>,
pub auth: Mutex<Authentication>,
pub bot_data: BotData,
pub client: Mutex<Client>,
pub chat_messages: Mutex<Vec<TwitchMessage>>,
}

impl Bot {
pub fn new(bot_info: BotInfo, bot_data: BotData, auth: Authentication) -> Self {
pub fn new(settings: Settings, bot_data: BotData, auth: Authentication) -> Self {
Self {
bot_info: Mutex::new(bot_info),
settings: Mutex::new(settings),
auth: Mutex::new(auth),
bot_data,
client: Mutex::new(Client::default()),
Expand All @@ -66,7 +66,7 @@ impl Bot {
}

pub fn get_channel_name(&self) -> String {
self.bot_info.lock().unwrap().channel_name.clone()
self.settings.lock().unwrap().channel_name.clone()
}

pub async fn get_channel_status(&self) -> Option<(bool, bool)> {
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Bot {
impl Default for Bot {
fn default() -> Self {
Self {
bot_info: Mutex::new(BotInfo::default()),
settings: Mutex::new(Settings::default()),
auth: Mutex::new(Authentication::default()),
bot_data: BotData::default(),
client: Mutex::new(Client::default()),
Expand Down
40 changes: 20 additions & 20 deletions src-tauri/src/bot/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
handle_whisper, process_comebacks, process_corrections, process_user_state, AnnouncementThread,
Bot, BotData, InsultThread, SerializeRBGColor, TwitchMessage,
};
use crate::bot::api::get_bot_info;
use crate::bot::api::get_settings;
use crate::bot::Authentication;
use crate::commands::{meets_minimum_user_level, parse_for_command, parse_msg_for_user_level};
use crate::twitch::get_broadcaster_id;
Expand Down Expand Up @@ -61,15 +61,15 @@ impl Client {
#[tauri::command]
pub async fn say(state: tauri::State<'_, Bot>, message: &str) -> Result<(), String> {
let channel_name = {
let bot_info = state
.bot_info
let settings = state
.settings
.lock()
.expect("Failed to get lock for bot info.");
.expect("Failed to get lock for settings.");

if bot_info.channel_name.is_empty() {
if settings.channel_name.is_empty() {
return Err("Channel name not found.".into());
}
bot_info.channel_name.clone()
settings.channel_name.clone()
};

let Some(client) = ({
Expand Down Expand Up @@ -282,15 +282,15 @@ pub mod api {
let twitch_client_thread_handle =
tokio::spawn(handle_incoming_chat(app_handle.clone(), incoming_messages));

let bot_info = state
.bot_info
let settings = state
.settings
.lock()
.expect("Failed to get lock for bot info");
.expect("Failed to get lock for settings");

let insult_thread = InsultThread::new(app_handle.clone(), bot_info.enable_insults);
let insult_thread = InsultThread::new(app_handle.clone(), settings.enable_insults);

let announcement_thread =
AnnouncementThread::new(app_handle.clone(), bot_info.enable_announcements);
AnnouncementThread::new(app_handle.clone(), settings.enable_announcements);

let mut client = state.client.lock().expect("Failed to get lock for client");
*client = Client::new(
Expand All @@ -309,10 +309,10 @@ pub mod api {
pub fn disconnect_from_twitch(app_handle: AppHandle) -> Result<(), String> {
let state = app_handle.state::<Bot>();
let mut client = state.client.lock().expect("Failed to get lock for client");
let bot_info = state
.bot_info
let settings = state
.settings
.lock()
.expect("Failed to get lock for bot info");
.expect("Failed to get lock for settings");

match &mut *client {
Client::Disconnected => Err("Client already disconnected".to_string()),
Expand Down Expand Up @@ -343,8 +343,8 @@ pub mod api {
}

// Tell the client to leave the twitch channel.
twitch_client.part(bot_info.channel_name.clone());
let _ = app_handle.emit("channel_part", bot_info.channel_name.clone());
twitch_client.part(settings.channel_name.clone());
let _ = app_handle.emit("channel_part", settings.channel_name.clone());

// Update the state to reflect the client being disconnected.
*client = Client::Disconnected;
Expand All @@ -359,9 +359,9 @@ pub mod api {
let state = app_handle.state::<Bot>();
let channel_name = {
state
.bot_info
.settings
.lock()
.expect("Failed to get lock for bot info")
.expect("Failed to get lock for settings")
.channel_name
.clone()
};
Expand Down Expand Up @@ -432,7 +432,7 @@ pub mod api {
#[tauri::command]
pub async fn get_channel_status(state: tauri::State<'_, Bot>) -> Result<(bool, bool), String> {
let channel_name = state
.bot_info
.settings
.lock()
.expect("Failed to get lock")
.channel_name
Expand All @@ -456,7 +456,7 @@ pub mod api {
state: tauri::State<'_, Bot>,
) -> Result<String, String> {
let channel_name = state
.bot_info
.settings
.lock()
.expect("Failed to get lock")
.channel_name
Expand Down
16 changes: 8 additions & 8 deletions src-tauri/src/bot/comebacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tauri::{AppHandle, Manager};
use ts_rs::TS;
use twitch_irc::message::PrivmsgMessage;

use crate::bot::{say, Bot, BotInfo};
use crate::bot::{say, Bot, Settings};

use super::BotData;

Expand All @@ -23,13 +23,13 @@ pub async fn process_comebacks(app_handle: AppHandle, msg: &PrivmsgMessage) -> b
let state = app_handle.state::<Bot>();

let (bot_name, percent_chance_of_comeback, comeback_options, channel_name) = {
let bot_info = state
.bot_info
let settings = state
.settings
.lock()
.expect("Failed to get lock for bot info.");
.expect("Failed to get lock for settings.");

// Check to make sure comebacks are enabled in the settings.
if !bot_info.enable_comebacks {
if !settings.enable_comebacks {
return false;
}

Expand All @@ -47,9 +47,9 @@ pub async fn process_comebacks(app_handle: AppHandle, msg: &PrivmsgMessage) -> b
// Get bot name
(
bot_name,
bot_info.percent_chance_of_comeback,
settings.percent_chance_of_comeback,
comeback_options.0.clone(),
bot_info.channel_name.clone(),
settings.channel_name.clone(),
)
} else {
return false;
Expand Down Expand Up @@ -149,7 +149,7 @@ pub mod api {
.bot_data
.comebacks
.lock()
.expect("Failed to get lock for bot info") = comebacks.clone();
.expect("Failed to get lock for settings") = comebacks.clone();

let write_result =
write_file::<Comebacks>(&app_handle, "comebacks.json", comebacks.clone());
Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/bot/corrections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rand::Rng;
use tauri::{AppHandle, Manager};
use twitch_irc::message::PrivmsgMessage;

use super::{say, Bot, BotInfo};
use super::{say, Bot, Settings};

pub async fn process_corrections(app_handle: AppHandle, msg: &PrivmsgMessage) -> bool {
if !msg.message_text.to_lowercase().contains("en") {
Expand All @@ -13,19 +13,19 @@ pub async fn process_corrections(app_handle: AppHandle, msg: &PrivmsgMessage) ->

// Get values from state and lock the value back up.
let (correction_exceptions, percent_chance_of_correction) = {
let bot_info = state
.bot_info
let settings = state
.settings
.lock()
.expect("Failed to get lock for bot info.");
.expect("Failed to get lock for settings.");

// Check to make sure comebacks are enabled in the settings.
if !bot_info.enable_corrections {
if !settings.enable_corrections {
return false;
}

(
bot_info.correction_exceptions.clone(),
bot_info.percent_chance_of_correction,
settings.correction_exceptions.clone(),
settings.percent_chance_of_correction,
)
};

Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/bot/insults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ pub async fn insult_thread_loop(app_handle: AppHandle, rx: Receiver<()>) {
let state = app_handle.state::<Bot>();

let (enable_insults, time_between_insults) = {
let bot_info = state
.bot_info
let settings = state
.settings
.lock()
.expect("Failed to get lock for bot_info");
.expect("Failed to get lock for settings");

(bot_info.enable_insults, bot_info.time_between_insults)
(settings.enable_insults, settings.time_between_insults)
};

let sleep_time = Duration::from_secs(time_between_insults as u64);
Expand Down Expand Up @@ -203,8 +203,8 @@ pub fn format_insult(
if formatted_message.contains("{{streamer}}") {
let channel_name = {
let state = app_handle.state::<Bot>();
let bot_info = state.bot_info.lock().expect("Failed to get bot_info");
bot_info.channel_name.clone()
let settings = state.settings.lock().expect("Failed to get settings");
settings.channel_name.clone()
};

formatted_message = formatted_message.replace("{{streamer}}", channel_name.as_str())
Expand Down Expand Up @@ -348,7 +348,7 @@ pub mod api {
.bot_data
.insults
.lock()
.expect("Failed to get lock for bot info") = insults.clone();
.expect("Failed to get lock for settings") = insults.clone();

let write_result = write_file::<Insults>(&app_handle, "insults.json", insults.clone());

Expand Down
8 changes: 4 additions & 4 deletions src-tauri/src/bot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ mod announcements;
mod auth;
mod bot;
mod bot_data;
mod bot_info;
mod client;
mod comebacks;
mod corrections;
mod insults;
mod settings;
mod users;
mod whispers;

pub use announcements::*;
pub use auth::*;
pub use bot::*;
pub use bot_data::*;
pub use bot_info::*;
pub use client::*;
pub use comebacks::*;
pub use corrections::*;
pub use insults::*;
pub use settings::*;
pub use users::*;
pub use whispers::*;

pub mod api {
use super::announcements;
use super::auth;
use super::bot;
use super::bot_info;
use super::client;
use super::comebacks;
use super::insults;
use super::settings;
use super::users;
use super::whispers;

pub use announcements::api::*;
pub use auth::api::*;
pub use bot::api::*;
pub use bot_info::api::*;
pub use client::api::*;
pub use comebacks::api::*;
pub use insults::api::*;
pub use settings::api::*;
pub use users::api::*;
pub use whispers::api::*;
}
Loading

0 comments on commit 76ab28d

Please sign in to comment.