From bb26c579856ee16cbd5554ac1a37d1cbbfa97d27 Mon Sep 17 00:00:00 2001 From: alexvanin Date: Fri, 10 May 2019 12:55:52 +0300 Subject: [PATCH] Added message handling mechanism for discord module --- main.go | 4 +- modules/discord/discord.go | 26 ++++++++++++- modules/discord/handlers.go | 68 ++++++++++++++++++++++++++++++++++ modules/discord/testhandler.go | 25 +++++++++++++ 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 modules/discord/handlers.go create mode 100644 modules/discord/testhandler.go diff --git a/main.go b/main.go index d2073ab..2196859 100644 --- a/main.go +++ b/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") diff --git a/modules/discord/discord.go b/modules/discord/discord.go index c12da33..3a19213 100644 --- a/modules/discord/discord.go +++ b/modules/discord/discord.go @@ -2,6 +2,7 @@ package discord import ( "fmt" + "log" "galched-bot/modules/settings" @@ -11,7 +12,9 @@ import ( type ( Discord struct { - session *discordgo.Session + 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() } diff --git a/modules/discord/handlers.go b/modules/discord/handlers.go new file mode 100644 index 0000000..a7d9234 --- /dev/null +++ b/modules/discord/handlers.go @@ -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 + } + } +} diff --git a/modules/discord/testhandler.go b/modules/discord/testhandler.go new file mode 100644 index 0000000..8ed4370 --- /dev/null +++ b/modules/discord/testhandler.go @@ -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) +}