Stats initial
This commit is contained in:
parent
f43426bbee
commit
c838efbf27
5
go.mod
5
go.mod
@ -2,7 +2,4 @@ module github.com/Difrex/ctf2021_ircbot
|
|||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb
|
||||||
github.com/go-chat-bot/plugins v0.0.0-20201024114236-00ff43fcf77f
|
|
||||||
github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb
|
|
||||||
)
|
|
||||||
|
2
go.sum
2
go.sum
@ -1,5 +1,3 @@
|
|||||||
github.com/go-chat-bot/plugins v0.0.0-20201024114236-00ff43fcf77f h1:To58XdIp+Sw9QoyhJE3oh2KMZKoj4Ta8W/ltrA+cL68=
|
|
||||||
github.com/go-chat-bot/plugins v0.0.0-20201024114236-00ff43fcf77f/go.mod h1:Ga63x4EC4NFYr/KGzhn8D8fLj89sfJA/dpBsuowiHOQ=
|
|
||||||
github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb h1:EavwSqheIJl3nb91HhkL73DwnT2Fk8W3yM7T7TuLZvA=
|
github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb h1:EavwSqheIJl3nb91HhkL73DwnT2Fk8W3yM7T7TuLZvA=
|
||||||
github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb/go.mod h1:I0ZT9x8wStY6VOxtNOrLpnDURFs7HS0z1e1vhuKUEVc=
|
github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb/go.mod h1:I0ZT9x8wStY6VOxtNOrLpnDURFs7HS0z1e1vhuKUEVc=
|
||||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||||
|
104
main.go
104
main.go
@ -1,15 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-chat-bot/plugins/web"
|
"log"
|
||||||
|
|
||||||
irc "github.com/thoj/go-ircevent"
|
irc "github.com/thoj/go-ircevent"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,6 +20,7 @@ const channel = "#ctf"
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
pattern = "(?i)\\b(cat|gato|miau|meow|garfield|lolcat)[s|z]{0,1}\\b"
|
pattern = "(?i)\\b(cat|gato|miau|meow|garfield|lolcat)[s|z]{0,1}\\b"
|
||||||
|
nyastat = "nyastat"
|
||||||
msgPrefix = "I love cats! Here's a fact: %s ^_^"
|
msgPrefix = "I love cats! Here's a fact: %s ^_^"
|
||||||
gifPrefix = "Meow! Here's a gif: %s ^_^"
|
gifPrefix = "Meow! Here's a gif: %s ^_^"
|
||||||
)
|
)
|
||||||
@ -31,10 +35,57 @@ var (
|
|||||||
catFactsURL = "http://catfact.ninja/fact"
|
catFactsURL = "http://catfact.ninja/fact"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type stats struct {
|
||||||
|
userMessages map[string]uint
|
||||||
|
mux *sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStats() *stats {
|
||||||
|
return &stats{
|
||||||
|
userMessages: make(map[string]uint),
|
||||||
|
mux: &sync.Mutex{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stats) addUser(user string) {
|
||||||
|
s.mux.Lock()
|
||||||
|
defer s.mux.Unlock()
|
||||||
|
|
||||||
|
if v, ok := s.userMessages[user]; ok {
|
||||||
|
log.Printf("User %s writes %d message", user, v+1)
|
||||||
|
s.userMessages[user]++
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("First message from user %s", user)
|
||||||
|
s.userMessages[user] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stats) online(e *irc.Event) int {
|
||||||
|
e.Connection.Mutex.Lock()
|
||||||
|
defer e.Connection.Mutex.Unlock()
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stats) realStats(e *irc.Event) {
|
||||||
|
e.Connection.Privmsg(channel, "🙀🙀🙀 WoW 🙀🙀🙀 NYAStat 🙀🙀🙀")
|
||||||
|
t := "User %s sent %d messages"
|
||||||
|
for u, v := range s.userMessages {
|
||||||
|
e.Connection.Privmsg(channel, fmt.Sprintf(t, u, v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stats) printStats(e *irc.Event) {
|
||||||
|
s.online(e)
|
||||||
|
s.realStats(e)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nick := os.Getenv("CTF2021NICK")
|
nick := os.Getenv("CTF2021NICK")
|
||||||
irccon := irc.IRC(nick, os.Getenv("CTF2021USER"))
|
irccon := irc.IRC(nick, os.Getenv("CTF2021USER"))
|
||||||
|
|
||||||
|
s := newStats()
|
||||||
|
|
||||||
irccon.VerboseCallbackHandler = false
|
irccon.VerboseCallbackHandler = false
|
||||||
irccon.Debug = false
|
irccon.Debug = false
|
||||||
irccon.UseTLS = false
|
irccon.UseTLS = false
|
||||||
@ -52,12 +103,16 @@ func main() {
|
|||||||
e.Connection.Privmsg(channel, `Now with gifs! ^_^`)
|
e.Connection.Privmsg(channel, `Now with gifs! ^_^`)
|
||||||
})
|
})
|
||||||
|
|
||||||
irccon.AddCallback("PRIVMSG", func(e *irc.Event) {
|
irccon.AddCallback("MODE", func(e *irc.Event) {
|
||||||
log.Printf("Message from %s received: %s", e.Nick, e.Message())
|
log.Println(e.Message())
|
||||||
doCommand(e.Message(), e.Connection)
|
})
|
||||||
|
|
||||||
|
irccon.AddCallback("PRIVMSG", func(e *irc.Event) {
|
||||||
|
log.Printf("Message from %s received: %s", e.Nick, e.Message())
|
||||||
|
s.addUser(e.Nick)
|
||||||
|
doCommand(e.Message(), e, s)
|
||||||
})
|
})
|
||||||
|
|
||||||
irccon.AddCallback("", func(e *irc.Event) {})
|
|
||||||
err := irccon.Connect(os.Getenv("CTF2021URL"))
|
err := irccon.Connect(os.Getenv("CTF2021URL"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Err %s", err)
|
fmt.Printf("Err %s", err)
|
||||||
@ -66,8 +121,13 @@ func main() {
|
|||||||
irccon.Loop()
|
irccon.Loop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func doCommand(command string, con *irc.Connection) {
|
func doCommand(command string, e *irc.Event, s *stats) {
|
||||||
if ok := checkCommad(command, con); !ok {
|
if ok := statCommand(command); ok {
|
||||||
|
s.printStats(e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := checkCommad(command, e.Connection); !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,10 +135,17 @@ func doCommand(command string, con *irc.Connection) {
|
|||||||
i := rand.Intn(50)
|
i := rand.Intn(50)
|
||||||
fmt.Println(i)
|
fmt.Println(i)
|
||||||
if i > 30 {
|
if i > 30 {
|
||||||
catGif(con)
|
catGif(e.Connection)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
catFacts(con)
|
catFacts(e.Connection)
|
||||||
|
}
|
||||||
|
|
||||||
|
func statCommand(command string) bool {
|
||||||
|
if command == nyastat {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkCommad(command string, con *irc.Connection) bool {
|
func checkCommad(command string, con *irc.Connection) bool {
|
||||||
@ -97,10 +164,25 @@ func catGif(con *irc.Connection) (string, error) {
|
|||||||
return fmt.Sprintf(gifPrefix, res.Request.URL.String()), nil
|
return fmt.Sprintf(gifPrefix, res.Request.URL.String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getJson(url string, v interface{}) error {
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(body, v)
|
||||||
|
}
|
||||||
|
|
||||||
func catFacts(con *irc.Connection) (string, error) {
|
func catFacts(con *irc.Connection) (string, error) {
|
||||||
|
|
||||||
data := &catFact{}
|
data := &catFact{}
|
||||||
err := web.GetJSON(catFactsURL, data)
|
err := getJson(catFactsURL, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user