fix dereference error somehow

This commit is contained in:
lyx0 2022-08-07 02:27:40 +02:00
parent fee71708e7
commit effffff270
5 changed files with 45 additions and 29 deletions

View file

@ -5,14 +5,18 @@ import (
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/pkg/commands"
"github.com/lyx0/nourybot/pkg/common"
"github.com/sirupsen/logrus"
)
func (app *Application) handleCommand(message twitch.PrivateMessage) {
app.Logger.Info("[COMMAND HANDLER]", message)
func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) {
logrus.Info("[COMMAND HANDLER]", message)
logrus.Info(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)
// cmdParams are additional command parameters.
// e.g. `()weather san antonio`
@ -21,12 +25,13 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
// Since Twitch messages are at most 500 characters I use a
// maximum count of 500+10 just to be safe.
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
cmdParams := strings.SplitN(message.Message, " ", 510)
cmdParams := strings.SplitN(message.Message, " ", 500)
_ = cmdParams
// 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)
// target is the channelname the message originated from and
// where we are responding.
@ -35,15 +40,15 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
switch commandName {
case "":
if msgLen == 1 {
app.Send(target, "xd")
common.Send(target, "xd", tc)
return
}
case "echo":
if msgLen < 2 {
app.Send(target, "Not enough arguments provided.")
common.Send(target, "Not enough arguments provided.", tc)
return
} else {
commands.Echo(target, message.Message[7:len(message.Message)])
commands.Echo(target, message.Message[7:len(message.Message)], tc)
// bot.Send(target, message.Message[7:len(message.Message)])
return
}

View file

@ -3,6 +3,7 @@ package main
import (
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/config"
"github.com/lyx0/nourybot/pkg/common"
"github.com/sirupsen/logrus"
)
@ -13,33 +14,35 @@ type Application struct {
func main() {
cfg := config.New()
lgr := logrus.New()
twitchClient := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
app := Application{
TwitchClient: twitchClient,
Logger: logrus.New(),
tc := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
app := &Application{
TwitchClient: tc,
Logger: lgr,
}
// 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.handlePrivateMessage(message)
})
// Received a WhisperMessage (Twitch DM), pass it to
// the handler who checks for further action.
app.TwitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
app.handleWhisperMessage(message)
})
// 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)
app.Send("nourylul", "xd")
common.Send("nourylul", "xd", app.TwitchClient)
})
app.TwitchClient.Join("nourylul")
err := app.TwitchClient.Connect()
if err != nil {
panic(err)

View file

@ -16,7 +16,8 @@ func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) {
if len(message.Message) >= 2 {
if message.Message[:2] == "()" {
// TODO: Command Handling
app.handleCommand(message)
app.Logger.Infof("[Command detected]: ", message.Message)
handleCommand(message, app.TwitchClient)
// app.logger.Infof("[Command detected]: ", message.Message)
return
}

View file

@ -1,5 +1,10 @@
package commands
func Echo(target, message string) {
import (
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/pkg/common"
)
func Echo(target, message string, tc *twitch.Client) {
common.Send(target, message, tc)
}

View file

@ -1,12 +1,14 @@
package main
package common
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gempir/go-twitch-irc/v3"
"github.com/sirupsen/logrus"
)
@ -38,25 +40,24 @@ var (
// More information:
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
func checkMessage(text string) (bool, string) {
var l *logrus.Logger
// {"message": "AHAHAHAHA LUL"}
reqBody, err := json.Marshal(map[string]string{
"message": text,
})
if err != nil {
l.Error(err)
log.Panic(err)
}
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
if err != nil {
l.Error(err)
log.Panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
l.Error(err)
log.Panic(err)
}
var responseObject banphraseResponse
@ -79,11 +80,12 @@ func checkMessage(text string) (bool, string) {
// Send is used to send twitch replies and contains the necessary
// safeguards and logic for that.
func (app *Application) Send(target, message string) {
func Send(target, message string, tc *twitch.Client) {
// 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
@ -97,8 +99,8 @@ func (app *Application) Send(target, message string) {
messageBanned, banReason := checkMessage(message)
if messageBanned {
// Bad message, replace message and log it.
app.TwitchClient.Say(target, "[BANPHRASED] monkaS")
app.Logger.Info("Banned message detected: ", banReason)
tc.Say(target, "[BANPHRASED] monkaS")
logrus.Info("Banned message detected: ", banReason)
return
} else {
@ -109,13 +111,13 @@ func (app *Application) Send(target, message string) {
firstMessage := message[0:499]
secondMessage := message[499:]
app.TwitchClient.Say(target, firstMessage)
app.TwitchClient.Say(target, secondMessage)
tc.Say(target, firstMessage)
tc.Say(target, secondMessage)
return
}
// Message was fine.
app.TwitchClient.Say(target, message)
tc.Say(target, message)
return
}
}