pepebot/discord/middleware.go

40 lines
926 B
Go
Raw Permalink Normal View History

2021-06-03 13:45:07 +02:00
package main
2021-06-04 09:54:28 +02:00
import (
"github.com/bwmarrin/discordgo"
"quenten.nl/pepebot/discord/mux"
2021-06-04 16:07:19 +02:00
"quenten.nl/pepebot/limiter"
2021-06-04 09:54:28 +02:00
)
2021-06-03 13:45:07 +02:00
2021-06-04 16:07:19 +02:00
/*
Middleware chain
Logtoconsole -> loginteraction -> mux -> command
*/
2021-06-04 09:54:28 +02:00
func (app *application) LogToConsole(next mux.Command) mux.Command {
2021-06-03 13:45:07 +02:00
fn := func(s *discordgo.Session, m *discordgo.MessageCreate) {
2021-06-03 14:07:11 +02:00
app.infoLog.Printf("%s \tsaid: %s\n", m.Author.Username, m.Content)
2021-06-03 13:45:07 +02:00
next.Execute(s, m)
}
2021-06-04 09:54:28 +02:00
return mux.HandlerFunc(fn)
2021-06-03 13:45:07 +02:00
}
2021-06-04 16:07:19 +02:00
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)
}