mirror of
https://github.com/Luzifer/lounge-control.git
synced 2024-12-22 14:31:17 +00:00
Add send command
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
20565e033b
commit
c211f5666c
2 changed files with 68 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
.env
|
||||
lounge-control
|
||||
|
|
67
cmd_send.go
Normal file
67
cmd_send.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Luzifer/lounge-control/sioclient"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerCommand("send", commandSend)
|
||||
}
|
||||
|
||||
func commandSend(args []string) handlerFunc {
|
||||
if len(args) != 2 {
|
||||
log.Fatal("Usage: send <target> <message>")
|
||||
}
|
||||
|
||||
var (
|
||||
channelName = args[0]
|
||||
message = args[1]
|
||||
)
|
||||
|
||||
return addGenericHandler(func(pType string, msg *sioclient.Message) error {
|
||||
if pType != "init" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// After join command is finished we can execute the joins
|
||||
network := initData.NetworkByNameOrUUID(cfg.Network)
|
||||
if network == nil {
|
||||
return errors.New("Network not found")
|
||||
}
|
||||
|
||||
var target *channel
|
||||
for _, c := range network.Channels {
|
||||
if channelName == "lobby" && c.Type == "lobby" {
|
||||
target = &c
|
||||
break
|
||||
} else if channelName == c.Name {
|
||||
target = &c
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if target == nil {
|
||||
return errors.New("Unable to find channel in network")
|
||||
}
|
||||
|
||||
msg, err := sioclient.NewMessage(sioclient.MessageTypeEvent, 0, "input", map[string]interface{}{
|
||||
"text": message,
|
||||
"target": target.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Unable to compose message")
|
||||
}
|
||||
|
||||
if err = msg.Send(client); err != nil {
|
||||
return errors.Wrap(err, "Unable to send message")
|
||||
}
|
||||
|
||||
interrupt <- os.Interrupt
|
||||
return nil
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue