Add chatcommand argument parsing
This commit is contained in:
parent
25512974d2
commit
85ab2a963b
25
matrix.go
25
matrix.go
@ -3,15 +3,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
"strings"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/id"
|
|
||||||
"maunium.net/go/mautrix/format"
|
"maunium.net/go/mautrix/format"
|
||||||
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MatrixMessage stores information about a matrix message.
|
// MatrixMessage stores information about a matrix message.
|
||||||
@ -19,10 +19,13 @@ type MatrixMessage struct {
|
|||||||
text string
|
text string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MatrixCommandCallback is a function that gets called when the bot is issued a command over Matrix
|
||||||
|
type MatrixCommandCallback func(ctx context.Context, evt *event.Event, args []string) string
|
||||||
|
|
||||||
// MatrixCommand stores information about a matrix chat command
|
// MatrixCommand stores information about a matrix chat command
|
||||||
type MatrixCommand struct {
|
type MatrixCommand struct {
|
||||||
cmd string
|
cmd string
|
||||||
callback func(ctx context.Context, evt *event.Event) string
|
callback MatrixCommandCallback
|
||||||
description string
|
description string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +41,7 @@ func sendMessage(msg MatrixMessage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// registerChatCommand
|
// registerChatCommand
|
||||||
func registerChatCommand(cmd string, description string, callback func(ctx context.Context, evt *event.Event) string) {
|
func registerChatCommand(cmd string, description string, callback MatrixCommandCallback) {
|
||||||
Chatcommands = append(Chatcommands, MatrixCommand{
|
Chatcommands = append(Chatcommands, MatrixCommand{
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
callback: callback,
|
callback: callback,
|
||||||
@ -84,11 +87,13 @@ func initMatrix() {
|
|||||||
prefixes := []string{"!", configuration.matrix.username + " "}
|
prefixes := []string{"!", configuration.matrix.username + " "}
|
||||||
for _, prefix := range prefixes {
|
for _, prefix := range prefixes {
|
||||||
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
|
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
|
||||||
|
msg := evt.Content.AsMessage().Body
|
||||||
|
cmd := strings.TrimPrefix(strings.Split(msg, " ")[0], prefix)
|
||||||
found_command := false
|
found_command := false
|
||||||
for _, command := range Chatcommands {
|
for _, command := range Chatcommands {
|
||||||
if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, prefix) {
|
if command.cmd == cmd {
|
||||||
found_command = true
|
found_command = true
|
||||||
out := command.callback(ctx, evt)
|
out := command.callback(ctx, evt, strings.Split(strings.TrimPrefix(msg, prefix+cmd), " "))
|
||||||
content := format.RenderMarkdown(out, true, false)
|
content := format.RenderMarkdown(out, true, false)
|
||||||
content.SetReply(evt)
|
content.SetReply(evt)
|
||||||
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
||||||
@ -103,7 +108,7 @@ func initMatrix() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event) string {
|
registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
text := ""
|
text := ""
|
||||||
for _, cmd := range Chatcommands {
|
for _, cmd := range Chatcommands {
|
||||||
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
||||||
@ -111,7 +116,7 @@ func initMatrix() {
|
|||||||
return text
|
return text
|
||||||
})
|
})
|
||||||
|
|
||||||
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event) string {
|
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
text := "⚪️ Getting sites...\n"
|
text := "⚪️ Getting sites...\n"
|
||||||
sites, err := getAllSites()
|
sites, err := getAllSites()
|
||||||
if err.code != 0 {
|
if err.code != 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user