Added message handling mechanism for discord module

This commit is contained in:
Alex Vanin 2019-05-10 12:55:52 +03:00
parent bf279581fb
commit bb26c57985
4 changed files with 119 additions and 4 deletions

View file

@ -34,8 +34,8 @@ func start(p appParam) {
if err != nil { if err != nil {
log.Fatal("discord: cannot start instance", err) log.Fatal("discord: cannot start instance", err)
} }
log.Print("main: discord instance running") log.Printf("main: discord instance running")
log.Print("main: — — —") log.Printf("main: — — —")
<-p.Context.Done() <-p.Context.Done()
log.Print("main: stopping galched-bot") log.Print("main: stopping galched-bot")

View file

@ -2,6 +2,7 @@ package discord
import ( import (
"fmt" "fmt"
"log"
"galched-bot/modules/settings" "galched-bot/modules/settings"
@ -11,6 +12,8 @@ import (
type ( type (
Discord struct { Discord struct {
appVersion string
processor *HandlerProcessor
session *discordgo.Session session *discordgo.Session
} }
) )
@ -21,10 +24,29 @@ func New(s *settings.Settings) (*Discord, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "cannot create discord instance") return nil, errors.Wrap(err, "cannot create discord instance")
} }
return &Discord{session: instance}, nil
processor := NewProcessor(s.Version)
log.Printf("discord: added %d message handlers", len(processor.handlers))
if len(processor.handlers) > 0 {
for i := range processor.handlers {
log.Printf("discord: %d) %s", i+1, processor.handlers[i].Signature())
}
}
return &Discord{
appVersion: s.Version,
processor: processor,
session: instance,
}, nil
}
func LogMessage(m *discordgo.MessageCreate) {
log.Printf("discord: msg [%s]: %s", m.Author.Username, m.Content)
} }
func (d *Discord) Start() error { func (d *Discord) Start() error {
d.session.AddHandler(d.processor.Process)
return d.session.Open() return d.session.Open()
} }

View file

@ -0,0 +1,68 @@
package discord
import (
"fmt"
"log"
"strings"
"github.com/bwmarrin/discordgo"
)
type (
MessageHandler interface {
Signature() string
Description() string
IsValid(string) bool
Handle(s *discordgo.Session, m *discordgo.MessageCreate)
}
HandlerProcessor struct {
appVersion string
handlers []MessageHandler
}
)
func NewProcessor(appVersion string) *HandlerProcessor {
return &HandlerProcessor{
appVersion: appVersion,
}
}
func (h *HandlerProcessor) AddHandler(handler MessageHandler) {
h.handlers = append(h.handlers, handler)
}
func (h *HandlerProcessor) HelpMessage() string {
helpMessage := fmt.Sprintf("ГалчедБот версии %s. Описание команд: \n"+
" **!galched** - список команд бота\n", h.appVersion)
for i := range h.handlers {
helpMessage += fmt.Sprintf(" **%s** - %s\n",
h.handlers[i].Signature(),
h.handlers[i].Description())
}
return strings.TrimRight(helpMessage, "\n")
}
func (h *HandlerProcessor) Process(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if strings.HasPrefix(m.Content, "!galched") {
LogMessage(m)
_, err := s.ChannelMessageSend(m.ChannelID, h.HelpMessage())
if err != nil {
log.Printf("discord: cannot send message [%s]: %v", h.HelpMessage(), err)
}
}
for i := range h.handlers {
if h.handlers[i].IsValid(m.Content) {
h.handlers[i].Handle(s, m)
break
}
}
}

View file

@ -0,0 +1,25 @@
package discord
import (
"github.com/bwmarrin/discordgo"
)
type testHandler struct{}
var _ = (testHandler)(nil) // ignore unused warning
func (t *testHandler) Signature() string {
return "!test"
}
func (t *testHandler) Description() string {
return "тестовый хэндлер"
}
func (t *testHandler) IsValid(msg string) bool {
return msg == "!test"
}
func (t *testHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
LogMessage(m)
}