2023-05-06 21:37:21 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/lyx0/nourybot-matrix/internal/commands"
|
2023-05-06 23:38:07 +02:00
|
|
|
"github.com/lyx0/nourybot-matrix/internal/common"
|
2023-05-06 21:37:21 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (app *Application) ParseCommand(evt *event.Event) {
|
2023-05-06 23:38:07 +02:00
|
|
|
common.CommandUsed()
|
2023-05-06 21:56:27 +02:00
|
|
|
// commandName is the actual name of the command without the prefix.
|
|
|
|
// e.g. `!ping` would be `ping`.
|
2023-05-06 21:37:21 +02:00
|
|
|
commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:])
|
2023-05-06 21:56:27 +02:00
|
|
|
|
|
|
|
// cmdParams are additional command parameters.
|
|
|
|
// e.g. `!weather san antonio`
|
|
|
|
// cmdParam[0] is `san` and cmdParam[1] = `antonio`.
|
|
|
|
cmdParams := strings.SplitN(evt.Content.AsMessage().Body, " ", 500)
|
|
|
|
app.Log.Info().Msgf("cmdParams: %s", cmdParams)
|
|
|
|
|
|
|
|
// msgLen is the amount of words in a message without the prefix.
|
|
|
|
// Useful to check if enough cmdParams are provided.
|
|
|
|
msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -2))
|
|
|
|
|
2023-05-06 21:37:21 +02:00
|
|
|
app.Log.Info().Msgf("Command: %s", commandName)
|
|
|
|
|
|
|
|
switch commandName {
|
|
|
|
case "xd":
|
|
|
|
app.SendText(evt, "XD !")
|
|
|
|
return
|
|
|
|
|
2023-05-06 22:52:45 +02:00
|
|
|
case "currency":
|
|
|
|
if msgLen < 4 {
|
|
|
|
app.SendText(evt, "Not enough arguments provided")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if resp, err := commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4]); err != nil {
|
2023-05-06 22:56:00 +02:00
|
|
|
app.Log.Error().Err(err).Msg("failed to handle currency command")
|
2023-05-06 22:52:45 +02:00
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 23:51:50 +02:00
|
|
|
case "lastfm":
|
|
|
|
if msgLen == 1 {
|
|
|
|
app.SendText(evt, "Not enough arguments provided")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if resp, err := commands.LastFmUserRecent(cmdParams[1]); err != nil {
|
|
|
|
app.Log.Error().Err(err).Msg("failed to handle lastfm command")
|
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 23:23:23 +02:00
|
|
|
case "phonetic":
|
2023-05-07 00:30:24 +02:00
|
|
|
if msgLen == 1 {
|
2023-05-07 00:45:31 +02:00
|
|
|
if resp, err := commands.Help(commandName); err != nil {
|
|
|
|
app.Log.Error().Err(err).Msg("failed to handle help->phonetic command")
|
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
2023-05-07 00:30:24 +02:00
|
|
|
} else {
|
|
|
|
msg := evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)]
|
|
|
|
if resp, err := commands.Phonetic(msg); err != nil {
|
|
|
|
app.Log.Error().Err(err).Msg("failed to handle phonetic command")
|
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
2023-05-06 23:23:23 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 21:37:21 +02:00
|
|
|
case "ping":
|
|
|
|
if resp, err := commands.Ping(); err != nil {
|
2023-05-06 22:56:00 +02:00
|
|
|
app.Log.Error().Err(err).Msg("failed to handle ping command")
|
2023-05-06 21:37:21 +02:00
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:56:27 +02:00
|
|
|
case "random":
|
|
|
|
if msgLen == 2 && cmdParams[1] == "xkcd" {
|
|
|
|
if resp, err := commands.RandomXkcd(); err != nil {
|
2023-05-06 22:56:00 +02:00
|
|
|
app.Log.Error().Err(err).Msg("failed to handle random->xkcd command")
|
2023-05-06 21:56:27 +02:00
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:37:21 +02:00
|
|
|
case "xkcd":
|
2023-05-06 21:56:27 +02:00
|
|
|
if msgLen == 2 && cmdParams[1] == "random" {
|
|
|
|
if resp, err := commands.RandomXkcd(); err != nil {
|
2023-05-06 22:56:00 +02:00
|
|
|
app.Log.Error().Err(err).Msg("failed to handle xkcd->randomXkcd command")
|
2023-05-06 21:56:27 +02:00
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if msgLen == 1 {
|
|
|
|
if resp, err := commands.Xkcd(); err != nil {
|
2023-05-06 22:56:00 +02:00
|
|
|
app.Log.Error().Err(err).Msg("failed to handle xkcd command")
|
2023-05-06 21:56:27 +02:00
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
2023-05-06 21:37:21 +02:00
|
|
|
}
|
2023-05-06 22:30:53 +02:00
|
|
|
|
|
|
|
case "weather":
|
|
|
|
if msgLen < 2 {
|
|
|
|
app.SendText(evt, "Not enough arguments provided")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
location := evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)]
|
|
|
|
if resp, err := commands.Weather(location); err != nil {
|
|
|
|
app.Log.Error().Err(err).Msg("Failed to handle Weather command")
|
|
|
|
app.SendText(evt, "Something went wrong.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.SendText(evt, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-05-06 21:37:21 +02:00
|
|
|
}
|
|
|
|
}
|