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
|
||||
|
||||
require (
|
||||
github.com/go-chat-bot/plugins v0.0.0-20201024114236-00ff43fcf77f
|
||||
github.com/thoj/go-ircevent v0.0.0-20190807115034-8e7ce4b5a1eb
|
||||
)
|
||||
require 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/go.mod h1:I0ZT9x8wStY6VOxtNOrLpnDURFs7HS0z1e1vhuKUEVc=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
|
104
main.go
104
main.go
@ -1,15 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-chat-bot/plugins/web"
|
||||
"log"
|
||||
|
||||
irc "github.com/thoj/go-ircevent"
|
||||
)
|
||||
|
||||
@ -17,6 +20,7 @@ const channel = "#ctf"
|
||||
|
||||
const (
|
||||
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 ^_^"
|
||||
gifPrefix = "Meow! Here's a gif: %s ^_^"
|
||||
)
|
||||
@ -31,10 +35,57 @@ var (
|
||||
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() {
|
||||
nick := os.Getenv("CTF2021NICK")
|
||||
irccon := irc.IRC(nick, os.Getenv("CTF2021USER"))
|
||||
|
||||
s := newStats()
|
||||
|
||||
irccon.VerboseCallbackHandler = false
|
||||
irccon.Debug = false
|
||||
irccon.UseTLS = false
|
||||
@ -52,12 +103,16 @@ func main() {
|
||||
e.Connection.Privmsg(channel, `Now with gifs! ^_^`)
|
||||
})
|
||||
|
||||
irccon.AddCallback("PRIVMSG", func(e *irc.Event) {
|
||||
log.Printf("Message from %s received: %s", e.Nick, e.Message())
|
||||
doCommand(e.Message(), e.Connection)
|
||||
irccon.AddCallback("MODE", func(e *irc.Event) {
|
||||
log.Println(e.Message())
|
||||
})
|
||||
|
||||
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"))
|
||||
if err != nil {
|
||||
fmt.Printf("Err %s", err)
|
||||
@ -66,8 +121,13 @@ func main() {
|
||||
irccon.Loop()
|
||||
}
|
||||
|
||||
func doCommand(command string, con *irc.Connection) {
|
||||
if ok := checkCommad(command, con); !ok {
|
||||
func doCommand(command string, e *irc.Event, s *stats) {
|
||||
if ok := statCommand(command); ok {
|
||||
s.printStats(e)
|
||||
return
|
||||
}
|
||||
|
||||
if ok := checkCommad(command, e.Connection); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
@ -75,10 +135,17 @@ func doCommand(command string, con *irc.Connection) {
|
||||
i := rand.Intn(50)
|
||||
fmt.Println(i)
|
||||
if i > 30 {
|
||||
catGif(con)
|
||||
catGif(e.Connection)
|
||||
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 {
|
||||
@ -97,10 +164,25 @@ func catGif(con *irc.Connection) (string, error) {
|
||||
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) {
|
||||
|
||||
data := &catFact{}
|
||||
err := web.GetJSON(catFactsURL, data)
|
||||
err := getJson(catFactsURL, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
Reference in New Issue
Block a user