Added message handling mechanism for discord module
This commit is contained in:
parent
bf279581fb
commit
bb26c57985
4 changed files with 119 additions and 4 deletions
4
main.go
4
main.go
|
@ -34,8 +34,8 @@ func start(p appParam) {
|
|||
if err != nil {
|
||||
log.Fatal("discord: cannot start instance", err)
|
||||
}
|
||||
log.Print("main: discord instance running")
|
||||
log.Print("main: — — —")
|
||||
log.Printf("main: discord instance running")
|
||||
log.Printf("main: — — —")
|
||||
|
||||
<-p.Context.Done()
|
||||
log.Print("main: stopping galched-bot")
|
||||
|
|
|
@ -2,6 +2,7 @@ package discord
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"galched-bot/modules/settings"
|
||||
|
||||
|
@ -11,6 +12,8 @@ import (
|
|||
|
||||
type (
|
||||
Discord struct {
|
||||
appVersion string
|
||||
processor *HandlerProcessor
|
||||
session *discordgo.Session
|
||||
}
|
||||
)
|
||||
|
@ -21,10 +24,29 @@ func New(s *settings.Settings) (*Discord, error) {
|
|||
if err != nil {
|
||||
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 {
|
||||
d.session.AddHandler(d.processor.Process)
|
||||
return d.session.Open()
|
||||
}
|
||||
|
||||
|
|
68
modules/discord/handlers.go
Normal file
68
modules/discord/handlers.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
25
modules/discord/testhandler.go
Normal file
25
modules/discord/testhandler.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue