Add chatcommand argument parsing
This commit is contained in:
parent
25512974d2
commit
85ab2a963b
51
matrix.go
51
matrix.go
@ -3,15 +3,15 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
"strings"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
"maunium.net/go/mautrix/format"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// MatrixMessage stores information about a matrix message.
|
||||
@ -19,10 +19,13 @@ type MatrixMessage struct {
|
||||
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
|
||||
type MatrixCommand struct {
|
||||
cmd string
|
||||
callback func(ctx context.Context, evt *event.Event) string
|
||||
cmd string
|
||||
callback MatrixCommandCallback
|
||||
description string
|
||||
}
|
||||
|
||||
@ -38,10 +41,10 @@ func sendMessage(msg MatrixMessage) {
|
||||
}
|
||||
|
||||
// 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{
|
||||
cmd: cmd,
|
||||
callback: callback,
|
||||
cmd: cmd,
|
||||
callback: callback,
|
||||
description: description,
|
||||
})
|
||||
}
|
||||
@ -53,9 +56,9 @@ func initMatrix() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
resp, err := client.Login(context.Background(), &mautrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
||||
Password: configuration.matrix.password,
|
||||
Type: "m.login.password",
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
||||
Password: configuration.matrix.password,
|
||||
StoreCredentials: true,
|
||||
})
|
||||
if err != nil {
|
||||
@ -78,17 +81,19 @@ func initMatrix() {
|
||||
log.Printf("[Matrix] Rejected invite to room '%s' (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
||||
if evt.RoomID.String() == configuration.matrix.room_id {
|
||||
prefixes := []string{"!", configuration.matrix.username+" "}
|
||||
for _,prefix := range prefixes {
|
||||
prefixes := []string{"!", configuration.matrix.username + " "}
|
||||
for _, prefix := range prefixes {
|
||||
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
|
||||
msg := evt.Content.AsMessage().Body
|
||||
cmd := strings.TrimPrefix(strings.Split(msg, " ")[0], prefix)
|
||||
found_command := false
|
||||
for _, command := range Chatcommands {
|
||||
if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, prefix) {
|
||||
if command.cmd == cmd {
|
||||
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.SetReply(evt)
|
||||
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
||||
@ -103,30 +108,30 @@ 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 := ""
|
||||
for _,cmd := range Chatcommands {
|
||||
for _, cmd := range Chatcommands {
|
||||
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
||||
}
|
||||
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"
|
||||
sites, err := getAllSites()
|
||||
if err.code != 0 {
|
||||
return text+"🔴 Failed to get sites!"
|
||||
return text + "🔴 Failed to get sites!"
|
||||
}
|
||||
for _, site := range sites {
|
||||
text = fmt.Sprintf("%s- %s\n", text, site.name)
|
||||
}
|
||||
if len(sites) == 0 {
|
||||
text = text+"*No sites found*"
|
||||
text = text + "*No sites found*"
|
||||
}
|
||||
return text
|
||||
})
|
||||
|
||||
go func () {
|
||||
go func() {
|
||||
for range time.Tick(time.Second * 6) {
|
||||
log.Printf("Scanning for queued messages...")
|
||||
for _, msg := range MatrixOut {
|
||||
|
Loading…
Reference in New Issue
Block a user