From 661ce08764b84418ae0e2cf677c60ac40942262d Mon Sep 17 00:00:00 2001 From: DutchEllie Date: Fri, 4 Jun 2021 16:07:19 +0200 Subject: [PATCH] log --- discord/middleware.go | 25 +++++++++++++++++++++++++ discord/mux/server.go | 6 ++++++ limiter/action.go | 7 +++++++ 3 files changed, 38 insertions(+) diff --git a/discord/middleware.go b/discord/middleware.go index a8cd5b5..a8b1b4b 100644 --- a/discord/middleware.go +++ b/discord/middleware.go @@ -3,8 +3,16 @@ package main import ( "github.com/bwmarrin/discordgo" "quenten.nl/pepebot/discord/mux" + "quenten.nl/pepebot/limiter" ) +/* +Middleware chain + +Logtoconsole -> loginteraction -> mux -> command + +*/ + func (app *application) LogToConsole(next mux.Command) mux.Command { fn := func(s *discordgo.Session, m *discordgo.MessageCreate) { app.infoLog.Printf("%s \tsaid: %s\n", m.Author.Username, m.Content) @@ -12,3 +20,20 @@ func (app *application) LogToConsole(next mux.Command) mux.Command { } return mux.HandlerFunc(fn) } + +func (app *application) LogInteraction(next mux.Command) mux.Command { + fn := func(s *discordgo.Session, m *discordgo.MessageCreate) { + // Logging interaction + a := limiter.NewAction("Any message") + app.limiter.Logs[m.Author.ID] = append(app.limiter.Logs[m.Author.ID], a) + + // Checking if rate limit exceeded + err := app.limiter.CheckAllowed(m.Author.ID) + if err != nil { + mux.NotFound(s, m) + } else { + next.Execute(s, m) + } + } + return mux.HandlerFunc(fn) +} diff --git a/discord/mux/server.go b/discord/mux/server.go index 8822a29..f8790d9 100644 --- a/discord/mux/server.go +++ b/discord/mux/server.go @@ -11,6 +11,12 @@ type Command interface { Execute(s *discordgo.Session, m *discordgo.MessageCreate) } +func NotFound(s *discordgo.Session, m *discordgo.MessageCreate) { + return +} + +func NotFoundHandler() Command { return HandlerFunc(NotFound) } + type HandlerFunc func(s *discordgo.Session, m *discordgo.MessageCreate) func (f HandlerFunc) Execute(s *discordgo.Session, m *discordgo.MessageCreate) { diff --git a/limiter/action.go b/limiter/action.go index 920f92d..48a5f1d 100644 --- a/limiter/action.go +++ b/limiter/action.go @@ -6,3 +6,10 @@ type Action struct { Type string Timestamp time.Time } + +func NewAction(t string) *Action { + a := new(Action) + a.Timestamp = time.Now() + a.Type = t + return a +}