Skip to content

Commit

Permalink
Rearrange middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kriive committed Jan 18, 2024
1 parent 8d3a476 commit 4e2797e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
8 changes: 5 additions & 3 deletions discord/ctf.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (s *Server) handleCommandNewCTF(event *handler.CommandEvent) error {
_, err = event.CreateFollowupMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetColor(ColorBlurple).
SetTitle(":white_check_mark: Confirm creation").
SetDescriptionf("Would you like to create a new CTF named `%s`?", ctfName).
Build()).
SetEphemeral(true).
Expand Down Expand Up @@ -101,10 +102,11 @@ func (s *Server) handleCommandDeleteCTF(event *handler.CommandEvent) error {

ctfName := ctf.Name()

err = event.CreateMessage(discord.NewMessageCreateBuilder().
_, err = event.CreateFollowupMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetColor(ColorRed).
SetDescriptionf("Would you like to delete `%s`? There's no undo.", ctfName).
SetColor(ColorFuchsia).
SetTitle(":warning: Confirm deletion").
SetDescriptionf("Are you sure you want to delete `%s`? There's no undo.", ctfName).
Build()).
SetEphemeral(true).
AddActionRow(
Expand Down
4 changes: 2 additions & 2 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Error(event CreateFollowupMessager, err error) error {
code, message := havcebot.ErrorCode(err), havcebot.ErrorMessage(err)

if code == havcebot.EINTERNAL {
event.Client().Logger().Error("Internal server error", code, message)
event.Client().Logger().Error("Internal server error", code, err)
}

// Print user message to response.
Expand All @@ -29,7 +29,7 @@ func Error(event CreateFollowupMessager, err error) error {
// messageError is a utility that builds and outputs a embed.
func messageEmbedError(message string) discord.Embed {
return discord.NewEmbedBuilder().
SetTitlef(":warning: There was an error while handling your request.").
SetTitlef(":octagonal_sign: There was an error while handling your request.").
SetColor(ColorRed).
SetDescriptionf(message).
SetField(0, "Message", message, true).Build()
Expand Down
23 changes: 19 additions & 4 deletions discord/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,38 @@ import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/disgo/handler/middleware"
"github.com/havce/havcebot"
)

// AdminOnly restricts access to the routes to Administrators only.
var AdminOnly handler.Middleware = func(next handler.Handler) handler.Handler {
return func(e *events.InteractionCreate) error {
if e.Member().Permissions.Has(discord.PermissionAdministrator) {
return next(e)
return middleware.Defer(discord.InteractionTypeComponent, false, true)(
middleware.Defer(discord.InteractionTypeApplicationCommand, false, true)(next),
)(e)
}

return e.Respond(discord.InteractionResponseTypeCreateMessage,
_ = e.Respond(discord.InteractionResponseTypeCreateMessage,
discord.NewMessageCreateBuilder().
SetEphemeral(true).
SetEmbeds(messageEmbedError("You're not authorized to run this command.")).Build())

return havcebot.Errorf(havcebot.EUNAUTHORIZED, "You're not authorized to run this command.")
}
}

func (s *Server) MustBeInsideCTFAndAdmin(next handler.Handler) handler.Handler {
return func(e *events.InteractionCreate) error {
return AdminOnly(s.MustBeInsideCTF(next))(e)
if !e.Member().Permissions.Has(discord.PermissionAdministrator) {
_ = e.Respond(discord.InteractionResponseTypeCreateMessage,
discord.NewMessageCreateBuilder().
SetEphemeral(true).
SetEmbeds(messageEmbedError("You're not authorized to run this command.")).Build())
return havcebot.Errorf(havcebot.EUNAUTHORIZED, "You're not authorized to run this command.")
}
return s.MustBeInsideCTF(next)(e)
}
}

Expand All @@ -46,6 +59,8 @@ func (s *Server) MustBeInsideCTF(next handler.Handler) handler.Handler {
return err
}

return next(e)
return middleware.Defer(discord.InteractionTypeComponent, false, true)(
middleware.Defer(discord.InteractionTypeApplicationCommand, false, true)(next),
)(e)
}
}
16 changes: 7 additions & 9 deletions discord/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,33 @@ func NewServer() *Server {
// order of evaluation is important. It isn't super clean, but it works.
s.router.Group(func(r handler.Router) {
r.Use(AdminOnly)
r.Use(middleware.Defer(discord.InteractionTypeApplicationCommand, false, true))
r.Use(middleware.Defer(discord.InteractionTypeComponent, false, true))

r.Command("/new", s.handleCommandNewCTF)
r.Component("/new/{ctf}/create", s.handleCreateCTF)
r.Command("/close", s.handleUpdateCanJoin(false))
r.Command("/open", s.handleUpdateCanJoin(true))
r.Command("/vote", s.handleInfoCTF(true))
})

// Admin only routes and must be under a registered CTF.
s.router.Group(func(r handler.Router) {
r.Use(s.MustBeInsideCTFAndAdmin)
r.Command("/delete", s.handleCommandDeleteCTF)
r.Component("/delete/really", s.handleDeleteCTF)
r.Command("/close", s.handleUpdateCanJoin(false))
r.Command("/open", s.handleUpdateCanJoin(true))
})

// These routes must be hit while inside of a CTF.
// These routes must be hit while inside of a CTF, but don't
// require any admin priviledge.
s.router.Group(func(r handler.Router) {
r.Use(s.MustBeInsideCTF)
r.Use(middleware.Defer(discord.InteractionTypeApplicationCommand, false, true))
r.Use(middleware.Defer(discord.InteractionTypeComponent, false, true))

r.Component("/join/{ctf}", s.handleJoinCTF)
r.Command("/flag", s.handleFlag(false))
r.Command("/blood", s.handleFlag(true))
r.Command("/chal", s.handleNewChal)
})

// These routes can be use by anyone. They won't create any public message.
// These routes can be used by anyone.
// They won't create any public message.
s.router.Group(func(r handler.Router) {
r.Use(middleware.Defer(discord.InteractionTypeApplicationCommand, false, true))
r.Use(middleware.Defer(discord.InteractionTypeComponent, false, true))
Expand Down

0 comments on commit 4e2797e

Please sign in to comment.