From 29b6b3c9526288f853804b4f61f1b688983a51ad Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Fri, 12 Aug 2022 00:08:39 +0200 Subject: [PATCH] add comments --- cmd/bot/.env .example | 5 +++++ cmd/bot/command.go | 5 +++-- cmd/bot/commands.go | 13 +++++++++++++ cmd/bot/main.go | 10 ++++++---- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 cmd/bot/.env .example diff --git a/cmd/bot/.env .example b/cmd/bot/.env .example new file mode 100644 index 0000000..72e5fe5 --- /dev/null +++ b/cmd/bot/.env .example @@ -0,0 +1,5 @@ +TWITCH_USERNAME=nourybot +TWITCH_OAUTH=oauth:mycooloauthtoken42069 +TWITCH_COMMAND_PREFIX=() + +DB_DSN=postgres://username:password@localhost/nourybot?sslmode=disable \ No newline at end of file diff --git a/cmd/bot/command.go b/cmd/bot/command.go index d8ccb42..c702497 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -45,11 +45,12 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { // If the level returned is 0 then the user was not found in the database. userLevel := app.GetUserLevel(message.User.Name) + // Left in for debugging purposes. app.Logger.Infow("Command received", - // "message", message, + // "message", message, // Pretty taxing "message.Message", message.Message, "message.Channel", target, - "user level", userLevel, + "userLevel", userLevel, "commandName", commandName, "cmdParams", cmdParams, "msgLen", msgLen, diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 93b6d8d..3d97337 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -8,6 +8,10 @@ import ( "github.com/lyx0/nourybot/pkg/common" ) +// AddCommand takes in a name parameter and a twitch.PrivateMessage. It slices the +// twitch.PrivateMessage after the name parameter and adds everything after to a text +// value. Then it calls the app.Models.Commands.Insert method with both name, and text +// values adding them to the database. func (app *Application) AddCommand(name string, message twitch.PrivateMessage) { // prefixLength is the length of `()addcommand` plus +2 (for the space and zero based) prefixLength := 14 @@ -36,6 +40,12 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) { } } +// GetCommand queries the database for a name. If an entry exists it checks +// if the Command.Level is 0, if it is the command.Text value is returned. +// +// If the Command.Level is not 0 it queries the database for the level of the +// user who sent the message. If the users level is equal or higher +// the command.Text field is returned. func (app *Application) GetCommand(name, username string) (string, error) { // Fetch the command from the database if it exists. command, err := app.Models.Commands.Get(name) @@ -66,6 +76,8 @@ func (app *Application) GetCommand(name, username string) (string, error) { } +// EditCommandLevel takes in a name and level string and updates the entry with name +// to the supplied level value. func (app *Application) EditCommandLevel(name, lvl string, message twitch.PrivateMessage) { level, err := strconv.Atoi(lvl) if err != nil { @@ -87,6 +99,7 @@ func (app *Application) EditCommandLevel(name, lvl string, message twitch.Privat } } +// DeleteCommand takes in a name value and deletes the command from the database if it exists. func (app *Application) DeleteCommand(name string, message twitch.PrivateMessage) { err := app.Models.Commands.Delete(name) if err != nil { diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 45be81e..c9e5abf 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -31,10 +31,10 @@ func main() { // Twitch cfg.twitchUsername = os.Getenv("TWITCH_USERNAME") cfg.twitchOauth = os.Getenv("TWITCH_OAUTH") + cfg.commandPrefix = os.Getenv("TWITCH_COMMAND_PREFIX") tc := twitch.NewClient(cfg.twitchUsername, cfg.twitchOauth) - // Basic configuration - cfg.commandPrefix = "()" + // Will be used someday Copesen cfg.environment = "Development" // Database @@ -43,6 +43,7 @@ func main() { cfg.db.maxIdleConns = 25 cfg.db.maxIdleTime = "15m" + // Establish database connection db, err := openDB(cfg) if err != nil { sugar.Fatal(err) @@ -113,9 +114,10 @@ func main() { // Start time common.StartTime() - common.Send("nourylul", "xd", app.TwitchClient) - // app.GetAllChannels() + // Join the channels in the database. app.InitialJoin() + + common.Send("nourylul", "xd", app.TwitchClient) }) // Actually connect to chat.