query the database on a command and send it if it exists

This commit is contained in:
lyx0 2022-08-11 01:49:49 +02:00
parent 4a4e34a929
commit 350283018e
4 changed files with 68 additions and 1 deletions

View file

@ -290,5 +290,12 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
case "xkcd": case "xkcd":
commands.Xkcd(target, app.TwitchClient) commands.Xkcd(target, app.TwitchClient)
return 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
} }
} }

View file

@ -15,7 +15,7 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) {
// prefixLength |name| text // prefixLength |name| text
// 0123456789012|4567| // 0123456789012|4567|
// e.g. ()addcommand dank FeelsDankMan // e.g. ()addcommand dank FeelsDankMan
// | snip ^ | // | part1 snip ^ part2 |
text := message.Message[prefixLength+len(name) : len(message.Message)] text := message.Message[prefixLength+len(name) : len(message.Message)]
err := app.Models.Commands.Insert(name, text) err := app.Models.Commands.Insert(name, text)
@ -34,3 +34,14 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) {
return 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
}

View file

@ -4,3 +4,7 @@ CREATE TABLE IF NOT EXISTS commands (
text text NOT NULL, text text NOT NULL,
permission integer 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);

View file

@ -133,3 +133,48 @@ func Send(target, message string, tc *twitch.Client) {
return 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
}
}