restructure

This commit is contained in:
lyx0 2022-08-07 01:34:31 +02:00
parent 79c9fa9da7
commit fee71708e7
7 changed files with 74 additions and 88 deletions

View file

@ -1,13 +1,14 @@
package bot
package main
import (
"strings"
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/pkg/commands"
)
func (bot *Bot) handleCommand(message twitch.PrivateMessage) {
bot.logger.Info("[COMMAND HANDLER]", message)
func (app *Application) handleCommand(message twitch.PrivateMessage) {
app.Logger.Info("[COMMAND HANDLER]", message)
// commandName is the actual name of the command without the prefix.
// e.g. `()ping` would be `ping`.
@ -34,15 +35,16 @@ func (bot *Bot) handleCommand(message twitch.PrivateMessage) {
switch commandName {
case "":
if msgLen == 1 {
bot.Send(target, "xd")
app.Send(target, "xd")
return
}
case "echo":
if msgLen < 2 {
bot.Send(target, "Not enough arguments provided.")
app.Send(target, "Not enough arguments provided.")
return
} else {
bot.Send(target, message.Message[7:len(message.Message)])
commands.Echo(target, message.Message[7:len(message.Message)])
// bot.Send(target, message.Message[7:len(message.Message)])
return
}
}

View file

@ -1,13 +1,46 @@
package main
import "github.com/lyx0/nourybot/pkg/bot"
import (
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/config"
"github.com/sirupsen/logrus"
)
type Application struct {
TwitchClient *twitch.Client
Logger *logrus.Logger
}
func main() {
cfg := config.New()
bot := bot.New()
twitchClient := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
bot.TwitchClient.Join("nourylul")
err := bot.TwitchClient.Connect()
app := Application{
TwitchClient: twitchClient,
Logger: logrus.New(),
}
// Received a PrivateMessage (normal chat message), pass it to
// the handler who checks for further action.
app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
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)
})
// 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")
})
app.TwitchClient.Join("nourylul")
err := app.TwitchClient.Connect()
if err != nil {
panic(err)
}

View file

@ -1,28 +1,28 @@
package bot
package main
import "github.com/gempir/go-twitch-irc/v3"
func (bot *Bot) handlePrivateMessage(message twitch.PrivateMessage) {
func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) {
// 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 == "" {
bot.logger.Error("Missing room-id in message tag ", roomId)
app.Logger.Error("Missing room-id in message tag ", roomId)
return
}
if len(message.Message) >= 2 {
if message.Message[:2] == "()" {
// TODO: Command Handling
bot.handleCommand(message)
app.handleCommand(message)
// app.logger.Infof("[Command detected]: ", message.Message)
return
}
}
// Message was no command so we just print it.
bot.logger.Infof("[#%s]:%s: %s", message.Channel, message.User.DisplayName, message.Message)
app.Logger.Infof("[#%s]:%s: %s", message.Channel, message.User.DisplayName, message.Message)
}

View file

@ -1,4 +1,4 @@
package bot
package main
import (
"bytes"
@ -6,6 +6,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"github.com/sirupsen/logrus"
)
// banphraseResponse is the data we receive back from
@ -35,26 +37,26 @@ var (
// returns true and a string with the reason if it was banned.
// More information:
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
func (bot *Bot) checkMessage(text string) (bool, string) {
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 {
bot.logger.Error(err)
l.Error(err)
}
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
if err != nil {
bot.logger.Error(err)
l.Error(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
bot.logger.Error(err)
l.Error(err)
}
var responseObject banphraseResponse
@ -77,7 +79,7 @@ func (bot *Bot) checkMessage(text string) (bool, string) {
// Send is used to send twitch replies and contains the necessary
// safeguards and logic for that.
func (bot *Bot) Send(target, message string) {
func (app *Application) Send(target, message string) {
// Message we are trying to send is empty.
if len(message) == 0 {
return
@ -92,11 +94,11 @@ func (bot *Bot) Send(target, message string) {
}
// check the message for bad words before we say it
messageBanned, banReason := bot.checkMessage(message)
messageBanned, banReason := checkMessage(message)
if messageBanned {
// Bad message, replace message and log it.
bot.TwitchClient.Say(target, "[BANPHRASED] monkaS")
bot.logger.Info("Banned message detected: ", banReason)
app.TwitchClient.Say(target, "[BANPHRASED] monkaS")
app.Logger.Info("Banned message detected: ", banReason)
return
} else {
@ -107,13 +109,13 @@ func (bot *Bot) Send(target, message string) {
firstMessage := message[0:499]
secondMessage := message[499:]
bot.TwitchClient.Say(target, firstMessage)
bot.TwitchClient.Say(target, secondMessage)
app.TwitchClient.Say(target, firstMessage)
app.TwitchClient.Say(target, secondMessage)
return
}
// Message was fine.
bot.TwitchClient.Say(target, message)
app.TwitchClient.Say(target, message)
return
}
}

View file

@ -0,0 +1,9 @@
package main
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)
}

View file

@ -1,51 +0,0 @@
package bot
import (
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/config"
"github.com/sirupsen/logrus"
)
type Bot struct {
TwitchClient *twitch.Client
logger *logrus.Logger
}
func New() *Bot {
// Initialize a new logger we attach to our application struct.
lgr := logrus.New()
cfg := config.New()
// Initialize a new twitch client which we attach to our
// application struct.
logrus.Info(cfg.TwitchUsername, cfg.TwitchOauth)
twitchClient := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
bot := &Bot{
TwitchClient: twitchClient,
logger: lgr,
}
// Received a PrivateMessage (normal chat message), pass it to
// the handler who checks for further action.
bot.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
bot.handlePrivateMessage(message)
})
// Received a WhisperMessage (Twitch DM), pass it to
// the handler who checks for further action.
bot.TwitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
bot.handleWhisperMessage(message)
})
// Successfully connected to Twitch so we log a message with the
// mode we are currently running in..
bot.TwitchClient.OnConnect(func() {
bot.logger.Infof("Successfully connected to Twitch Servers in %s mode!", cfg.Environment)
bot.Send("nourylul", "xd")
})
return bot
}

View file

@ -1,9 +0,0 @@
package bot
import "github.com/gempir/go-twitch-irc/v3"
func (bot *Bot) handleWhisperMessage(message twitch.WhisperMessage) {
// Print the whisper message for now.
// TODO: Implement a basic whisper handler.
bot.logger.Infof("[#whisper]:%s: %s", message.User.DisplayName, message.Message)
}