Fixed a bunch of bugs

This commit is contained in:
tyler 2024-01-31 15:29:06 -05:00
parent 60b8dd7bab
commit 84276e2eb8
8 changed files with 112 additions and 64 deletions

2
app.go
View file

@ -236,7 +236,7 @@ func (a *App) NewChatBot(cid string, username string, password string, streamUrl
a.cb, err = chatbot.NewChatBot(a.ctx, streamUrl, channel.ChatBot, a.logError)
if err != nil {
a.logError.Println("error creating new chat bot:", err)
return fmt.Errorf("Error creating new chat bot. Try Again.")
return fmt.Errorf("Error creating new chat bot. Try again.")
}
err = a.cb.Login(username, password)

View file

@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import { Modal } from './Modal';
import { Modal, SmallModal } from './Modal';
import { NewChatBot } from '../../wailsjs/go/main/App';
@ -73,6 +73,7 @@ export function ChatBotModal(props) {
};
return (
<>
<Modal
onClose={close}
show={props.show}
@ -90,7 +91,7 @@ export function ChatBotModal(props) {
title={'Chat Bot'}
>
<div className='chat-bot-modal'>
{error && <span className='chat-bot-error'>{error}</span>}
{/* {error && <span className='chat-bot-error'>{error}</span>} */}
<div className='chat-bot-setting'>
<span className='chat-bot-setting-label'>Username</span>
<input
@ -123,6 +124,16 @@ export function ChatBotModal(props) {
</div>
</div>
</Modal>
<SmallModal
onClose={() => setError('')}
show={error !== ''}
style={{ minWidth: '300px', maxWidth: '300px', maxHeight: '100px' }}
title={'Error'}
message={error}
submitButton={'OK'}
onSubmit={() => setError('')}
/>
</>
);
}

View file

@ -36,7 +36,12 @@ function StreamChatBot(props) {
</div>
<div className='stream-chatbot-list'>
{sortChatsAlpha().map((chat, index) => (
<StreamChatItem chat={props.chats[chat]} onItemClick={props.onEdit} />
<StreamChatItem
activateMessage={props.activateMessage}
chat={props.chats[chat]}
isMessageActive={props.isMessageActive}
onItemClick={props.onEdit}
/>
))}
</div>
</div>
@ -46,9 +51,9 @@ function StreamChatBot(props) {
export default StreamChatBot;
function StreamChatItem(props) {
const [active, setActive] = useState(props.chat.active);
const [active, setActive] = useState(props.isMessageActive(props.chat.id));
const [error, setError] = useState('');
const [filename, setFilename] = useState('');
const [filename, setFilename] = useState(props.chat.text_file);
useEffect(() => {
if (props.chat.text_file !== '') {
@ -56,11 +61,13 @@ function StreamChatItem(props) {
setFilename(name);
});
}
}, []);
setActive(props.isMessageActive(props.chat.id));
}, [props]);
const changeActive = (bool) => {
console.log('ChangeActive:', bool);
props.chat.active = bool;
// console.log('ChangeActive:', bool);
// props.chat.active = bool;
props.activateMessage(props.chat.id, bool);
setActive(bool);
};

View file

@ -44,6 +44,9 @@ function StreamEvent(props) {
let hours = hours24 % 12 || 12;
let minutes = date.getMinutes();
if (minutes < 10) {
minutes = '0' + minutes;
}
let mer = 'pm';
if (hours24 < 12) {

View file

@ -34,6 +34,7 @@ function Dashboard() {
const [active, setActive] = useState(false);
const [openChatBot, setOpenChatBot] = useState(false);
const [chatBotMessages, setChatBotMessages] = useState({});
const [chatBotMessagesActive, setChatBotMessagesActive] = useState({});
const [chatAsChannel, setChatAsChannel] = useState(false);
const [chatCommand, setChatCommand] = useState('');
const [chatOnCommand, setChatOnCommand] = useState(false);
@ -75,7 +76,7 @@ function Dashboard() {
});
EventsOn('QueryResponse', (response) => {
console.log('query response received');
// console.log('query response received');
setRefresh(!refresh);
setActive(true);
setUsername(response.username);
@ -102,7 +103,7 @@ function Dashboard() {
EventsOn('QueryResponseError', (error) => {
setError(error);
console.log('Query response error:', error);
// console.log('Query response error:', error);
setActive(false);
});
}, []);
@ -237,6 +238,21 @@ function Dashboard() {
.catch((error) => console.log('Error creating new chat bot:', error));
};
const activateMessage = (id, active) => {
// console.log('Dashboard activateMessage:', id, active);
chatBotMessagesActive[id] = active;
};
const isMessageActive = (id) => {
// console.log('is Message active start', id, chatBotMessagesActive[id]);
if (chatBotMessagesActive[id] === undefined) {
chatBotMessagesActive[id] = false;
}
// console.log('is Message active after', id, chatBotMessagesActive[id]);
return chatBotMessagesActive[id];
};
return (
<>
{openChat && (
@ -290,11 +306,13 @@ function Dashboard() {
</div> */}
<div className='main-right'>
<StreamChatBot
activateMessage={activateMessage}
chats={chatBotMessages}
onAdd={newChat}
onEdit={editChat}
onSettings={() => setOpenChatBot(true)}
title={'Chat Bot'}
isMessageActive={isMessageActive}
/>
</div>
</div>

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.19
require (
github.com/tylertravisty/go-utils v0.0.0-20230524204414-6893ae548909
github.com/tylertravisty/rumble-livestream-lib-go v0.2.0
github.com/tylertravisty/rumble-livestream-lib-go v0.2.1
github.com/wailsapp/wails/v2 v2.7.1
)

4
go.sum
View file

@ -57,8 +57,8 @@ github.com/tkrajina/go-reflector v0.5.6 h1:hKQ0gyocG7vgMD2M3dRlYN6WBBOmdoOzJ6njQ
github.com/tkrajina/go-reflector v0.5.6/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4=
github.com/tylertravisty/go-utils v0.0.0-20230524204414-6893ae548909 h1:xrjIFqzGQXlCrCdMPpW6+SodGFSlrQ3ZNUCr3f5tF1g=
github.com/tylertravisty/go-utils v0.0.0-20230524204414-6893ae548909/go.mod h1:2W31Jhs9YSy7y500wsCOW0bcamGi9foQV1CKrfvfTxk=
github.com/tylertravisty/rumble-livestream-lib-go v0.2.0 h1:sOXTZKBeB9PN3xfVSVYiILhMQdBQ2OiLMq+k70x5yb0=
github.com/tylertravisty/rumble-livestream-lib-go v0.2.0/go.mod h1:CACpHQV9xQqBKB7C13tUkL7O8Neb35+dJzRV1N211s4=
github.com/tylertravisty/rumble-livestream-lib-go v0.2.1 h1:VtdzuMIBePVTc26ZQeHmk3g2wtrPoAOiSjYvf8s2GTY=
github.com/tylertravisty/rumble-livestream-lib-go v0.2.1/go.mod h1:CACpHQV9xQqBKB7C13tUkL7O8Neb35+dJzRV1N211s4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=

View file

@ -41,7 +41,7 @@ type message struct {
}
func NewChatBot(ctx context.Context, streamUrl string, cfg config.ChatBot, logError *log.Logger) (*ChatBot, error) {
client, err := rumblelivestreamlib.NewClient("", streamUrl)
client, err := rumblelivestreamlib.NewClient("", validUrl(streamUrl))
if err != nil {
return nil, fmt.Errorf("chatbot: error creating new client: %v", err)
}
@ -49,6 +49,15 @@ func NewChatBot(ctx context.Context, streamUrl string, cfg config.ChatBot, logEr
return &ChatBot{ctx: ctx, client: client, Cfg: cfg, commands: map[string]chan rumblelivestreamlib.ChatView{}, logError: logError, messages: map[string]*message{}}, nil
}
func validUrl(url string) string {
valid := strings.TrimLeft(url, "http://")
if !strings.HasPrefix(valid, "https://") {
valid = "https://" + valid
}
return valid
}
func (cb *ChatBot) StartMessage(id string) error {
msg, exists := cb.Cfg.Messages[id]
if !exists {
@ -106,7 +115,6 @@ func (cb *ChatBot) StartMessage(id string) error {
return nil
}
// TODO: lock commands map, update commands map with channel, unlock commands map
func (cb *ChatBot) startCommand(ctx context.Context, m *message) {
cb.commandsMu.Lock()
ch := make(chan rumblelivestreamlib.ChatView)
@ -131,6 +139,7 @@ func (cb *ChatBot) startCommand(ctx context.Context, m *message) {
cb.logError.Println("error sending chat:", err)
cb.StopMessage(m.id)
runtime.EventsEmit(cb.ctx, "ChatBotCommandError-"+m.id, m.id)
return
} else {
prev = now
runtime.EventsEmit(cb.ctx, "ChatBotCommandActive-"+m.id, m.id)