Added chat bot message command options

This commit is contained in:
tyler 2024-02-06 14:57:56 -05:00
parent adacddb97a
commit a3ba8aa721
9 changed files with 304 additions and 108 deletions

14
app.go
View file

@ -143,11 +143,11 @@ func (a *App) ChatBotMessages(cid string) (map[string]config.ChatMessage, error)
return channel.ChatBot.Messages, nil return channel.ChatBot.Messages, nil
} }
func (a *App) AddChatMessage(cid string, asChannel bool, command string, interval time.Duration, onCommand bool, text string, textFile string) (map[string]config.ChatMessage, error) { func (a *App) AddChatMessage(cid string, cm config.ChatMessage) (map[string]config.ChatMessage, error) {
var err error var err error
a.cfgMu.Lock() a.cfgMu.Lock()
defer a.cfgMu.Unlock() defer a.cfgMu.Unlock()
_, err = a.cfg.NewChatMessage(cid, asChannel, command, interval, onCommand, text, textFile) _, err = a.cfg.NewChatMessage(cid, cm)
if err != nil { if err != nil {
a.logError.Println("error creating new chat:", err) a.logError.Println("error creating new chat:", err)
return nil, fmt.Errorf("Error creating new chat message. Try again.") return nil, fmt.Errorf("Error creating new chat message. Try again.")
@ -164,10 +164,10 @@ func (a *App) AddChatMessage(cid string, asChannel bool, command string, interva
return a.cfg.Channels[cid].ChatBot.Messages, nil return a.cfg.Channels[cid].ChatBot.Messages, nil
} }
func (a *App) DeleteChatMessage(mid string, cid string) (map[string]config.ChatMessage, error) { func (a *App) DeleteChatMessage(cid string, cm config.ChatMessage) (map[string]config.ChatMessage, error) {
a.cbMu.Lock() a.cbMu.Lock()
if a.cb != nil { if a.cb != nil {
err := a.cb.StopMessage(mid) err := a.cb.StopMessage(cm.ID)
if err != nil { if err != nil {
a.logError.Println("error stopping chat bot message:", err) a.logError.Println("error stopping chat bot message:", err)
return nil, fmt.Errorf("Error stopping message. Try Again.") return nil, fmt.Errorf("Error stopping message. Try Again.")
@ -177,7 +177,7 @@ func (a *App) DeleteChatMessage(mid string, cid string) (map[string]config.ChatM
a.cfgMu.Lock() a.cfgMu.Lock()
defer a.cfgMu.Unlock() defer a.cfgMu.Unlock()
err := a.cfg.DeleteChatMessage(mid, cid) err := a.cfg.DeleteChatMessage(cid, cm)
if err != nil { if err != nil {
a.logError.Println("error deleting chat message:", err) a.logError.Println("error deleting chat message:", err)
return nil, fmt.Errorf("Error deleting chat message. Try again.") return nil, fmt.Errorf("Error deleting chat message. Try again.")
@ -194,11 +194,11 @@ func (a *App) DeleteChatMessage(mid string, cid string) (map[string]config.ChatM
return a.cfg.Channels[cid].ChatBot.Messages, nil return a.cfg.Channels[cid].ChatBot.Messages, nil
} }
func (a *App) UpdateChatMessage(id string, cid string, asChannel bool, command string, interval time.Duration, onCommand bool, text string, textFile string) (map[string]config.ChatMessage, error) { func (a *App) UpdateChatMessage(cid string, cm config.ChatMessage) (map[string]config.ChatMessage, error) {
var err error var err error
a.cfgMu.Lock() a.cfgMu.Lock()
defer a.cfgMu.Unlock() defer a.cfgMu.Unlock()
_, err = a.cfg.UpdateChatMessage(id, cid, asChannel, command, interval, onCommand, text, textFile) _, err = a.cfg.UpdateChatMessage(cid, cm)
if err != nil { if err != nil {
a.logError.Println("error updating chat message:", err) a.logError.Println("error updating chat message:", err)
return nil, fmt.Errorf("Error updating chat message. Try again.") return nil, fmt.Errorf("Error updating chat message. Try again.")

View file

@ -147,15 +147,18 @@ function StreamChatItem(props) {
}; };
const openChat = () => { const openChat = () => {
props.onItemClick( props.onItemClick({
props.chat.id, id: props.chat.id,
props.chat.as_channel, as_channel: props.chat.as_channel,
props.chat.command, command: props.chat.command,
intervalToTimer(props.chat.interval), interval: intervalToTimer(props.chat.interval),
props.chat.on_command, on_command: props.chat.on_command,
props.chat.text, on_command_follower: props.chat.on_command_follower,
props.chat.text_file on_command_rant_amount: props.chat.on_command_rant_amount,
); on_command_subscriber: props.chat.on_command_subscriber,
text: props.chat.text,
text_file: props.chat.text_file,
});
}; };
const startMessage = () => { const startMessage = () => {

View file

@ -51,26 +51,54 @@
font-size: 16px; font-size: 16px;
outline: none; outline: none;
padding: 5px 10px 5px 10px; padding: 5px 10px 5px 10px;
text-align: right; text-align: center;
width: 100%; width: 100%;
} }
.chat-command-option {
align-items: center;
display: flex;
flex-direction: row;
justify-content: center;
}
.chat-command-options {
align-items: center;
display: flex;
flex-direction: column;
justify-content: space-evenly;
}
.chat-command-label { .chat-command-label {
color: white; color: white;
height: 29px; height: 29px;
} }
.chat-command-input { .chat-command-rant-amount {
border: none; border: none;
border-radius: 34px; /* border-radius: 34px; */
box-sizing: border-box; box-sizing: border-box;
font-family: monospace; font-family: monospace;
font-size: 16px; font-size: 16px;
outline: none; outline: none;
padding: 5px 10px 5px 10px; /* padding: 5px 10px 5px 10px; */
padding: 5px;
text-align: center; text-align: center;
} }
.chat-command-rant-amount-label {
color: white;
font-family: sans-serif;
padding-right: 10px;
}
.chat-command-rant-amount-symbol {
color: white;
font-family: sans-serif;
font-size: 20px;
padding-right: 1px;
}
.chat-interval { .chat-interval {
align-items: center; align-items: center;
display: flex; display: flex;
@ -292,6 +320,8 @@ input:checked + .chat-toggle-slider:before {
} }
.chat-toggle-check-label { .chat-toggle-check-label {
color: white;
font-family: sans-serif;
padding-right: 5px; padding-right: 5px;
} }

View file

@ -11,6 +11,9 @@ export function StreamChatMessageModal(props) {
const [chatCommand, setChatCommand] = useState(props.chatCommand); const [chatCommand, setChatCommand] = useState(props.chatCommand);
const [error, setError] = useState(''); const [error, setError] = useState('');
const [onCommand, setOnCommand] = useState(props.onCommand); const [onCommand, setOnCommand] = useState(props.onCommand);
const [onCommandFollower, setOnCommandFollower] = useState(props.onCommandFollower);
const [onCommandRantAmount, setOnCommandRantAmount] = useState(props.onCommandRantAmount);
const [onCommandSubscriber, setOnCommandSubscriber] = useState(props.onCommandSubscriber);
const [openDelete, setOpenDelete] = useState(false); const [openDelete, setOpenDelete] = useState(false);
const [readFromFile, setReadFromFile] = useState(false); const [readFromFile, setReadFromFile] = useState(false);
const [text, setText] = useState(props.text); const [text, setText] = useState(props.text);
@ -22,6 +25,9 @@ export function StreamChatMessageModal(props) {
console.log('update chat'); console.log('update chat');
setAsChannel(props.asChannel); setAsChannel(props.asChannel);
setOnCommand(props.onCommand); setOnCommand(props.onCommand);
setOnCommandFollower(props.onCommandFollower);
setOnCommandSubscriber(props.onCommandSubscriber);
setOnCommandRantAmount(props.onCommandRantAmount);
setError(''); setError('');
setReadFromFile(props.textFile !== ''); setReadFromFile(props.textFile !== '');
setText(props.text); setText(props.text);
@ -37,6 +43,9 @@ export function StreamChatMessageModal(props) {
setText(''); setText('');
setTextFile(''); setTextFile('');
setOnCommand(false); setOnCommand(false);
setOnCommandFollower(false);
setOnCommandSubscriber(false);
setOnCommandRantAmount(0);
setTimer(''); setTimer('');
}; };
@ -66,14 +75,20 @@ export function StreamChatMessageModal(props) {
return; return;
} }
let ac = asChannel; let message = {
let oc = onCommand; id: props.chatID,
let cmd = chatCommand; as_channel: asChannel,
let int = timerToInterval(); command: chatCommand,
let txt = text; interval: timerToInterval(),
let txtfile = textFile; on_command: onCommand,
reset(); on_command_follower: onCommandFollower,
props.onSubmit(props.chatID, ac, cmd, int, oc, txt, txtfile); on_command_rant_amount: onCommandRantAmount,
on_command_subscriber: onCommandSubscriber,
text: text,
text_file: textFile,
};
props.onSubmit(message);
}; };
const deleteMessage = () => { const deleteMessage = () => {
@ -168,6 +183,23 @@ export function StreamChatMessageModal(props) {
setOnCommand(e.target.checked); setOnCommand(e.target.checked);
}; };
const checkCommandFollower = (e) => {
setOnCommandFollower(e.target.checked);
};
const checkCommandSubscriber = (e) => {
setOnCommandSubscriber(e.target.checked);
};
const updateRantAmount = (e) => {
let amount = parseInt(e.target.value);
if (isNaN(amount)) {
amount = 0;
}
setOnCommandRantAmount(amount);
};
const checkReadFromFile = (e) => { const checkReadFromFile = (e) => {
setReadFromFile(e.target.checked); setReadFromFile(e.target.checked);
if (!e.target.checked) { if (!e.target.checked) {
@ -190,12 +222,11 @@ export function StreamChatMessageModal(props) {
<Modal <Modal
onClose={close} onClose={close}
show={props.show} show={props.show}
style={{ minWidth: '300px', maxWidth: '400px' }} style={{ minHeight: '500px', minWidth: '300px', maxWidth: '400px' }}
cancelButton={props.chatID === '' ? 'Cancel' : ''} cancelButton={props.chatID === '' ? 'Cancel' : ''}
onCancel={deleteMessage} onCancel={deleteMessage}
deleteButton={props.chatID === '' ? '' : 'Delete'} deleteButton={props.chatID === '' ? '' : 'Delete'}
onDelete={deleteMessage} onDelete={deleteMessage}
style={{ minHeight: '450px', maxWidth: '400px' }}
submitButton={'Save'} submitButton={'Save'}
onSubmit={submit} onSubmit={submit}
title={'Chat Message'} title={'Chat Message'}
@ -278,6 +309,7 @@ export function StreamChatMessageModal(props) {
</label> </label>
</div> </div>
{onCommand ? ( {onCommand ? (
<div>
<div className='chat-command'> <div className='chat-command'>
<input <input
className='chat-command-input' className='chat-command-input'
@ -288,6 +320,53 @@ export function StreamChatMessageModal(props) {
value={chatCommand} value={chatCommand}
/> />
</div> </div>
<div className='chat-command-options'>
<div className='chat-toggle'>
<span className='chat-toggle-label'>Followers only</span>
<label className='chat-toggle-switch'>
<input
onChange={checkCommandFollower}
type='checkbox'
checked={onCommandFollower}
/>
<span className='chat-toggle-slider round'></span>
</label>
</div>
<div className='chat-toggle'>
<span className='chat-toggle-label'>Subscribers only</span>
<label className='chat-toggle-switch'>
<input
onChange={checkCommandSubscriber}
type='checkbox'
checked={onCommandSubscriber}
/>
<span className='chat-toggle-slider round'></span>
</label>
</div>
<div className='chat-interval'>
<span className='chat-command-rant-amount-label'>
Minimum rant amount
</span>
<div>
<span className='chat-command-rant-amount-symbol'>
$
</span>
<input
className='chat-command-rant-amount'
onChange={updateRantAmount}
placeholder='0'
size='4'
type='text'
value={
onCommandRantAmount === 0
? ''
: onCommandRantAmount
}
/>
</div>
</div>
</div>
</div>
) : ( ) : (
<div className='chat-command'> <div className='chat-command'>
<span className='chat-command-label'>{'\u00A0'}</span> <span className='chat-command-label'>{'\u00A0'}</span>

View file

@ -38,6 +38,9 @@ function Dashboard() {
const [chatAsChannel, setChatAsChannel] = useState(false); const [chatAsChannel, setChatAsChannel] = useState(false);
const [chatCommand, setChatCommand] = useState(''); const [chatCommand, setChatCommand] = useState('');
const [chatOnCommand, setChatOnCommand] = useState(false); const [chatOnCommand, setChatOnCommand] = useState(false);
const [chatOnCommandFollower, setChatOnCommandFollower] = useState(false);
const [chatOnCommandRantAmount, setChatOnCommandRantAmount] = useState(0);
const [chatOnCommandSubscriber, setChatOnCommandSubscriber] = useState(false);
const [chatID, setChatID] = useState(''); const [chatID, setChatID] = useState('');
const [chatInterval, setChatInterval] = useState(''); const [chatInterval, setChatInterval] = useState('');
const [chatText, setChatText] = useState(''); const [chatText, setChatText] = useState('');
@ -166,17 +169,24 @@ function Dashboard() {
setChatText(''); setChatText('');
setChatTextFile(''); setChatTextFile('');
setChatOnCommand(false); setChatOnCommand(false);
setChatOnCommandFollower(false);
setChatOnCommandRantAmount(0);
setChatOnCommandSubscriber(false);
setOpenChat(true); setOpenChat(true);
}; };
const editChat = (id, asChannel, command, interval, onCommand, text, textFile) => { // const editChat = (id, asChannel, command, interval, onCommand, text, textFile) => {
setChatAsChannel(asChannel); const editChat = (message) => {
setChatCommand(command); setChatAsChannel(message.as_channel);
setChatID(id); setChatCommand(message.command);
setChatInterval(interval); setChatID(message.id);
setChatOnCommand(onCommand); setChatInterval(message.interval);
setChatText(text); setChatOnCommand(message.on_command);
setChatTextFile(textFile); setChatOnCommandFollower(message.on_command_follower);
setChatOnCommandRantAmount(message.on_command_rant_amount);
setChatOnCommandSubscriber(message.on_command_subscriber);
setChatText(message.text);
setChatTextFile(message.text_file);
setOpenChat(true); setOpenChat(true);
}; };
@ -186,28 +196,31 @@ function Dashboard() {
return; return;
} }
StopChatBotMessage(id, cid) let message = { id: id };
StopChatBotMessage(id)
.then(() => { .then(() => {
DeleteChatMessage(id, cid) // DeleteChatMessage(id, cid)
DeleteChatMessage(cid, message)
.then((messages) => { .then((messages) => {
setChatBotMessages(messages); setChatBotMessages(messages);
}) })
.catch((error) => { .catch((error) => {
setError(error); setError(error);
console.log('Error deleting message:', error); // console.log('Error deleting message:', error);
}); });
}) })
.catch((error) => { .catch((error) => {
setError(error); setError(error);
console.log('Error stopping message:', error); // console.log('Error stopping message:', error);
}); });
}; };
const saveChat = (id, asChannel, command, interval, onCommand, text, textFile) => { // const saveChat = (id, asChannel, command, interval, onCommand, text, textFile) => {
console.log('save chat textfile:', textFile); const saveChat = (message) => {
setOpenChat(false); setOpenChat(false);
if (id === '') { if (message.id === '') {
AddChatMessage(cid, asChannel, command, interval, onCommand, text, textFile) // AddChatMessage(cid, asChannel, command, interval, onCommand, text, textFile)
AddChatMessage(cid, message)
.then((messages) => { .then((messages) => {
setChatBotMessages(messages); setChatBotMessages(messages);
}) })
@ -219,7 +232,8 @@ function Dashboard() {
return; return;
} }
UpdateChatMessage(id, cid, asChannel, command, interval, onCommand, text, textFile) // UpdateChatMessage(id, cid, asChannel, command, interval, onCommand, text, textFile)
UpdateChatMessage(cid, message)
.then((messages) => { .then((messages) => {
console.log(messages); console.log(messages);
setChatBotMessages(messages); setChatBotMessages(messages);
@ -261,6 +275,9 @@ function Dashboard() {
asChannel={chatAsChannel} asChannel={chatAsChannel}
chatCommand={chatCommand} chatCommand={chatCommand}
onCommand={chatOnCommand} onCommand={chatOnCommand}
onCommandFollower={chatOnCommandFollower}
onCommandRantAmount={chatOnCommandRantAmount}
onCommandSubscriber={chatOnCommandSubscriber}
interval={chatInterval} interval={chatInterval}
onClose={() => setOpenChat(false)} onClose={() => setOpenChat(false)}
onDelete={deleteChat} onDelete={deleteChat}

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.19
require ( require (
github.com/tylertravisty/go-utils v0.0.0-20230524204414-6893ae548909 github.com/tylertravisty/go-utils v0.0.0-20230524204414-6893ae548909
github.com/tylertravisty/rumble-livestream-lib-go v0.2.1 github.com/tylertravisty/rumble-livestream-lib-go v0.2.2
github.com/wailsapp/wails/v2 v2.7.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/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 h1:xrjIFqzGQXlCrCdMPpW6+SodGFSlrQ3ZNUCr3f5tF1g=
github.com/tylertravisty/go-utils v0.0.0-20230524204414-6893ae548909/go.mod h1:2W31Jhs9YSy7y500wsCOW0bcamGi9foQV1CKrfvfTxk= github.com/tylertravisty/go-utils v0.0.0-20230524204414-6893ae548909/go.mod h1:2W31Jhs9YSy7y500wsCOW0bcamGi9foQV1CKrfvfTxk=
github.com/tylertravisty/rumble-livestream-lib-go v0.2.1 h1:VtdzuMIBePVTc26ZQeHmk3g2wtrPoAOiSjYvf8s2GTY= github.com/tylertravisty/rumble-livestream-lib-go v0.2.2 h1:gWmw44gSNOK37UQIEWS0GVxFJABt/8np9ArhFSSZj1o=
github.com/tylertravisty/rumble-livestream-lib-go v0.2.1/go.mod h1:CACpHQV9xQqBKB7C13tUkL7O8Neb35+dJzRV1N211s4= github.com/tylertravisty/rumble-livestream-lib-go v0.2.2/go.mod h1:CACpHQV9xQqBKB7C13tUkL7O8Neb35+dJzRV1N211s4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 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/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=

View file

@ -2,9 +2,11 @@ package chatbot
import ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"html/template"
"log" "log"
"math/big" "math/big"
"os" "os"
@ -36,6 +38,9 @@ type message struct {
id string id string
interval time.Duration interval time.Duration
onCommand bool onCommand bool
onCommandFollower bool
onCommandRantAmount int
OnCommandSubscriber bool
text string text string
textFromFile []string textFromFile []string
} }
@ -96,6 +101,9 @@ func (cb *ChatBot) StartMessage(id string) error {
id: msg.ID, id: msg.ID,
interval: msg.Interval, interval: msg.Interval,
onCommand: msg.OnCommand, onCommand: msg.OnCommand,
onCommandFollower: msg.OnCommandFollower,
onCommandRantAmount: msg.OnCommandRantAmount,
OnCommandSubscriber: msg.OnCommandSubscriber,
text: msg.Text, text: msg.Text,
textFromFile: textFromFile, textFromFile: textFromFile,
} }
@ -127,14 +135,37 @@ func (cb *ChatBot) startCommand(ctx context.Context, m *message) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case <-ch: case cv := <-ch:
if m.onCommandFollower && !cv.IsFollower {
break
}
subscriber := false
for _, badge := range cv.Badges {
if badge == "recurring_subscription" || badge == "locals_supporter" {
subscriber = true
}
}
if m.OnCommandSubscriber && !subscriber {
break
}
// if m.onCommandRantAmount > 0 && cv.Rant < m.onCommandRantAmount * 100 {
// break
// }
if cv.Rant < m.onCommandRantAmount*100 {
break
}
// TODO: parse !command // TODO: parse !command
now := time.Now() now := time.Now()
if now.Sub(prev) < m.interval*time.Second { if now.Sub(prev) < m.interval*time.Second {
break break
} }
err := cb.chat(m) err := cb.chatCommand(m, cv)
if err != nil { if err != nil {
cb.logError.Println("error sending chat:", err) cb.logError.Println("error sending chat:", err)
cb.StopMessage(m.id) cb.StopMessage(m.id)
@ -171,6 +202,51 @@ func (cb *ChatBot) startMessage(ctx context.Context, m *message) {
} }
} }
func (cb *ChatBot) chatCommand(m *message, cv rumblelivestreamlib.ChatView) error {
if cb.client == nil {
return fmt.Errorf("client is nil")
}
msgText := m.text
if len(m.textFromFile) > 0 {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(m.textFromFile))))
if err != nil {
return fmt.Errorf("error generating random number: %v", err)
}
msgText = m.textFromFile[n.Int64()]
}
tmpl, err := template.New("chat").Parse(msgText)
if err != nil {
return fmt.Errorf("error creating template: %v", err)
}
fields := struct {
ChannelName string
Username string
Rant int
}{
ChannelName: cv.ChannelName,
Username: cv.Username,
Rant: cv.Rant / 100,
}
var textB bytes.Buffer
err = tmpl.Execute(&textB, fields)
if err != nil {
return fmt.Errorf("error executing template: %v", err)
}
text := textB.String()
err = cb.client.Chat(m.asChannel, text)
if err != nil {
return fmt.Errorf("error sending chat: %v", err)
}
return nil
}
func (cb *ChatBot) chat(m *message) error { func (cb *ChatBot) chat(m *message) error {
if cb.client == nil { if cb.client == nil {
return fmt.Errorf("client is nil") return fmt.Errorf("client is nil")

View file

@ -78,6 +78,9 @@ type ChatMessage struct {
Command string `json:"command"` Command string `json:"command"`
Interval time.Duration `json:"interval"` Interval time.Duration `json:"interval"`
OnCommand bool `json:"on_command"` OnCommand bool `json:"on_command"`
OnCommandFollower bool `json:"on_command_follower"`
OnCommandRantAmount int `json:"on_command_rant_amount"`
OnCommandSubscriber bool `json:"on_command_subscriber"`
Text string `json:"text"` Text string `json:"text"`
TextFile string `json:"text_file"` TextFile string `json:"text_file"`
} }
@ -109,23 +112,23 @@ func (a *App) NewChannel(url string, name string) (string, error) {
} }
} }
func (a *App) DeleteChatMessage(id string, cid string) error { func (a *App) DeleteChatMessage(cid string, cm ChatMessage) error {
channel, exists := a.Channels[cid] channel, exists := a.Channels[cid]
if !exists { if !exists {
return fmt.Errorf("config: channel does not exist") return fmt.Errorf("config: channel does not exist")
} }
_, exists = channel.ChatBot.Messages[id] _, exists = channel.ChatBot.Messages[cm.ID]
if !exists { if !exists {
return fmt.Errorf("config: message does not exist") return fmt.Errorf("config: message does not exist")
} }
delete(channel.ChatBot.Messages, id) delete(channel.ChatBot.Messages, cm.ID)
return nil return nil
} }
func (a *App) NewChatMessage(cid string, asChannel bool, command string, interval time.Duration, onCommand bool, text string, textFile string) (string, error) { func (a *App) NewChatMessage(cid string, cm ChatMessage) (string, error) {
if _, exists := a.Channels[cid]; !exists { if _, exists := a.Channels[cid]; !exists {
return "", fmt.Errorf("config: channel does not exist") return "", fmt.Errorf("config: channel does not exist")
} }
@ -136,41 +139,29 @@ func (a *App) NewChatMessage(cid string, asChannel bool, command string, interva
return "", fmt.Errorf("config: error generating ID: %v", err) return "", fmt.Errorf("config: error generating ID: %v", err)
} }
if _, exists := a.Channels[cid].ChatBot.Messages[id]; !exists { _, exists := a.Channels[cid].ChatBot.Messages[id]
a.Channels[cid].ChatBot.Messages[id] = ChatMessage{ if !exists {
ID: id, cm.ID = id
AsChannel: asChannel, a.Channels[cid].ChatBot.Messages[id] = cm
Command: command,
Interval: interval,
OnCommand: onCommand,
Text: text,
TextFile: textFile,
}
return id, nil return id, nil
} }
} }
} }
func (a *App) UpdateChatMessage(id string, cid string, asChannel bool, command string, interval time.Duration, onCommand bool, text string, textFile string) (string, error) { func (a *App) UpdateChatMessage(cid string, cm ChatMessage) (string, error) {
channel, exists := a.Channels[cid] channel, exists := a.Channels[cid]
if !exists { if !exists {
return "", fmt.Errorf("config: channel does not exist") return "", fmt.Errorf("config: channel does not exist")
} }
cm, exists := channel.ChatBot.Messages[id] _, exists = channel.ChatBot.Messages[cm.ID]
if !exists { if !exists {
return "", fmt.Errorf("config: message does not exist") return "", fmt.Errorf("config: message does not exist")
} }
cm.AsChannel = asChannel channel.ChatBot.Messages[cm.ID] = cm
cm.Command = command
cm.Interval = interval
cm.OnCommand = onCommand
cm.Text = text
cm.TextFile = textFile
channel.ChatBot.Messages[id] = cm
return id, nil return cm.ID, nil
} }
type App struct { type App struct {