feat: initial commit

This commit is contained in:
cătălin 2024-10-22 22:57:43 +02:00
commit beab2ff3a5
No known key found for this signature in database
24 changed files with 3506 additions and 0 deletions

View file

@ -0,0 +1,140 @@
import json
import logging
from pathlib import Path
from kivy.clock import Clock
from kivy.metrics import dp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
class ConfigWindow(BoxLayout):
def __init__(self, config_path: Path, **kwargs):
super().__init__(**kwargs)
self.config_path = config_path
self.orientation = "vertical"
self.spacing = dp(10)
self.padding = dp(20)
# Load existing configuration
self.default_config = {
"Host": "irc.chat.twitch.tv",
"Port": 6667,
"Channel": "#<channel>",
"Nickname": "<name>",
"Authentication": "oauth:<auth>",
"DeniedUsers": ["StreamElements", "Nightbot", "Moobot", "Marbiebot"],
"AllowedUsers": [],
"Cooldown": 20,
"KeyLength": 2,
"MaxSentenceWordAmount": 25,
"MinSentenceWordAmount": -1,
"HelpMessageTimer": 60 * 60 * 5, # 18000 seconds, 5 hours
"AutomaticGenerationTimer": -1,
"WhisperCooldown": True,
"EnableGenerateCommand": True,
"SentenceSeparator": " - ",
"AllowGenerateParams": True,
"GenerateCommands": ["!generate", "!g"],
}
try:
if config_path.exists():
with open(config_path) as f:
saved_config = json.load(f)
# Update self.default_config with saved values
self.default_config.update(saved_config)
except json.JSONDecodeError:
logging.error(f"Failed to parse config file at {config_path}")
except Exception as e:
logging.error(f"Error loading config file: {e}")
# Create widgets
# Channel input
channel_layout = BoxLayout(
orientation="horizontal", size_hint_y=None, height=dp(40)
)
channel_label = Label(text="Channel:", size_hint_x=0.3)
self.channel_input = TextInput(
multiline=False, size_hint_x=0.7, text=self.default_config["Channel"]
)
channel_layout.add_widget(channel_label)
channel_layout.add_widget(self.channel_input)
# Nickname input
nickname_layout = BoxLayout(
orientation="horizontal", size_hint_y=None, height=dp(40)
)
nickname_label = Label(text="Nickname:", size_hint_x=0.3)
self.nickname_input = TextInput(
multiline=False, size_hint_x=0.7, text=self.default_config["Nickname"]
)
nickname_layout.add_widget(nickname_label)
nickname_layout.add_widget(self.nickname_input)
# Authentication input
auth_layout = BoxLayout(
orientation="horizontal", size_hint_y=None, height=dp(40)
)
auth_label = Label(text="Auth:", size_hint_x=0.3)
self.auth_input = TextInput(
multiline=False,
size_hint_x=0.7,
password=True,
text=self.default_config["Authentication"],
)
auth_layout.add_widget(auth_label)
auth_layout.add_widget(self.auth_input)
# Save button
save_button = Button(
text="Save",
size_hint=(None, None),
size=(dp(100), dp(40)),
pos_hint={"center_x": 0.5},
)
save_button.bind(on_release=self.save_config)
# Add all widgets to the layout
self.add_widget(channel_layout)
self.add_widget(nickname_layout)
self.add_widget(auth_layout)
self.add_widget(save_button)
def save_config(self, instance):
# Get values from inputs
self.default_config["Channel"] = self.channel_input.text.strip()
self.default_config["Nickname"] = self.nickname_input.text.strip()
self.default_config["Authentication"] = self.auth_input.text.strip()
try:
# Create directory if it doesn't exist
self.config_path.parent.mkdir(parents=True, exist_ok=True)
# Save configuration
with self.config_path.open("w") as f:
json.dump(self.default_config, f, indent=4)
# Show success message
success_popup = Popup(
title="Success",
content=Label(text="Configuration saved successfully"),
size_hint=(None, None),
size=(dp(250), dp(100)),
)
success_popup.open()
Clock.schedule_once(success_popup.dismiss, 1)
except Exception as e:
# Show error message if saving fails
error_popup = Popup(
title="Error",
content=Label(text=f"Failed to save configuration:\n{str(e)}"),
size_hint=(None, None),
size=(dp(400), dp(150)),
)
error_popup.open()