diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 59c1567..8ca32a8 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -290,5 +290,12 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { case "xkcd": commands.Xkcd(target, app.TwitchClient) return + default: // Check if the command exists in the database, if it doesnt ignore it + reply, err := app.GetCommand(commandName) + if err != nil { + return + } + common.SendNoLimit(message.Channel, reply, app.TwitchClient) + return } } diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 5dc52e1..c86cedc 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -15,7 +15,7 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) { // prefixLength |name| text // 0123456789012|4567| // e.g. ()addcommand dank FeelsDankMan - // | snip ^ | + // | part1 snip ^ part2 | text := message.Message[prefixLength+len(name) : len(message.Message)] err := app.Models.Commands.Insert(name, text) @@ -34,3 +34,14 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) { return } } + +func (app *Application) GetCommand(name string) (string, error) { + command, err := app.Models.Commands.Get(name) + + if err != nil { + return "", err + } + + return command.Text, nil + +} diff --git a/migrations/000003_create_commands_table.up.sql b/migrations/000003_create_commands_table.up.sql index 3cc8684..f16254f 100644 --- a/migrations/000003_create_commands_table.up.sql +++ b/migrations/000003_create_commands_table.up.sql @@ -4,3 +4,7 @@ CREATE TABLE IF NOT EXISTS commands ( text text NOT NULL, permission integer NOT NULL ); + +INSERT INTO commands (name,"text","permission") VALUES + ('repeat','xset r rate 175 50',0), + ('eurkey','setxkbmap -layout eu',0); diff --git a/pkg/common/send.go b/pkg/common/send.go index 48bdb1a..6b72330 100644 --- a/pkg/common/send.go +++ b/pkg/common/send.go @@ -133,3 +133,48 @@ func Send(target, message string, tc *twitch.Client) { return } } + +// SendNoLimit does not check for the maximum message size. +// Used in sending commands from the database since the command has to have +// been gotten in there somehow. So it fits. Still checks for banphrases. +func SendNoLimit(target, message string, tc *twitch.Client) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + // Message we are trying to send is empty. + if len(message) == 0 { + return + } + + // Since messages starting with `.` or `/` are used for special actions + // (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 + // don't allow them either. + if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' { + message = ":tf: " + message + } + + // check the message for bad words before we say it + messageBanned, banReason := checkMessage(message) + if messageBanned { + // Bad message, replace message and log it. + tc.Say(target, "[BANPHRASED] monkaS") + sugar.Infow("banned message detected", + "target channel", target, + "message", message, + "ban reason", banReason, + ) + + return + } else { + // In case the message we are trying to send is longer than the + // maximum allowed message length on twitch we split the message 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 + // TODO: Make it so it splits at a space instead and not in the middle of a word. + // Message was fine. + tc.Say(target, message) + return + } +}