start rewrite

This commit is contained in:
lyx0 2022-08-06 23:45:02 +02:00
parent aa565a86e9
commit 86c7217974
4 changed files with 55 additions and 34 deletions

View file

@ -1 +1,14 @@
package main
package main
import "github.com/lyx0/nourybot/pkg/bot"
func main() {
bot := bot.New()
bot.TwitchClient.Join("nourylul")
err := bot.TwitchClient.Connect()
if err != nil {
panic(err)
}
}

View file

@ -1 +1,26 @@
package config
import (
"log"
"os"
"github.com/joho/godotenv"
)
type Config struct {
TwitchUsername string
TwitchOauth string
Environment string
}
func New() *Config {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
twitchUsername := os.Getenv("TWITCH_USERNAME")
twitchOauth := os.Getenv("TWITCH_OAUTH")
environment := "Development"
return &Config{twitchUsername, twitchOauth, environment}
}

View file

@ -1,66 +1,49 @@
package bot
import (
"os"
"github.com/gempir/go-twitch-irc/v3"
"github.com/joho/godotenv"
"github.com/lyx0/nourybot/internal/config"
"github.com/sirupsen/logrus"
)
type config struct {
env string
botUsername string
botOauth string
}
type Bot struct {
config config
twitchClient *twitch.Client
TwitchClient *twitch.Client
logger *logrus.Logger
}
func New() *Bot {
var cfg config
// Initialize a new logger we attach to our application struct.
lgr := logrus.New()
// Load the .env file and check for errors.
err := godotenv.Load()
if err != nil {
lgr.Fatal("Error loading .env file")
}
// Load bot credentials from the .env file.
cfg.botUsername = os.Getenv("BOT_USER")
cfg.botOauth = os.Getenv("BOT_OAUTH")
cfg := config.New()
// Initialize a new twitch client which we attach to our
// application struct.
twitchClient := twitch.NewClient(cfg.botUsername, cfg.botOauth)
logrus.Info(cfg.TwitchUsername, cfg.TwitchOauth)
twitchClient := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth)
bot := &Bot{
config: cfg,
twitchClient: twitchClient,
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.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.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!", bot.config.env)
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

@ -95,7 +95,7 @@ func (bot *Bot) Send(target, message string) {
messageBanned, banReason := bot.checkMessage(message)
if messageBanned {
// Bad message, replace message and log it.
bot.twitchClient.Say(target, "[BANPHRASED] monkaS")
bot.TwitchClient.Say(target, "[BANPHRASED] monkaS")
bot.logger.Info("Banned message detected: ", banReason)
return
@ -107,13 +107,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)
bot.TwitchClient.Say(target, firstMessage)
bot.TwitchClient.Say(target, secondMessage)
return
}
// Message was fine.
bot.twitchClient.Say(target, message)
bot.TwitchClient.Say(target, message)
return
}
}