mirror-nourybot/cmd/nourybot/commands.go

187 lines
5 KiB
Go
Raw Normal View History

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))
// 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,
)
// 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])
}
case "mail":
app.SendEmail("Test command used!", "This is an email test")
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()
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)
}
case "debug":
switch cmdParams[1] {
case "user":
app.DebugUser(cmdParams[2], message)
}
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
// Check if the commandName exists as the "name" of a command in the database.
// if it doesnt then ignore it.
// ##################
}
2023-09-07 20:12:09 +02:00
if reply != "" {
app.Send(target, reply)
return
}
2023-08-08 02:37:37 +02:00
}