@ -21,9 +21,9 @@ const PSTFormat = "Jan 2 15:04:05 PST"
// Regex for parsing PRIVMSG strings.
//
// First matched group is the user's name and the secon d matched group is the content of the
// First matched group is the user's name , second is the channel? and the thir d matched group is the content of the
// user's message.
var MsgRegex * regexp . Regexp = regexp . MustCompile ( ` ^:(\w+)!\w+@\w+\.tmi\.twitch\.tv (PRIVMSG) # \w+(?: :(.*))?$` )
var MsgRegex * regexp . Regexp = regexp . MustCompile ( ` ^:(\w+)!\w+@\w+\.tmi\.twitch\.tv (PRIVMSG) # ( \w+) (?: :(.*))?$` )
// Regex for parsing user commands, from already parsed PRIVMSG strings.
//
@ -38,6 +38,9 @@ type OAuthCred struct {
// The developer application client ID. Used for API calls to Twitch.
ClientID string ` json:"client_id,omitempty" `
// List of Channels to join
Channels [ ] string ` json:"channels,omitempty" `
}
type KardBot struct {
@ -111,10 +114,11 @@ func (bb *KardBot) HandleChat() error {
if nil != matches {
userName := matches [ 1 ]
msgType := matches [ 2 ]
channel := matches [ 3 ]
switch msgType {
case "PRIVMSG" :
msg := matches [ 3 ]
msg := matches [ 4 ]
rgb . GPrintf ( "[%s] %s: %s\n" , TimeStamp ( ) , userName , msg )
// parse commands from user message
@ -124,9 +128,9 @@ func (bb *KardBot) HandleChat() error {
switch cmd {
case "card" :
rgb . CPrintf ( "[%s] Card asked for !\n", TimeStamp ( ) )
rgb . CPrintf ( "[%s] Card asked for by %s on %s' channel !\n", TimeStamp ( ) , userName , channel )
bb . Say ( "Your prompt is : " + bb . Prompts [ rand . Intn ( len ( bb . Prompts ) ) ] )
bb . Say ( "Your prompt is : " + bb . Prompts [ rand . Intn ( len ( bb . Prompts ) ) ] , channel )
}
// channel-owner specific commands
@ -147,6 +151,7 @@ func (bb *KardBot) HandleChat() error {
}
default :
// do nothing
rgb . YPrintf ( "[%s] unknown IRC message : %s\n" , TimeStamp ( ) , line )
}
}
}
@ -154,14 +159,24 @@ func (bb *KardBot) HandleChat() error {
}
}
// Makes the bot join its pre-specified channel.
func ( bb * KardBot ) JoinChannel ( ) {
rgb . YPrintf ( "[%s] Joining #%s...\n", TimeStamp ( ) , bb . Channel )
// Login to the IRC server
func ( bb * KardBot ) Login ( ) {
rgb . YPrintf ( "[%s] Logging into #%s...\n", TimeStamp ( ) , bb . Channel )
bb . conn . Write ( [ ] byte ( "PASS " + bb . Credentials . Password + "\r\n" ) )
bb . conn . Write ( [ ] byte ( "NICK " + bb . Name + "\r\n" ) )
bb . conn . Write ( [ ] byte ( "JOIN #" + bb . Channel + "\r\n" ) )
}
// Makes the bot join its pre-specified channel.
func ( bb * KardBot ) JoinChannel ( channels ... string ) {
if len ( channels ) == 0 {
channels = append ( channels , bb . Channel )
}
rgb . YPrintf ( "[%s] Joined #%s as @%s!\n" , TimeStamp ( ) , bb . Channel , bb . Name )
for _ , channel := range channels {
rgb . YPrintf ( "[%s] Joining #%s...\n" , TimeStamp ( ) , channel )
bb . conn . Write ( [ ] byte ( "JOIN #" + channel + "\r\n" ) )
rgb . YPrintf ( "[%s] Joined #%s as @%s!\n" , TimeStamp ( ) , channel , bb . Name )
}
}
// Reads from the private credentials file and stores the data in the bot's Credentials field.
@ -185,7 +200,7 @@ func (bb *KardBot) ReadCredentials() error {
}
// Makes the bot send a message to the chat channel.
func ( bb * KardBot ) Say ( msg string ) error {
func ( bb * KardBot ) Say ( msg string , channels ... string ) error {
if "" == msg {
return errors . New ( "BasicBot.Say: msg was empty." )
}
@ -195,9 +210,17 @@ func (bb *KardBot) Say(msg string) error {
return errors . New ( "BasicBot.Say: msg exceeded 512 bytes" )
}
_ , err := bb . conn . Write ( [ ] byte ( fmt . Sprintf ( "PRIVMSG #%s :%s\r\n" , bb . Channel , msg ) ) )
if nil != err {
return err
if len ( channels ) == 0 {
channels = append ( channels , bb . Channel )
}
rgb . YPrintf ( "[%s] sending %s to channels %v as @%s!\n" , TimeStamp ( ) , msg , channels , bb . Name )
for _ , channel := range channels {
_ , err := bb . conn . Write ( [ ] byte ( fmt . Sprintf ( "PRIVMSG #%s :%s\r\n" , channel , msg ) ) )
rgb . YPrintf ( "[%s] PRIVMSG #%s :%s\r\n" , TimeStamp ( ) , channel , msg )
if nil != err {
return err
}
}
return nil
}
@ -215,7 +238,12 @@ func (bb *KardBot) Start() {
for {
bb . Connect ( )
bb . JoinChannel ( )
bb . Login ( )
if len ( bb . Credentials . Channels ) > 0 {
bb . JoinChannel ( bb . Credentials . Channels ... )
} else {
bb . JoinChannel ( )
}
err = bb . HandleChat ( )
if nil != err {