Add unfinished command handler system based on the improved bot support proposal

This commit is contained in:
Tulir Asokan 2018-06-19 01:25:47 +03:00
parent 946dac989a
commit 307a32e0c0
9 changed files with 355 additions and 16 deletions

View file

@ -1,4 +1,4 @@
// jesaribot - A simple maubot plugin.
// maubot - A plugin-based Matrix bot system written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
@ -17,6 +17,7 @@
package database
import (
"maubot.xyz"
log "maunium.net/go/maulogger"
"database/sql"
)
@ -35,6 +36,8 @@ type MatrixClient struct {
AutoJoinRooms bool `json:"auto_join_rooms"`
DisplayName string `json:"display_name"`
AvatarURL string `json:"avatar_url"`
Commands map[string]*CommandSpec `json:"commandspecs"`
}
type MatrixClientStatic struct {
@ -85,15 +88,32 @@ func (mcs *MatrixClientStatic) New() *MatrixClient {
}
}
type Scannable interface {
Scan(...interface{}) error
}
func (mxc *MatrixClient) Scan(row Scannable) *MatrixClient {
err := row.Scan(&mxc.UserID, &mxc.Homeserver, &mxc.AccessToken, &mxc.NextBatch, &mxc.FilterID, &mxc.Sync, &mxc.AutoJoinRooms, &mxc.DisplayName, &mxc.AvatarURL)
if err != nil {
log.Fatalln("Database scan failed:", err)
}
mxc.LoadCommandSpecs()
return mxc
}
func (mxc *MatrixClient) SetCommandSpec(owner string, newSpec *maubot.CommandSpec) bool {
spec := mxc.db.CommandSpec.GetOrCreate(owner, mxc.UserID)
if newSpec.Equals(spec.CommandSpec) {
return false
}
spec.CommandSpec = newSpec
spec.Update()
mxc.Commands[owner] = spec
return true
}
func (mxc *MatrixClient) LoadCommandSpecs() *MatrixClient {
specs := mxc.db.CommandSpec.GetAllByClient(mxc.UserID)
mxc.Commands = make(map[string]*CommandSpec)
for _, spec := range specs {
mxc.Commands[spec.Owner] = spec
}
return mxc
}