mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
replace logrus with zap
This commit is contained in:
parent
effffff270
commit
edae669bee
7 changed files with 113 additions and 35 deletions
|
@ -6,17 +6,21 @@ import (
|
|||
"github.com/gempir/go-twitch-irc/v3"
|
||||
"github.com/lyx0/nourybot/pkg/commands"
|
||||
"github.com/lyx0/nourybot/pkg/common"
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) {
|
||||
logrus.Info("[COMMAND HANDLER]", message)
|
||||
logrus.Info(message)
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
//sugar.Infow("Command received",
|
||||
// "message", message)
|
||||
|
||||
// commandName is the actual name of the command without the prefix.
|
||||
// e.g. `()ping` would be `ping`.
|
||||
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
|
||||
logrus.Info(commandName)
|
||||
sugar.Infow("[handleCommand]",
|
||||
"commandName", commandName)
|
||||
|
||||
// cmdParams are additional command parameters.
|
||||
// e.g. `()weather san antonio`
|
||||
|
@ -31,7 +35,8 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) {
|
|||
// msgLen is the amount of words in a message without the prefix.
|
||||
// Useful to check if enough cmdParams are provided.
|
||||
msgLen := len(strings.SplitN(message.Message, " ", -2))
|
||||
logrus.Info(msgLen)
|
||||
// sugar.Infow("handleCommand",
|
||||
// "msgLen:", msgLen)
|
||||
|
||||
// target is the channelname the message originated from and
|
||||
// where we are responding.
|
||||
|
@ -49,7 +54,6 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) {
|
|||
return
|
||||
} else {
|
||||
commands.Echo(target, message.Message[7:len(message.Message)], tc)
|
||||
// bot.Send(target, message.Message[7:len(message.Message)])
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,41 +4,46 @@ import (
|
|||
"github.com/gempir/go-twitch-irc/v3"
|
||||
"github.com/lyx0/nourybot/internal/config"
|
||||
"github.com/lyx0/nourybot/pkg/common"
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
TwitchClient *twitch.Client
|
||||
Logger *logrus.Logger
|
||||
Logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg := config.New()
|
||||
lgr := logrus.New()
|
||||
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
tc := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
|
||||
|
||||
app := &Application{
|
||||
TwitchClient: tc,
|
||||
Logger: lgr,
|
||||
Logger: sugar,
|
||||
}
|
||||
// Received a PrivateMessage (normal chat message), pass it to
|
||||
// the handler who checks for further action.
|
||||
app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
||||
app.Logger.Infof("%s", message)
|
||||
|
||||
//app.Logger.Infof("%s", message)
|
||||
app.handlePrivateMessage(message)
|
||||
})
|
||||
|
||||
// Received a WhisperMessage (Twitch DM), pass it to
|
||||
// the handler who checks for further action.
|
||||
// twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
|
||||
// app.handleWhisperMessage(message)
|
||||
// })
|
||||
app.TwitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
|
||||
app.handleWhisperMessage(message)
|
||||
})
|
||||
|
||||
// Successfully connected to Twitch so we log a message with the
|
||||
// mode we are currently running in..
|
||||
app.TwitchClient.OnConnect(func() {
|
||||
app.Logger.Infof("Successfully connected to Twitch Servers in %s mode!", cfg.Environment)
|
||||
sugar.Infow("Successfully connected to Twitch Servers",
|
||||
"Bot username", cfg.TwitchUsername,
|
||||
"Environment", cfg.Environment)
|
||||
|
||||
common.Send("nourylul", "xd", app.TwitchClient)
|
||||
})
|
||||
app.TwitchClient.Join("nourylul")
|
||||
|
|
|
@ -1,29 +1,38 @@
|
|||
package main
|
||||
|
||||
import "github.com/gempir/go-twitch-irc/v3"
|
||||
import (
|
||||
"github.com/gempir/go-twitch-irc/v3"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) {
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
// roomId is the Twitch UserID of the channel the
|
||||
// message originated from.
|
||||
roomId := message.Tags["room-id"]
|
||||
|
||||
// If there is no roomId something went wrong.
|
||||
if roomId == "" {
|
||||
app.Logger.Error("Missing room-id in message tag ", roomId)
|
||||
sugar.Errorw("Missing room-id in message tag",
|
||||
"roomId", roomId)
|
||||
return
|
||||
}
|
||||
|
||||
if len(message.Message) >= 2 {
|
||||
if message.Message[:2] == "()" {
|
||||
// TODO: Command Handling
|
||||
app.Logger.Infof("[Command detected]: ", message.Message)
|
||||
// app.Logger.Infow("handlePrivateMessage",
|
||||
// "message.Message", message.Message)
|
||||
|
||||
handleCommand(message, app.TwitchClient)
|
||||
// app.logger.Infof("[Command detected]: ", message.Message)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Message was no command so we just print it.
|
||||
app.Logger.Infof("[#%s]:%s: %s", message.Channel, message.User.DisplayName, message.Message)
|
||||
app.Logger.Infow("Private Message received",
|
||||
"message.Channel", message.Channel,
|
||||
"message.User", message.User.DisplayName,
|
||||
"message.Message", message.Message)
|
||||
|
||||
}
|
||||
|
|
|
@ -5,5 +5,7 @@ import "github.com/gempir/go-twitch-irc/v3"
|
|||
func (app *Application) handleWhisperMessage(message twitch.WhisperMessage) {
|
||||
// Print the whisper message for now.
|
||||
// TODO: Implement a basic whisper handler.
|
||||
app.Logger.Infof("[#whisper]:%s: %s", message.User.DisplayName, message.Message)
|
||||
app.Logger.Infow("Whisper Message received",
|
||||
"message.User.DisplayName", message.User.DisplayName,
|
||||
"message.Message", message.Message)
|
||||
}
|
||||
|
|
7
go.mod
7
go.mod
|
@ -5,7 +5,10 @@ go 1.17
|
|||
require (
|
||||
github.com/gempir/go-twitch-irc/v3 v3.1.0
|
||||
github.com/joho/godotenv v1.4.0
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
go.uber.org/zap v1.21.0
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
|
||||
require (
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
)
|
||||
|
|
61
go.sum
61
go.sum
|
@ -1,14 +1,63 @@
|
|||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gempir/go-twitch-irc/v3 v3.1.0 h1:bUVZ5mADhH7KidJVcl+z79kgLJ7sjdAk4b/ylAvaLy0=
|
||||
github.com/gempir/go-twitch-irc/v3 v3.1.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU=
|
||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
|
||||
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
||||
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
|
||||
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// banphraseResponse is the data we receive back from
|
||||
|
@ -40,12 +40,15 @@ var (
|
|||
// More information:
|
||||
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
|
||||
func checkMessage(text string) (bool, string) {
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
// {"message": "AHAHAHAHA LUL"}
|
||||
reqBody, err := json.Marshal(map[string]string{
|
||||
"message": text,
|
||||
})
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
|
||||
|
@ -81,11 +84,13 @@ func checkMessage(text string) (bool, string) {
|
|||
// Send is used to send twitch replies and contains the necessary
|
||||
// safeguards and logic for that.
|
||||
func Send(target, message string, tc *twitch.Client) {
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
// Message we are trying to send is empty.
|
||||
if len(message) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Println(message)
|
||||
|
||||
// Since messages starting with `.` or `/` are used for special actions
|
||||
// (ban, whisper, timeout) and so on, we place a emote infront of it so
|
||||
|
@ -100,7 +105,8 @@ func Send(target, message string, tc *twitch.Client) {
|
|||
if messageBanned {
|
||||
// Bad message, replace message and log it.
|
||||
tc.Say(target, "[BANPHRASED] monkaS")
|
||||
logrus.Info("Banned message detected: ", banReason)
|
||||
sugar.Infow("banned message detected",
|
||||
"banReason", banReason)
|
||||
|
||||
return
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue