mirror of
https://github.com/lyx0/nourybot-matrix.git
synced 2024-11-13 19:49:54 +01:00
add currency/preview/weather command
This commit is contained in:
parent
96de26a544
commit
9927aa2724
6 changed files with 122 additions and 14 deletions
|
@ -3,7 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/lyx0/nourybot-matrix/pkg/owm"
|
"github.com/lyx0/nourybot-matrix/pkg/commands"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func (app *application) ParseCommand(evt *event.Event) {
|
||||||
|
|
||||||
// msgLen is the amount of words in a message without the prefix.
|
// msgLen is the amount of words in a message without the prefix.
|
||||||
// Useful to check if enough cmdParams are provided.
|
// Useful to check if enough cmdParams are provided.
|
||||||
//msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -2))
|
msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -1))
|
||||||
|
|
||||||
app.Log.Info().Msgf("Command: %s", evt.Content.AsMessage().Body)
|
app.Log.Info().Msgf("Command: %s", evt.Content.AsMessage().Body)
|
||||||
|
|
||||||
|
@ -30,16 +30,42 @@ func (app *application) ParseCommand(evt *event.Event) {
|
||||||
case "xd":
|
case "xd":
|
||||||
app.SendText(evt, "xd !")
|
app.SendText(evt, "xd !")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
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 "gofile":
|
case "gofile":
|
||||||
app.NewDownload("gofile", evt, cmdParams[1])
|
if msgLen < 2 {
|
||||||
return
|
reply = "Not enough arguments provided. Usage: !gofile [link]"
|
||||||
|
} else {
|
||||||
|
app.NewDownload("gofile", evt, cmdParams[1])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
case "preview":
|
||||||
|
if msgLen < 2 {
|
||||||
|
reply = "Not enough arguments provided. Usage: !preview [stream]"
|
||||||
|
} else {
|
||||||
|
reply = commands.Preview(cmdParams[1])
|
||||||
|
}
|
||||||
|
|
||||||
case "weather":
|
case "weather":
|
||||||
reply, _ = owm.Weather(evt.Content.AsMessage().Body[9:len(evt.Content.AsMessage().Body)])
|
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)])
|
||||||
|
}
|
||||||
|
|
||||||
case "yaf":
|
case "yaf":
|
||||||
app.NewDownload("yaf", evt, cmdParams[1])
|
if msgLen < 2 {
|
||||||
return
|
reply = "Not enough arguments provided. Usage: !gofile [link]"
|
||||||
|
} else {
|
||||||
|
app.NewDownload("yaf", evt, cmdParams[1])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if reply != "" {
|
if reply != "" {
|
||||||
|
|
30
pkg/commands/currency.go
Normal file
30
pkg/commands/currency.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Currency(currAmount, currFrom, currTo string) (string, error) {
|
||||||
|
basePath := "https://decapi.me/misc/currency/"
|
||||||
|
from := fmt.Sprintf("?from=%s", currFrom)
|
||||||
|
to := fmt.Sprintf("&to=%s", currTo)
|
||||||
|
value := fmt.Sprintf("&value=%s", currAmount)
|
||||||
|
|
||||||
|
// https://decapi.me/misc/currency/?from=usd&to=usd&value=10
|
||||||
|
resp, err := http.Get(fmt.Sprint(basePath + from + to + value))
|
||||||
|
if err != nil {
|
||||||
|
return "", ErrInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", ErrInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
reply := string(body)
|
||||||
|
return reply, nil
|
||||||
|
}
|
8
pkg/commands/errors.go
Normal file
8
pkg/commands/errors.go
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInternalServerError = errors.New("internal server error")
|
||||||
|
ErrWeatherLocationNotFound = errors.New("location not found")
|
||||||
|
)
|
15
pkg/commands/preview.go
Normal file
15
pkg/commands/preview.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/lyx0/nourybot-matrix/pkg/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Preview(channel string) string {
|
||||||
|
imageHeight := common.GenerateRandomNumberRange(1040, 1080)
|
||||||
|
imageWidth := common.GenerateRandomNumberRange(1890, 1920)
|
||||||
|
|
||||||
|
reply := fmt.Sprintf("https://static-cdn.jtvnw.net/previews-ttv/live_user_%s-%vx%v.jpg", channel, imageWidth, imageHeight)
|
||||||
|
return reply
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package owm
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -9,11 +8,6 @@ import (
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
ErrInternalServerError = errors.New("internal server error")
|
|
||||||
ErrWeatherLocationNotFound = errors.New("location not found")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Weather queries the OpenWeatherMap Api for the given location and sends the
|
// Weather queries the OpenWeatherMap Api for the given location and sends the
|
||||||
// current weather response to the target twitch chat.
|
// current weather response to the target twitch chat.
|
||||||
func Weather(location string) (string, error) {
|
func Weather(location string) (string, error) {
|
35
pkg/common/random.go
Normal file
35
pkg/common/random.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StrGenerateRandomNumber generates a random number from
|
||||||
|
// a given max value as a string
|
||||||
|
func StrGenerateRandomNumber(max string) int {
|
||||||
|
num, err := strconv.Atoi(max)
|
||||||
|
if num < 1 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Supplied value %v is not a number", num)
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
|
return rand.Intn(num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateRandomNumber returns a random number from
|
||||||
|
// a given max value as a int
|
||||||
|
func GenerateRandomNumber(max int) int {
|
||||||
|
return rand.Intn(max)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateRandomNumberRange returns a random number
|
||||||
|
// over a given minimum and maximum range.
|
||||||
|
func GenerateRandomNumberRange(min int, max int) int {
|
||||||
|
return (rand.Intn(max-min) + min)
|
||||||
|
}
|
Loading…
Reference in a new issue