Track user online status

This commit is contained in:
DataHoarder 2023-04-22 11:21:25 +02:00
parent 9e25a80261
commit ffd83d4a2c
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

67
bot.go
View file

@ -259,6 +259,7 @@ func main() {
))
}
// Track channel joins to have users nicks as connected
bot.AddTrigger(hbot.Trigger{
Condition: func(bot *hbot.Bot, message *hbot.Message) bool {
return message.Command == irc.RPL_NAMREPLY || message.Command == irc.RPL_ENDOFNAMES
@ -285,6 +286,72 @@ func main() {
},
})
//Tracks nick changes
bot.AddTrigger(hbot.Trigger{
Condition: func(bot *hbot.Bot, message *hbot.Message) bool {
return message.Command == irc.NICK
},
Action: func(bot *hbot.Bot, message *hbot.Message) bool {
if message.Command == irc.NICK {
if len(message.Params) != 1 {
return false
}
from := message.From
to := message.To
onlineUsersLock.Lock()
defer onlineUsersLock.Unlock()
if i := slices.Index(onlineUsers, from); i != -1 {
onlineUsers[i] = to
} else {
log.Printf("Could not find old user %s -> %s", from, to)
}
}
return false
},
})
//Tracks JOIN/LEAVE
bot.AddTrigger(hbot.Trigger{
Condition: func(bot *hbot.Bot, message *hbot.Message) bool {
return message.Command == irc.JOIN || message.Command == irc.QUIT || message.Command == irc.PART
},
Action: func(bot *hbot.Bot, message *hbot.Message) bool {
if message.Command == irc.JOIN {
if len(message.Params) != 1 {
return false
}
nick := message.From
channelName := message.To
onlineUsersLock.Lock()
defer onlineUsersLock.Unlock()
var channel *channelEntry
for _, c := range channelEntries {
if c.Channel == channelName {
channel = c
break
}
}
if channel == nil {
//not our channels
return false
}
if i := slices.Index(onlineUsers, nick); i == -1 {
onlineUsers = append(onlineUsers, nick)
}
} else if message.Command == irc.QUIT {
nick := message.From
onlineUsersLock.Lock()
defer onlineUsersLock.Unlock()
if i := slices.Index(onlineUsers, nick); i != -1 {
onlineUsers = slices.Delete(onlineUsers, i, i+1)
} else {
log.Printf("Could not find user who quit %s", nick)
}
}
return false
},
})
//Private message
bot.AddTrigger(hbot.Trigger{
Condition: func(bot *hbot.Bot, message *hbot.Message) bool {