mirror-nourybot-matrix/cmd/bot/commands.go

104 lines
2.7 KiB
Go
Raw Normal View History

2024-01-10 18:10:28 +01:00
package main
import (
"strings"
2024-01-11 02:33:25 +01:00
"github.com/lyx0/nourybot-matrix/pkg/commands"
2024-01-10 18:10:28 +01:00
"maunium.net/go/mautrix/event"
)
2024-01-10 18:32:29 +01:00
func (app *application) ParseCommand(evt *event.Event) {
2024-01-11 02:08:42 +01:00
var reply string
2024-01-10 18:10:28 +01:00
// commandName is the actual name of the command without the prefix.
// e.g. `!ping` would be `ping`.
commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:])
2024-01-10 18:10:28 +01: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.
2024-01-11 02:33:25 +01:00
msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -1))
2024-01-10 18:10:28 +01:00
app.Log.Info().Msgf("Command: %s", evt.Content.AsMessage().Body)
//message := evt.Content.AsMessage().Body
switch commandName {
case "xd":
2024-01-10 18:10:28 +01:00
app.SendText(evt, "xd !")
return
2024-01-11 02:33:25 +01:00
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])
}
2024-01-10 21:05:06 +01:00
case "gofile":
2024-01-11 02:33:25 +01:00
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: !gofile [link]"
} else {
app.NewDownload("gofile", evt, cmdParams[1])
return
}
2024-01-11 02:52:29 +01:00
case "lastfm":
if msgLen == 1 {
reply = "Not enough arguments provided. Usage: ()lastfm <username>"
} else {
reply = commands.LastFmUserRecent(cmdParams[1], app.Log)
}
case "phonetic":
if msgLen == 1 {
reply = "Not enough arguments provided. Usage: ()phonetic <text to translate>"
} else {
reply, _ = commands.Phonetic(evt.Content.AsMessage().Body[10:len(evt.Content.AsMessage().Body)])
}
2024-01-11 02:33:25 +01:00
case "preview":
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: !preview [stream]"
} else {
reply = commands.Preview(cmdParams[1])
}
2024-01-11 02:08:42 +01:00
case "weather":
2024-01-11 02:33:25 +01:00
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: !weather [location]"
} else {
reply, _ = commands.Weather(evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)])
}
2024-01-11 02:08:42 +01:00
case "yaf":
2024-01-11 02:33:25 +01:00
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: !gofile [link]"
} else {
app.NewDownload("yaf", evt, cmdParams[1])
return
}
2024-01-11 02:08:42 +01:00
2024-01-15 22:06:57 +01:00
case "conv":
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: !gofile [link]"
} else {
app.ConvertToMP4(evt, cmdParams[1])
return
}
2024-01-11 02:44:05 +01:00
case "wa":
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: !wa [query]"
} else {
reply = commands.WolframAlphaQuery(evt.Content.AsMessage().Body[4:len(evt.Content.AsMessage().Body)])
}
2024-01-11 02:08:42 +01:00
}
if reply != "" {
app.SendText(evt, reply)
2024-01-10 18:10:28 +01:00
}
}