From feca85e024be9ec37f62e3d8a94afe432f9f3ba6 Mon Sep 17 00:00:00 2001 From: atridadl Date: Wed, 27 Dec 2023 17:56:21 -0700 Subject: [PATCH] Cleaned up name logic --- lib/helpers.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 57 +++++++++------------------------------------- 2 files changed, 73 insertions(+), 46 deletions(-) create mode 100644 lib/helpers.go diff --git a/lib/helpers.go b/lib/helpers.go new file mode 100644 index 0000000..c4a7a9a --- /dev/null +++ b/lib/helpers.go @@ -0,0 +1,62 @@ +package lib + +import ( + "github.com/diamondburned/arikawa/v3/discord" +) + +// Userish is an interface that captures the common methods you may want to call +// on either a discord.Member or discord.User, including a display name. +type Userish interface { + ID() discord.UserID + Username() string + DisplayName() string // The added method for getting the display name +} + +// memberUser adapts a discord.Member to the Userish interface. +type memberUser struct { + *discord.Member +} + +func (mu memberUser) ID() discord.UserID { + return mu.User.ID +} + +func (mu memberUser) Username() string { + return mu.User.Username +} + +func (mu memberUser) DisplayName() string { + // If Nick is set, return it as the display name, otherwise return Username + if mu.Member.Nick != "" { + return mu.Member.Nick + } + return mu.User.Username +} + +// directUser adapts a discord.User to the Userish interface. +type directUser struct { + *discord.User +} + +func (du directUser) ID() discord.UserID { + return du.User.ID +} + +func (du directUser) Username() string { + return du.User.Username +} + +func (du directUser) DisplayName() string { + // For a direct user, the display name is just the username since no nickname is available. + return du.User.Username +} + +// GetUserObject takes an interaction event and returns a Userish, which may be +// either a discord.Member or a discord.User, but exposes it through a consistent interface. +func GetUserObject(event discord.InteractionEvent) Userish { + if event.Member != nil { + return memberUser{event.Member} + } else { + return directUser{event.User} + } +} diff --git a/main.go b/main.go index 573805f..527af85 100644 --- a/main.go +++ b/main.go @@ -131,24 +131,15 @@ func (h *handler) cmdPing(ctx context.Context, data cmdroute.CommandData) *api.I func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { // Cooldown Logic - member := data.Event.Member - user := data.Event.User + user := lib.GetUserObject(*data.Event) - var idToSend string - - if member != nil { - idToSend = member.User.ID.String() - } else { - idToSend = user.ID.String() - } - - cachedVal := lib.GetCache(idToSend + ":" + "ask") + cachedVal := lib.GetCache(user.ID().String() + ":" + "ask") if cachedVal != "nil" { return &api.InteractionResponseData{ Content: option.NewNullableString("Please wait for the cooldown!"), } } - lib.SetCache(idToSend+":"+"ask", idToSend+":"+"ask", 1) + lib.SetCache(user.ID().String()+":"+"ask", user.ID().String()+":"+"ask", 1) // Command Logic var options struct { @@ -190,24 +181,15 @@ func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.In func (h *handler) cmdPic(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { // Cooldown Logic - member := data.Event.Member - user := data.Event.User + user := lib.GetUserObject(*data.Event) - var idToSend string - - if member != nil { - idToSend = member.User.ID.String() - } else { - idToSend = user.ID.String() - } - - cachedVal := lib.GetCache(idToSend + ":" + "pic") + cachedVal := lib.GetCache(user.ID().String() + ":" + "pic") if cachedVal != "nil" { return &api.InteractionResponseData{ Content: option.NewNullableString("Please wait for the cooldown!"), } } - lib.SetCache(idToSend+":"+"pic", idToSend+":"+"pic", 1) + lib.SetCache(user.ID().String()+":"+"pic", user.ID().String()+":"+"pic", 1) // Command Logic var options struct { @@ -279,23 +261,15 @@ func (h *handler) cmdPic(ctx context.Context, data cmdroute.CommandData) *api.In func (h *handler) cmdHDPic(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { // Cooldown Logic - member := data.Event.Member - user := data.Event.User + user := lib.GetUserObject(*data.Event) - var idToSend string - - if member != nil { - idToSend = member.User.ID.String() - } else { - idToSend = user.ID.String() - } - cachedVal := lib.GetCache(idToSend + ":" + "hdpic") + cachedVal := lib.GetCache(user.ID().String() + ":" + "hdpic") if cachedVal != "nil" { return &api.InteractionResponseData{ Content: option.NewNullableString("Please wait for the cooldown!"), } } - lib.SetCache(idToSend+":"+"hdpic", idToSend+":"+"hdpic", 10) + lib.SetCache(user.ID().String()+":"+"hdpic", user.ID().String()+":"+"hdpic", 10) // Command Logic var options struct { @@ -354,20 +328,11 @@ func (h *handler) cmdHS(ctx context.Context, data cmdroute.CommandData) *api.Int if err := data.Options.Unmarshal(&options); err != nil { return errorResponse(err) } - - member := data.Event.Member - user := data.Event.User - var nameToSend string - - if member != nil { - nameToSend = member.User.DisplayName - } else { - nameToSend = user.DisplayName - } + user := lib.GetUserObject(*data.Event) return &api.InteractionResponseData{ - Content: option.NewNullableString(options.Arg + " was " + nameToSend + "'s nickname in highschool!"), + Content: option.NewNullableString(options.Arg + " was " + user.DisplayName() + "'s nickname in highschool!"), } }