1
0
Fork 0
mirror of https://github.com/Luzifer/lounge-control.git synced 2024-11-14 13:52:43 +00:00
lounge-control/cmd_part.go

68 lines
1.3 KiB
Go
Raw Normal View History

2020-05-09 21:17:15 +00:00
package main
import (
"fmt"
"os"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/lounge-control/sioclient"
2020-05-09 21:17:15 +00:00
)
func init() {
registerCommand("part", commandPart)
}
func commandPart(args []string) handlerFunc {
if len(args) == 0 {
log.Fatal("No channels given to part")
}
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 lobby *channel
for _, c := range network.Channels {
if c.Type == "lobby" {
lobby = &c
break
}
}
if lobby == nil {
return errors.New("Unable to find lobby for network")
}
for _, ch := range args {
if !strings.HasPrefix(ch, "#") {
ch = "#" + ch
}
msg, err := sioclient.NewMessage(sioclient.MessageTypeEvent, 0, "input", map[string]interface{}{
"text": fmt.Sprintf("/part %s", ch),
"target": lobby.ID,
})
if err != nil {
return errors.Wrap(err, "Unable to compose part message")
}
if err = msg.Send(client); err != nil {
return errors.Wrap(err, "Unable to send part message")
}
}
interrupt <- os.Interrupt
return nil
})
}