2023-08-08 02:37:37 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v4"
|
|
|
|
"github.com/lyx0/nourybot/internal/commands"
|
|
|
|
"github.com/lyx0/nourybot/internal/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
// handleCommand takes in a twitch.PrivateMessage and then routes the message to
|
|
|
|
// the function that is responsible for each command and knows how to deal with it accordingly.
|
|
|
|
func (app *application) handleCommand(message twitch.PrivateMessage) {
|
2023-09-07 20:12:09 +02:00
|
|
|
var reply string
|
2023-08-08 02:37:37 +02:00
|
|
|
|
|
|
|
// Increments the counter how many commands have been used, called in the ping command.
|
|
|
|
common.CommandUsed()
|
|
|
|
|
|
|
|
// commandName is the actual name of the command without the prefix.
|
|
|
|
// e.g. `()ping` would be `ping`.
|
|
|
|
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
|
|
|
|
|
|
|
|
// cmdParams are additional command parameters.
|
|
|
|
// e.g. `()weather san antonio`
|
|
|
|
// cmdParam[0] is `san` and cmdParam[1] = `antonio`.
|
|
|
|
//
|
|
|
|
// Since Twitch messages are at most 500 characters I use a
|
|
|
|
// maximum count of 500+10 just to be safe.
|
|
|
|
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
|
|
|
|
cmdParams := strings.SplitN(message.Message, " ", 500)
|
|
|
|
|
|
|
|
// msgLen is the amount of words in a message without the prefix.
|
|
|
|
// Useful to check if enough cmdParams are provided.
|
|
|
|
msgLen := len(strings.SplitN(message.Message, " ", -2))
|
|
|
|
|
2023-09-19 14:04:58 +02:00
|
|
|
userLevel := app.GetUserLevel(message.User.ID)
|
2023-08-08 02:37:37 +02:00
|
|
|
// target is the channelname the message originated from and
|
|
|
|
// where the TwitchClient should send the response
|
|
|
|
target := message.Channel
|
|
|
|
app.Log.Infow("Command received",
|
|
|
|
// "message", message, // Pretty taxing
|
|
|
|
"message.Message", message.Message,
|
|
|
|
"message.Channel", target,
|
|
|
|
"commandName", commandName,
|
|
|
|
"cmdParams", cmdParams,
|
|
|
|
"msgLen", msgLen,
|
2023-09-19 14:04:58 +02:00
|
|
|
"userLevel", userLevel,
|
2023-08-08 02:37:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// A `commandName` is every message starting with `()`.
|
|
|
|
// Hardcoded commands have a priority over database commands.
|
|
|
|
// Switch over the commandName and see if there is a hardcoded case for it.
|
|
|
|
// If there was no switch case satisfied, query the database if there is
|
|
|
|
// a data.CommandModel.Name equal to the `commandName`
|
|
|
|
// If there is return the data.CommandModel.Text entry.
|
|
|
|
// Otherwise we ignore the message.
|
|
|
|
switch commandName {
|
|
|
|
case "":
|
|
|
|
if msgLen == 1 {
|
2023-09-07 20:12:09 +02:00
|
|
|
reply = "xd"
|
2023-08-08 02:37:37 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 20:43:16 +02:00
|
|
|
case "bttv":
|
|
|
|
if msgLen < 2 {
|
|
|
|
reply = "Not enough arguments provided. Usage: ()bttv <emote name>"
|
|
|
|
} else {
|
|
|
|
reply = commands.Bttv(cmdParams[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Coinflip
|
|
|
|
case "coin":
|
|
|
|
reply = commands.Coinflip()
|
|
|
|
case "coinflip":
|
|
|
|
reply = commands.Coinflip()
|
|
|
|
case "cf":
|
|
|
|
reply = commands.Coinflip()
|
|
|
|
|
2023-09-07 21:09:33 +02:00
|
|
|
// ()currency <amount> <input currency> to <output currency>
|
|
|
|
case "currency":
|
|
|
|
if msgLen < 4 {
|
|
|
|
reply = "Not enough arguments provided. Usage: ()currency 10 USD to EUR"
|
|
|
|
} else {
|
|
|
|
reply, _ = commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4])
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:47:54 +02:00
|
|
|
case "cb":
|
|
|
|
go commands.DownloadCatbox(target, cmdParams[1], app.TwitchClient, app.Log)
|
|
|
|
|
2023-09-27 16:44:42 +02:00
|
|
|
case "dl":
|
2023-10-03 19:33:31 +02:00
|
|
|
go commands.Download(target, cmdParams[1], fileUploaderURL, app.TwitchClient, app.Log)
|
2023-09-27 16:44:42 +02:00
|
|
|
|
2023-09-08 02:26:00 +02:00
|
|
|
case "mail":
|
2023-09-08 03:03:57 +02:00
|
|
|
app.SendEmail("Test command used!", "This is an email test")
|
2023-09-08 02:26:00 +02:00
|
|
|
|
2023-09-08 01:36:41 +02:00
|
|
|
case "lastfm":
|
|
|
|
if msgLen == 1 {
|
|
|
|
reply = app.UserCheckLastFM(message)
|
|
|
|
} else {
|
|
|
|
// Default to first argument supplied being the name
|
|
|
|
// of the user to look up recently played.
|
|
|
|
reply = commands.LastFmUserRecent(target, cmdParams[1])
|
|
|
|
}
|
|
|
|
|
2023-08-08 02:37:37 +02:00
|
|
|
case "nourybot":
|
2023-09-07 20:12:09 +02:00
|
|
|
reply = "Lidl Twitch bot made by @nourylul. Prefix: ()"
|
2023-08-08 02:37:37 +02:00
|
|
|
|
2023-09-07 21:09:33 +02:00
|
|
|
case "phonetic":
|
|
|
|
if msgLen == 1 {
|
|
|
|
reply = "Not enough arguments provided. Usage: ()phonetic <text to translate>"
|
|
|
|
} else {
|
|
|
|
reply, _ = commands.Phonetic(message.Message[11:len(message.Message)])
|
|
|
|
}
|
2023-08-08 02:37:37 +02:00
|
|
|
case "ping":
|
2023-09-07 20:12:09 +02:00
|
|
|
reply = commands.Ping()
|
2023-08-08 02:37:37 +02:00
|
|
|
// ()bttv <emote name>
|
|
|
|
|
2023-09-07 20:43:16 +02:00
|
|
|
// ()weather <location>
|
|
|
|
case "weather":
|
|
|
|
if msgLen == 1 {
|
2023-09-08 01:36:41 +02:00
|
|
|
app.UserCheckWeather(message)
|
2023-09-07 20:43:16 +02:00
|
|
|
} else if msgLen < 2 {
|
|
|
|
reply = "Not enough arguments provided."
|
|
|
|
} else {
|
|
|
|
reply, _ = commands.Weather(message.Message[10:len(message.Message)])
|
|
|
|
}
|
|
|
|
|
2023-09-07 21:09:33 +02:00
|
|
|
// Xkcd
|
|
|
|
// Random Xkcd
|
|
|
|
case "rxkcd":
|
|
|
|
reply, _ = commands.RandomXkcd()
|
|
|
|
case "randomxkcd":
|
|
|
|
reply, _ = commands.RandomXkcd()
|
|
|
|
// Latest Xkcd
|
|
|
|
case "xkcd":
|
|
|
|
reply, _ = commands.Xkcd()
|
|
|
|
|
2023-09-07 22:34:53 +02:00
|
|
|
case "timer":
|
|
|
|
switch cmdParams[1] {
|
|
|
|
case "add":
|
|
|
|
app.AddTimer(cmdParams[2], cmdParams[3], message)
|
|
|
|
case "edit":
|
|
|
|
app.EditTimer(cmdParams[2], cmdParams[3], message)
|
|
|
|
case "delete":
|
|
|
|
app.DeleteTimer(cmdParams[2], message)
|
2023-10-03 22:58:03 +02:00
|
|
|
case "list":
|
|
|
|
reply = app.ListTimers()
|
2023-09-07 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
2023-09-08 03:03:57 +02:00
|
|
|
case "debug":
|
|
|
|
switch cmdParams[1] {
|
|
|
|
case "user":
|
2023-09-19 14:04:58 +02:00
|
|
|
if userLevel >= 250 {
|
|
|
|
app.DebugUser(cmdParams[2], message)
|
|
|
|
}
|
2023-09-14 17:40:35 +02:00
|
|
|
case "command":
|
2023-09-19 14:04:58 +02:00
|
|
|
if userLevel >= 250 {
|
|
|
|
app.DebugCommand(cmdParams[2], message)
|
|
|
|
}
|
2023-09-08 03:03:57 +02:00
|
|
|
}
|
|
|
|
|
2023-09-08 00:18:09 +02:00
|
|
|
case "command":
|
|
|
|
switch cmdParams[1] {
|
|
|
|
case "add":
|
|
|
|
app.AddCommand(cmdParams[2], message)
|
|
|
|
case "delete":
|
|
|
|
app.DeleteCommand(cmdParams[2], message)
|
|
|
|
case "edit":
|
|
|
|
switch cmdParams[2] {
|
|
|
|
case "level":
|
|
|
|
app.EditCommandLevel(cmdParams[3], cmdParams[4], message)
|
|
|
|
case "category":
|
|
|
|
app.EditCommandCategory(cmdParams[3], cmdParams[4], message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 01:36:41 +02:00
|
|
|
case "set":
|
|
|
|
switch cmdParams[1] {
|
|
|
|
case "lastfm":
|
|
|
|
app.SetUserLastFM(cmdParams[2], message)
|
|
|
|
case "location":
|
|
|
|
app.SetUserLocation(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "user":
|
|
|
|
switch cmdParams[1] {
|
|
|
|
case "edit":
|
|
|
|
switch cmdParams[2] {
|
|
|
|
case "level":
|
|
|
|
app.EditUserLevel(cmdParams[3], cmdParams[4], message)
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 02:37:37 +02:00
|
|
|
}
|
2023-09-07 20:12:09 +02:00
|
|
|
if reply != "" {
|
2023-10-03 19:33:31 +02:00
|
|
|
go app.Send(target, reply)
|
2023-09-07 20:12:09 +02:00
|
|
|
return
|
|
|
|
}
|
2023-08-08 02:37:37 +02:00
|
|
|
}
|