This commit is contained in:
lyx0 2022-08-07 04:35:03 +02:00
parent dc70232d54
commit 33f61e2f0f
6 changed files with 25 additions and 15 deletions

View file

@ -37,8 +37,8 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) {
sugar.Infow("Command received", sugar.Infow("Command received",
// "message", message, // "message", message,
"message.Channel", target,
"message.Message", message.Message, "message.Message", message.Message,
"target", target,
"commandName", commandName, "commandName", commandName,
"cmdParams", cmdParams, "cmdParams", cmdParams,
"msgLen", msgLen, "msgLen", msgLen,

View file

@ -15,11 +15,10 @@ type Application struct {
func main() { func main() {
cfg := config.New() cfg := config.New()
tc := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
sugar := zap.NewExample().Sugar() sugar := zap.NewExample().Sugar()
defer sugar.Sync() defer sugar.Sync()
tc := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
app := &Application{ app := &Application{
TwitchClient: tc, TwitchClient: tc,
Logger: sugar, Logger: sugar,
@ -40,9 +39,10 @@ func main() {
// Successfully connected to Twitch so we log a message with the // Successfully connected to Twitch so we log a message with the
// mode we are currently running in.. // mode we are currently running in..
app.TwitchClient.OnConnect(func() { app.TwitchClient.OnConnect(func() {
sugar.Infow("Successfully connected to Twitch Servers", app.Logger.Infow("Successfully connected to Twitch Servers",
"Bot username", cfg.TwitchUsername, "Bot username", cfg.TwitchUsername,
"Environment", cfg.Environment) "Environment", cfg.Environment,
)
common.Send("nourylul", "xd", app.TwitchClient) common.Send("nourylul", "xd", app.TwitchClient)
}) })

View file

@ -10,17 +10,19 @@ func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) {
defer sugar.Sync() defer sugar.Sync()
// roomId is the Twitch UserID of the channel the message originated from. // roomId is the Twitch UserID of the channel the message originated from.
// If there is no roomId something went really wrong.
roomId := message.Tags["room-id"] roomId := message.Tags["room-id"]
// If there is no roomId something went wrong.
if roomId == "" { if roomId == "" {
sugar.Errorw("Missing room-id in message tag", app.Logger.Errorw("Missing room-id in message tag",
"roomId", roomId) "roomId", roomId,
)
return return
} }
// Message was shorter than our prefix is therefore it's irrelevant for us. // Message was shorter than our prefix is therefore it's irrelevant for us.
if len(message.Message) >= 2 { if len(message.Message) >= 2 {
// Strip the `()` prefix
if message.Message[:2] == "()" { if message.Message[:2] == "()" {
handleCommand(message, app.TwitchClient) handleCommand(message, app.TwitchClient)
return return
@ -29,7 +31,7 @@ func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) {
// Message was no command so we just print it. // Message was no command so we just print it.
app.Logger.Infow("Private Message received", app.Logger.Infow("Private Message received",
"message", message, // "message", message,
"message.Channel", message.Channel, "message.Channel", message.Channel,
"message.User", message.User.DisplayName, "message.User", message.User.DisplayName,
"message.Message", message.Message, "message.Message", message.Message,

View file

@ -8,5 +8,6 @@ func (app *Application) handleWhisperMessage(message twitch.WhisperMessage) {
app.Logger.Infow("Whisper Message received", app.Logger.Infow("Whisper Message received",
"message", message, "message", message,
"message.User.DisplayName", message.User.DisplayName, "message.User.DisplayName", message.User.DisplayName,
"message.Message", message.Message) "message.Message", message.Message,
)
} }

2
go.sum
View file

@ -5,6 +5,8 @@ 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/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 h1:bUVZ5mADhH7KidJVcl+z79kgLJ7sjdAk4b/ylAvaLy0=
github.com/gempir/go-twitch-irc/v3 v3.1.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU= github.com/gempir/go-twitch-irc/v3 v3.1.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU=
github.com/gempir/go-twitch-irc/v3 v3.2.0 h1:ENhsa7RgBE1GMmDqe0iMkvcSYfgw6ZsXilt+sAg32/U=
github.com/gempir/go-twitch-irc/v3 v3.2.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= 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/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=

View file

@ -93,7 +93,7 @@ func Send(target, message string, tc *twitch.Client) {
} }
// Since messages starting with `.` or `/` are used for special actions // Since messages starting with `.` or `/` are used for special actions
// (ban, whisper, timeout) and so on, we place a emote infront of it so // (ban, whisper, timeout) and so on, we place an emote infront of it so
// the actions wouldn't execute. `!` and `$` are common bot prefixes so we // the actions wouldn't execute. `!` and `$` are common bot prefixes so we
// don't allow them either. // don't allow them either.
if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' { if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' {
@ -106,13 +106,18 @@ func Send(target, message string, tc *twitch.Client) {
// Bad message, replace message and log it. // Bad message, replace message and log it.
tc.Say(target, "[BANPHRASED] monkaS") tc.Say(target, "[BANPHRASED] monkaS")
sugar.Infow("banned message detected", sugar.Infow("banned message detected",
"banReason", banReason) "target channel", target,
"message", message,
"ban reason", banReason,
)
return return
} else { } else {
// In case the message we are trying to send is longer than the // In case the message we are trying to send is longer than the
// maximum allowed message length on twitch. We split the message // maximum allowed message length on twitch we split the message in two parts.
// in two parts. // Twitch has a maximum length for messages of 510 characters so to be safe
// we split and check at 500 characters.
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
if len(message) > 500 { if len(message) > 500 {
firstMessage := message[0:499] firstMessage := message[0:499]
secondMessage := message[499:] secondMessage := message[499:]