Cleaned up name logic

This commit is contained in:
Atridad Lahiji 2023-12-27 17:56:21 -07:00
parent 45563a0c11
commit feca85e024
No known key found for this signature in database
2 changed files with 73 additions and 46 deletions

62
lib/helpers.go Normal file
View file

@ -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}
}
}

57
main.go
View file

@ -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!"),
}
}